A Point

Introduction

All graphical objects and drawings have a primary characteristic which is their location. The location is the section where an object starts. This is also referred to as its origin, known as coordinate (0, 0). A location is identified by a point.

To support the concept of points, the .NET Framework first provides a structure named Point:

public struct Point : IEquatable<System.Drawing.Point>

The Point structure is defined in the System.Drawing namespace that is a member of the System.Drawing.dll library.

To create a point, declare a Point variable. You can initialize it using the default constructor of that structure. Here is an example:

namespace GraphicalApplication
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }
					
        private void Exercise_Load(object sender, EventArgs e)
        {
            Point origin = new Point();
        }
    }
}

Practical LearningPractical Learning: Introducing .NET Framework Collections

  1. Start Microsoft Visual Studio
  2. Create a Windows Forms App named GeometricAccessories

The Horizontal Location of a Point

One of the characteristics of a point is the horizontal distance of the point from the top-left corner of the object that owns the point. To support this value, the Point structure is equipped with the following constructor:

public Point(int value);

Based on this, to create a point, call this constructor and pass an integer to it.

The Point structure is highly used in .NET programming, especially in Windows Forms. As a result, the Location property of a Windows control is a Point object. Therefore, you can create a Point object and assign it to the Location property of a Windows control. Here is an example:

namespace GraphicalApplication
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }
	
        private void Exercise_Load(object sender, EventArgs e)
        {
            Button btnAccept = new Button();
	
            btnAccept.Location = new Point(44);
	
            btnAccept.Text = "Accept";
	
            Controls.Add(btnAccept);
        }
    }
}

The Coordinates of a Point

We saw that the primary characteristic of a point is its horizontal measurement. Another characteristic is the vertical measurement of the point with regards to the top-left corner of the object that owns the point. To support these characteristics, the Point structure is equipped with the following constructor:

public Point(int x, int y);

The first argument represents the horizontal position of the Point object. To support this value, the Point structure is equipped a property named X:

public int X { get; set; }

The second argument of the Point() constructor represents the vertical position of the object. To support that value, the Point structure is equipped with a property named Y:

public int Y { get; set; }

Notice that both the X and the Y properties of the Point structure are of type int. As a result, a point located at origin (0, 0) can be illustrated as follows:

Representation of a Point

The Point structure is highly used in .NET programming, especially in Windows Forms. As a result, the Location property of a Windows control is represented in a form (or the container of the control) as follows:

The Origin of the Windows default coordinate system

As mentioned already, the location of a Windows control can be specified using the Point(x, y) constructor. Here is an example:

namespace GraphicalApplication
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }
	
        private void Exercise_Load(object sender, EventArgs e)
        {
            Button btnAccept = new Button();
	
            btnAccept.Location = new Point(100, 40);
	
            btnAccept.Text = "Accept";

            Controls.Add(btnAccept);
        }
    }
}

An Empty Point

As we know already, you can create a point by declaring a Point variable using the default constructor of the structure. When you create such a point, it originally doesn't have values. As an option, the Point structure is equipped with a static field named Empty:

public static readonly System.Drawing.Point Empty;

One way you can use this field is to initialize a Point object with it. After doing that, you can specify one or both coordinates of the point if you want. Here is an example:

using System.Numerics;

namespace GraphicalApplication
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }
	
        private void Exercise_Load(object sender, EventArgs e)
        {
            Point pt = Point.Empty;
	
            pt.X = 248;
        }
    }
}

On the other hand, if you had already defined a point, you can reset it to the (0, 0) origin. To do that, you can assign the Point.Empty field to the Point object.

At any time, to help you find out whether a point is empty, the Point structure is equipped with a Boolean property named IsEmpty. You can enquire about this property using a conditional statement.

The Original Origin of a Shape

If you create a point by declaring a variable using the default constructor of a Point or PointF, or if you assign the Point.Empty or PointF.Empty field to the variable, the point is automatically located at (0, 0) or (0.00F, 0.00F). This is referred to as the origin of a point. It can be illustrated as follows:

Axes of the Windows Coordinate System

Of course at any time, you can specify or change the coordinates of the point.

A Point as an Object

A Point Variable

