Drawing Polygons

From RAD Studio
Jump to: navigation, search

Go Up to Drawing Shapes


To draw a polygon with any number of sides on a canvas, call the Polygon method of the canvas.

Polygon takes an array of points as its only parameter and connects the points with the pen, then connects the last point to the first to close the polygon. After drawing the lines, Polygon uses the brush to fill the area inside the polygon.

For example, the following code draws a right triangle in the lower left half of a form:

Delphi

procedure TForm1.FormPaint(Sender: TObject);
var 
  vertices: array[0..2] of TPoint;
begin
  vertices[0] := Point(10, 10);
  vertices[1] := Point(10, ClientHeight - 10);
  vertices[2] := Point(ClientWidth - 10, ClientHeight - 10);
  Canvas.Polygon(vertices);
end

C++

void __fastcall TForm1::FormPaint(TObject *Sender) {
    TPoint vertices[3];
    vertices[0] = Point(10, 10);
    vertices[1] = Point(10, ClientHeight - 10);
    vertices[2] = Point(ClientWidth - 10, ClientHeight - 10);
    Canvas->Polygon(vertices, 2);
}

See Also