C# Point, PointF StructsUse the Point type, which is found in the System.Drawing namespace.
dot net perls
Point. This C# struct 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.
An example. An empty Point is one where X and Y are equal to zero. You can use the constructor that receives 2 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.
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 2 ways. One way uses 2 floating-point arguments. The other uses the parameterless constructor.
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 } }
{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}
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.
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); } }
{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
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.
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.
Size, SizeF
Point, WPF. Another Point type is available in WPF. This is a struct found in the System.Windows namespace. We can use the constructor to specify an X and a Y at creation time.
Note Thanks to Frans for writing in with information about the Point struct in WPF, which is separate from the one in System.Drawing.
A summary. We used Point from the System.Drawing namespace in the .NET Framework. Another option you could use instead of Point is a KeyValuePair of 2 integers, but this would not provide the helper methods available on Point.
© 2007-2021 sam allen. see site info on the changelog