Home
C#
Point, PointF Structs
Updated Jan 4, 2024
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 .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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jan 4, 2024 (edit).
Home
Changes
© 2007-2025 Sam Allen