Home
Map
Point, PointF StructsUse the Point type, which is found in the System.Drawing namespace.
C#
This page was last reviewed on Jan 4, 2024.
Point. This C# struct represents an X and a Y coordinate grouped together. Points are useful whenever you need to store coordinates. Sometimes other .NET methods require this type.
Point versus PointF. What is the difference between Point and PointF? Point uses int coordinates. PointF uses floating-point (float) coordinates—these have a fractional part.
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.
using System; using System.Drawing; // 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}
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.
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
Type notes. Conceptually, any PointF that has 0 as its X property and 0 as its Y property is empty. The default PointF is empty, and a PointF created with arguments 0, 0 is also empty.
Detail Is the PointF type a class or a struct? We find that PointF is a struct type.
Thus The PointF type provides a way for you to represent a coordinate of floating-point values.
Size
Summary. We used Point from the System.Drawing namespace. We could use a KeyValuePair of 2 integers, but this would not provide the helper methods available on Point.
KeyValuePair
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 4, 2024 (edit).
Home
Changes
© 2007-2024 Sam Allen.