TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Point, PointF Structs

This C# tutorial demonstrates the Point type, which is found in System.Drawing.

Point represents an X and a Y coordinate grouped together.

Points are useful whenever you need to store coordinates. Sometimes other methods in the .NET Framework require this type.

Point. An empty Point is one where X and Y are equal to zero. You can use the constructor that receives two integers to create a Point. Next, we show Point.Add and Subtract: these add or subtract each coordinate from a Point and a Size.

Finally: Point.Ceiling, Round and Truncate all act upon a PointF argument. They compute a mathematical function on each coordinate.

Math.CeilingMath.RoundMath.Truncate

C# program that uses Point

using System;
using System.Drawing;

class Program
{
    static void Main()
    {
	// Empty point.
	Point point0 = new Point();
	Console.WriteLine(point0);
	Console.WriteLine(point0.IsEmpty);

	// Create point.
	Point point1 = new Point(4, 6);
	Console.WriteLine(point1); // 4, 6

	// Create size.
	Size size = new Size(2, 2);

	// Add point and size.
	Point point3 = Point.Add(point1, size);
	Console.WriteLine(point3); // 6, 8

	// Subtract size from point.
	Point point4 = Point.Subtract(point1, size);
	Console.WriteLine(point4); // 2, 4

	// Compute ceiling from PointF.
	Point point5 = Point.Ceiling(new PointF(4.5f, 5.6f));
	Console.WriteLine(point5); // 5, 6

	// Round to point.
	Point point6 = Point.Round(new PointF(4.5f, 5.7f));
	Console.WriteLine(point6); // 4, 6

	// Truncate to point.
	Point point7 = Point.Truncate(new PointF(4.7f, 5.8f));
	Console.WriteLine(point7); // 4, 5
    }
}

Output

{X=0,Y=0}
True
{X=4,Y=6}
{X=6,Y=8}
{X=2,Y=4}
{X=5,Y=6}
{X=4,Y=6}
{X=4,Y=5}

PointF. The PointF type is the same as the Point type but it stores floating point numbers (float). The decision of which to use depends on the nature of the coordinate system in your program.

Tip: You can create a PointF instance in two ways. One way uses two floating-point arguments. The other uses the parameterless constructor.

C# program that uses PointF struct

using System;
using System.Drawing;

class Program
{
    static void Main()
    {
	PointF pointF = new PointF(4.5f, 6.5f);
	Display(pointF);

	pointF = new PointF();
	Display(pointF);

	pointF = new PointF(0, 0);
	Display(pointF);
    }

    static void Display(PointF pointF)
    {
	Console.WriteLine(pointF);
	Console.WriteLine(pointF.X);
	Console.WriteLine(pointF.Y);
	Console.WriteLine(pointF.IsEmpty);
    }
}

Output

{X=4.5, Y=6.5}
4.5
6.5
False
{X=0, Y=0}
0
0
True
{X=0, Y=0}
0
0
True

Emptiness. Conceptually, any PointF that has 0 as its X property and 0 as its Y property is empty. You can see that the default PointF is empty, and a PointF created with arguments 0, 0 is also empty.

Point versus PointF. What is the difference between Point and PointF? Point uses integer coordinates (specified by the int type). PointF uses floating-point (float) coordinates—these have a fractional part.

Class or struct: Is the PointF type a class or a struct? We find that PointF is a struct type.

Note: The documentation for the PointF constructor with two arguments calls it a class. In most programs, this makes no difference.

The PointF type provides a way for you to represent a coordinate of floating-point values. This can be used in various types in the System.Drawing namespace or in any other code that requires this representation in memory.

SizeF

Summary. We looked the Point type from the System.Drawing namespace in the .NET Framework. Another option you could use instead of Point is a KeyValuePair of two integers, but this would not provide the helper methods available on Point.

However: The KeyValuePair would eliminate the need to include the System.Drawing namespace.

KeyValuePair


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf