Dibujando Polígonos con Delphi (Tcanvas)

Aquí os dejo una de las aplicaciones que hice para dibujar polígonos a partir de un número de aristas. Utilizando el canvas de un TImage, podemos hacer los dibujos que queramos. La aplicación se llama Thundax DrawPolygon, y podemos hacer multitud de polígonos (hasta 60 aristas), y dejo el código fuente para ver como hago los cálculos de los puntos y sus diferentes conexiones:


Como podéis observar, podemos seleccionar el número de puntos a dibujar, y si queremos dibujar todas las conexiones o no. De esta manera nos aparecen imágenes tan bonitas como la última. Espectacular, no?

Aquí dejo el código fuente de la implementación de los puntos y su dibujo:




var
Form3: TForm3;
edges: array of TPoint;

implementation

{$R *.dfm}

procedure TForm3.DrawRectangle(image1: TImage; BackGroundColor: Integer; RectangleColor: integer);
begin
image1.Canvas.Brush.Style := bsSolid;
image1.Canvas.Brush.Color := BackGroundColor;
image1.Canvas.Pen.Width := 3;
image1.Canvas.Pen.Color := RectangleColor;
image1.Canvas.Rectangle(0, 0, image1.Width, image1.Height);
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
DrawRectangle(polygon, clBlack, clLime);
end;

procedure TForm3.DrawLine(image1: TImage; point1: TPoint; point2: TPoint; colorLine: integer);
begin
image1.Canvas.Pen.Width := 1;
image1.Canvas.Pen.Color := colorLine;
image1.Canvas.MoveTo(point1.x, point1.y);
image1.Canvas.LineTo(point2.x, point2.y);
end;

procedure TForm3.DrawPoint(image1: TImage; point: TPoint; colorPoint: integer);
begin
image1.Canvas.Pen.Color := clyellow;
image1.Canvas.Pen.Width := 2;
image1.Canvas.Pie(point.x - 3, point.y - 3, point.x + 3, point.y + 3, 1, 1, 1, 1);
end;

procedure TForm3.DrawPolygon(image1: TImage; numberPoints: integer);
var
j, i, omega, rad, Offset: Integer;
Center: TPoint;
begin
setlength(edges, numberPoints + 1);
Offset := 20; //Pixeles de margen
Center := Point(image1.Height div 2, image1.Width div 2);
rad := Center.x - Offset;
omega := 360 div numberPoints;
DrawRectangle(image1, clBlack, clLime);
for i := 1 to numberPoints do
begin
edges[i] := Point(Center.y + trunc(rad * cos(omega * i * ((2 * Pi) / 360))),
Center.x + trunc(rad * sin(omega * i * ((2 * Pi) / 360))));
DrawPoint(image1, edges[i], clyellow);
end;

if not CheckBox1.Checked then
begin
for i := 1 to numberPoints - 1 do
DrawLine(image1, edges[i], edges[i + 1], cllime);
DrawLine(image1, edges[numberPoints], edges[1], cllime);
end
else
begin
for i := 1 to numberPoints do
for j := 1 to numberPoints do
DrawLine(image1, edges[i], edges[j], cllime);
end;
end;

procedure TForm3.Button4Click(Sender: TObject);
begin
DrawPolygon(polygon, cxSpinEdit1.value);
end;




A partir del centro, creo los puntos equidistantes con el incremento de un ángulo Omega, y voy pintando los puntos. Una vez los tengo, en función de si tengo activada la conexión de puntos, conecto con líneas cada uno de los puntos, sino solo conecto los puntos entre si llegando a aproximarme a un círculo.

Comments

Popular Posts