The point structures are type like any other. Therefore, in the body of a function, a method, or a type, you can declare a variable of type Point. Here is an example:

public readonly struct Design
{
    Point pt;
}

If you want a point that uses decimal numbers, declare the variable as PointF. After doing that, in the body of the function, method or tupe, you can use or ignore the variable.

A Point as an Argument

When creating a function or method, you can create a parameter whose is a point. Here is an example:

void Draw(Point coord)
{

}

In the body of the function or method, you can ignore or use the point.

A Point Property

In a type, you can create a property whose type is Point or PointF. If you want a complete property, you can first create a private field and use its variable in the property. Here is an example:

public class Drawer
{
    private Point pt;

    public Drawer(Point point)
    {
        pt = point;
    }

    public Point Point
    {
        get
        {
            return pt;
        }
        set
        {
            pt = value;
        }
    }
}

If you want a simple property, you can create it as an automatic one. Here is an example:

public readonly struct Design
{
    public Point Coordinate { get; set; }
}

In the same way, you can use a type, a function, or a method that uses a combination of Point and PointF objects.

Vectors

Introduction

A vector is a drawn or imaginary line that has a length (or distance) and indicates its direction. A vector must have a starting point and an end point. Obviously, the starting point indicates the origin of the vector. The end point indicates the direction where the segment of the vector ends. Vectors are used in various areas such as geometry and physics, including application programming. The .NET Framework supports vectors through various classes and structures.

Introduction to Vector Operations

In geometry, there are various operations that can be performed on a vector. To support those operations, the .NET Framework provides a static class named Vector:

public static class Vector

This class contains all methods necessary to perform all types of operations on a vector.

An Empty Vector

As mentioned in our introduction, a vector must have an origin. One of the common ways to indicate the direction of a vector is by specifying the horizontal and vertical point of its end. To support this description, the .NET Framework provides a structure named Vector2:

public struct Vector2 : IEquatable<System.Numerics.Vector2>, IFormattable

You can start a vector without specifying its details. To do this, you can declare a Vector2 variable using the default constructor of the structure. Here is an example:

using System.Numerics;

namespace WindowsFormsApplication
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }
	
        private void Exercise_Load(object sender, EventArgs e)
        {
            Vector2 vector = new Vector2();
        }
    }
}

The Measures of a Vector

As mentioned in our introduction, a vector has a direction. For a two-dimensional vector, the horizontal dimension is usually specified as an x value. To support this value, the Vector2 structure is equipped with a field named X:

public float X;

To support the vertical measure to its end point, the Vector2 structure is equipped with a field named Y:

public float Y;

As a result, after declaring a Vector2 variable, to specify one or both mesures to its end point, you can access one or both of those field and assign the desired value(s). Here are examples:

using System.Numerics;

namespace WindowsFormsApplication
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }
	
        private void Exercise_Load(object sender, EventArgs e)
        {
            Vector2 vector = new Vector2();

            vtr1.X = 12.75f;
            vtr1.Y = 48.05f;

            Vector2 vtr2 = new();

            vtr2.X = 612.75f;
            vtr2.Y = vtr1.Y;
        }
    }
}

The Units of a Vector

If you had already defined a vector, to let you get the horizontal measure of the end point, the Vector2 structure is equipped with a read-only property named UnitX:

public static System.Numerics.Vector2 UnitX { get; }

To let you get the vertical measure of the end point, the Vector2 structure is equipped with a read-only property named UnitX:

public static System.Numerics.Vector2 UnitX { get; }

Creating a Two-Dimensional Vector

In geometry, the horizontal and the vertical directions of a vector are usually represented with a distance. To let you provide these two pieces of information when creating a vector, the Vector2 structure is equipped with a constructor that takes two arguments. Its syntax is:

public Vector2 (float x, float y);

In some cases, you want both the horizontal and the vertical measures to have the same value. To let you create such a vector, the Vector2 structure is equipped with a constructor that takes one argument. Its syntax is:

public Vector2 (float value);

Operations on a Two-Dimensional Vector

As mentioned already, various operations can be performed on a vector. To support such operations on a 2D vectore, the Vector2 structure is equipped with various methods.

A Three-Dimensional Vector

