Home

Applications Accessories: The 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 provides a structure named Point. The Point structure is defined in the System.Drawing namespace that is a member of the System.Drawing.dll library.
   

Characteristics of a Point

One of the properties of the Point structure is X, which represents the horizontal distance of the point from the top-left corner of the object that owns the point. Another property, Y, represents the vertical measurement of the point with regards to the top-left corner of the object that owns the point. Based on this, a Point object can be represented on the Windows coordinate system as follows:

Representation of a Point

Both the X and the Y properties of the Point structure are of type int. In some cases, you will want to use either integer or decimal characteristics. To support this, the .NET Framework provides the PointF structure. Its X and Y properties are of type float.

We mentioned that a special point named the origin has coordinates (0, 0):

The Origin of the Windows default coordinate system

To indicate that this is the point you are referring to, the Point and the PointF structures are equipped with a static field named Empty. In this property, both the X and the Y values are set to 0:

Axes of the Windows Coordinate System

Here is an example of accessing it:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            Point origin = Point.Empty;

            MessageBox.Show(string.Format("P({0}, {1})", origin.X, origin.Y), "Origin");
        }
    }
}

To help you find out whether a point is empty, the Point and the PointF structures are equipped with a Boolean property named IsEmpty. You can enquire about this property using a conditional statement.

To create or identify a point, you can use one of three constructors of the Point or the PointF structure. One of these constructors uses the following syntax:

public Point(int x, int y)

This constructor takes a left and a top arguments. Here is an example of creating a point using it:

using private void btnPoint_Click(object sender, EventArgs e)
{
    Point pt = new Point(6, -2);

    MessageBox.Show(string.Format("P({0}, {1})", pt.X, pt.Y), "Coordinate");
}

The Point and the PointF structures are equipped with methods to perform various operations such as adding or subtracting points, etc.

 
 

Home Copyright © 2010-2011 C#Key