GDI Topics: Polygons



The polylines we have used so far were drawn by defining the starting point of the first line and the end point of the last line. There was no relationship or connection between these two extreme points. A polygon is a closed polyline. In other words, it is a polyline defined so that the end point of the last line is connected to the start point of the first line.

To draw a polygon, you can use the TCanvas::Polygon() method. Its syntax is:

void __fastcall Polygon(const TPoint * Points,
			const int Points_Size);

This member function uses the same types of arguments as the Polyline() method. The only difference is on the drawing of the line combination. Here is an example:

void __fastcall TForm1::FormPaint(TObject *Sender)
	TPoint Pt[7];

	Pt[0] = Point(20, 50);
	Pt[1] = Point(180, 50);
	Pt[2] = Point(180, 20);
	Pt[3] = Point(230, 70);
	Pt[4] = Point(180, 120);
	Pt[5] = Point(180, 90);
	Pt[6] = Point(20, 90);

	Canvas->Polygon(Pt, 7);

Multiple Polygons

If you want to draw a series of polygons, you can use the PolyPolygon() function whose syntax is:

BOOL PolyPolygon(HDC hdc,
                 CONST POINT *lpPoints,
                 CONST INT *lpPolyCounts,
                 int nCount);

The hdc argument is a handle to the canvas on which you want to draw.

Like the Polygon() method, the lpPoints argument is an array of POINT or TPoint values. The PolyPolygon() function needs to know the number of polygons you would be drawing. Each polygon uses the points of the lpPoints value but when creating the array of points, the values must be incremental: each polygon has its own set of points.

The lpPolyCounts argument is an array or integers. Each member of this array specifies the number of vertices (lines) that its polygon will have..

Unlike Polygon(), the nCount argument of PolyPolygon() is the number of polygons you want to draw and not the number of points.

Here is an example:

void __fastcall TForm1::FormPaint(TObject *Sender)
	TPoint Pt[12];
	int lpPts[] = { 3, 3, 3, 3 };

	// Top Triangle
	Pt[0] = Point(125, 10);
	Pt[1] = Point( 95, 70);
	Pt[2] = Point(155, 70);

	// Left Triangle
	Pt[3] = Point( 80, 80);
	Pt[4] = Point( 20, 110);
	Pt[5] = Point( 80, 140);

	// Bottom Triangle
	Pt[6] = Point( 95, 155);
	Pt[7] = Point(125, 215);
	Pt[8] = Point(155, 155);

	// Right Triangle
	Pt[9] = Point(170, 80);
	Pt[10] = Point(170, 140);
	Pt[11] = Point(230, 110);

	HDC hDC = Canvas->Handle;
	PolyPolygon(hDC, Pt, lpPts, 4);
Home Copyright © 2004-2016, FunctionX, Inc.