Introduction

A three-dimensional vector has a horizontal measurement, a vertical measurement, and a depth measurement that lead to the end-point of the vector. To support such a vector, the .NET Framework provides a structure named Vector3.

The Depth of a Vector

The Vector3 structure is equipped with the X and the Y fields we saw for the Vector2 structure. To support the depth measure, the Vector3 structure is equipped with a field named Z:

public float Z;

Creating a Three-Dimensional Vector

To let you specify the coordinates of a three-dimensional vector, the Vector3 structure is equipped with a constructor that takes three arguments. Its syntax is:

public Vector2 (float x, float y, float z);

A Point with Floating-Point Numbers

Introduction

So far, we considered that a point is given in terms of natural numbers. There are situations for which you want the coordicates of a point to use more precision, in terms of a fraction of a number. To support this, the .NET Framework provides a structure named PointF:

public struct PointF : IEquatable<System.Drawing.PointF>

To let you specify that you want your point to use floating-point numbers, the PointF structure is equipped with a constructor that takes two arguments. Its syntax is:

public PointF (float x, float y);

The Point of a Vector

You can create a point using a 2D vector you have. To support this, the PointF structure is equipped with a constructor that takes a Vector2 argument. Its syntax is:

public PointF (System.Numerics.Vector2 vector);

The Size of an Object

Introduction

The distance from the left border to the right border of an object is referred to as its width. In the same way, the distance from the top to the bottom borders of a control is its height. This can be illustrated as follows:

The location and dimension of a control

The combination of the width and the height of an object is referred to as its size.

To support the size of an object, the System.Drawing namespace defines the Size structure. There are four characteristics that define a Size value: its location and its dimensions. A Size value must have a starting point (X, Y) just as the Point object was illustrated earlier. The width is the distance from the left to the right borders of a Size object. The height represents the distance from the top to the bottom borders of a Size value:

Size Representation

The members of a Size structure use values of type int. If you want to use decimal values, the .NET Framework provides the SizeF structure whose members deal with float values.

Characteristics of a Size

The Size and the SizeF structures are equipped with properties Width and Height that represent their width and height respectively.

To assist you with sizes, the Size structure provides the following constructor:

public Size(int width, int height);

The equivalent SizeF constructor uses the following syntax:

public SizeF(float width, float height);

You can also define a Size (or SizeF) object using a Point (or PointF) value. To support this, the Size structure is equipped with the following constructor:

public Size(Point pt);

The equivalent SizeF constructor uses the following syntax:

public SizeF(PointF pt);

After declaring a variable with this constructor, you can access its Width and Height properties to complete the definition of the Size object. If you already have the size of an object, you may only want to specify the dimensions of the variable.

A Rectangle

Introduction

The combination of the location and size of an object is represented as a rectangle: a geometric figure with four sides. To support this figure, the System.Drawing namespace provides the Rectangle and the RectangleF structures. A rectangle can be represented as follows:

Like every geometric representation in your program, a rectangular figure is based on a coordinate system whose origin is located on a top-left corner. The object that "owns" or defines the rectangle also owns this origin.

Characteristics of a Rectangle

To completely represent it, a rectangle is defined by its location and it size. The location is defined by a point on the top-left corner of the rectangle:

Based on this, a rectangle can be illustrated as follows:

Rectangle Representation

To create a rectangle, you must provide at least its location and dimensions. The location can be represented by a Point value and the dimensions can be represented with a Size value. Based on this, you can use the following constructor to declare a Rectangle variable:

public Rectangle(Point location, Size size);

The equivalent RectangleF constructor uses the following syntax:

public RectangleF(PointF location, SizeF size);

This constructor requires that you define a Point (or PointF) and a Size (or SizeF) in order to use it. If instead you know the integer values of the location and dimensions, you can use the following constructor to declare a Rectangle object:

public Rectangle(int x, int y, int width, int height);

The equivalent RectangleF constructor has the following syntax:

public RectangleF(float x, float y, float width, float height);

At any time, you can access or retrieve the characteristics of a Rectangle object as illustrated in the above picture from its properties. You use the same names we used in the picture.

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2010-2024, FunctionX Wednesday 17 April 2024 Next