Offset
This method is available in both the Point and PointF classes, and it is mainly used to programmatically move or adjust the coordinates of a certain point. The Offset()
method modifies the current x and y coordinates of a point without returning a new Point object.
To use the Offset()
method, simply call it with two integer values as arguments on a Point
or PointF
object. The first argument will offset the Point's x coordinate, while the second argument will offset the Point's y-coordinate. This method also accepts negative values as arguments.
Below is an example of how to use the Offset()
method with a PointF
object in C#.
using System;
using System.Drawing; // Make sure to include the System.Drawing namespace
class Program
{
static void Main()
{
// Instantiate a PointF object with initial x and y coordinates
PointF point = new PointF(10.5f, 20.5f);
// Output initial coordinates
Console.WriteLine($"Initial Coordinates: X = {point.X}, Y = {point.Y}");
// Use the Offset method to adjust the PointF coordinates
// Move the point 5 units to the left and 10 units up
point.Offset(-5.0f, 10.0f);
// Output new coordinates after offsetting
Console.WriteLine($"New Coordinates: X = {point.X}, Y = {point.Y}");
}
}
using System;
using System.Drawing; // Make sure to include the System.Drawing namespace
class Program
{
static void Main()
{
// Instantiate a PointF object with initial x and y coordinates
PointF point = new PointF(10.5f, 20.5f);
// Output initial coordinates
Console.WriteLine($"Initial Coordinates: X = {point.X}, Y = {point.Y}");
// Use the Offset method to adjust the PointF coordinates
// Move the point 5 units to the left and 10 units up
point.Offset(-5.0f, 10.0f);
// Output new coordinates after offsetting
Console.WriteLine($"New Coordinates: X = {point.X}, Y = {point.Y}");
}
}
Imports System
Imports System.Drawing ' Make sure to include the System.Drawing namespace
Friend Class Program
Shared Sub Main()
' Instantiate a PointF object with initial x and y coordinates
Dim point As New PointF(10.5F, 20.5F)
' Output initial coordinates
Console.WriteLine($"Initial Coordinates: X = {point.X}, Y = {point.Y}")
' Use the Offset method to adjust the PointF coordinates
' Move the point 5 units to the left and 10 units up
point.Offset(-5.0F, 10.0F)
' Output new coordinates after offsetting
Console.WriteLine($"New Coordinates: X = {point.X}, Y = {point.Y}")
End Sub
End Class
Explanation:
Instantiating a
PointF
object: ThePointF
class is used to represent points with floating-point numbers for x and y coordinates. In this example, the point is initialized at (10.5, 20.5).Calling the
Offset()
method: The method adjusts the coordinates of thePointF
object by the specified offset values. Specifically,point.Offset(-5.0f, 10.0f);
will move the point's x coordinate by -5.0 and its y coordinate by +10.0.- Console output: The initial and new coordinates of the
PointF
object are printed to the console, demonstrating the effect of theOffset()
method.
This example should guide you on how to adjust coordinates in the PointF
or Point
class using the Offset()
method.