Home

Drawing a Line

 

A Line

A line is a junction of two points. This means that a line has a beginning and an end:

Line Definition

The beginning and the end are two distinct points. Based on this, a line is represented either with two Point values or by four numbers representing its values on the Cartesian axes. To draw a line, the Graphics class is equipped with the following overloaded DrawLine() method: 

public:
    void DrawLine(Pen ^pen,
		  Point pt1,
		  Point pt2);
    void DrawLine(Pen ^pen,
		  PointF pt1,
		  PointF pt2);
    void DrawLine(Pen ^pen,
		  int x1,
		  int y1,
		  int x2,
		  int y2);
    void DrawLine(Pen ^pen,
		  float x1,
		  float y1,
		  float x2,
		  float y2);

If the line is represented with natural numbers, its origin can be specified as a Point pt1 and its end would be represented with a Point pt2. If the line is drawn using floating numbers, you can start it at one PointF pt1 and end it at another PointF pt2. Otherwise, you can specify the starting point with coordinates (x1, y1) and the end would be represented with coordinates (x2, y2). The same type of line can be drawn using decimal values from coordinates (x1, y1) to coordinates (x2, y2).

Here is an example that draws three lines:

System::Void Form1_Paint(System::Object ^  sender, 
			 System::Windows::Forms::PaintEventArgs ^  e)
{
    Pen ^penCurrent = gcnew Pen(Color::Red);
    e->Graphics->DrawLine(penCurrent, 20, 20, 205, 20);

    penCurrent = gcnew Pen(Color::Green);
    e->Graphics->DrawLine(penCurrent, 40, 40, 225, 40);

    penCurrent = gcnew Pen(Color::Blue);
    e->Graphics->DrawLine(penCurrent, 30, 60, 215, 60);
}

Line

 

Home Copyright © 2007-2013, FunctionX Home