Solved

TImage.Canvas Polygon

Posted on 1998-12-18
2
655 Views
Last Modified: 2010-05-19
Hi, how do I draw a user defined polygon ? The user clicks
with the mouse on some points. Then a polygon should be drawn. How do I add the points to the polygon and how do I create an array for them ? Source ?
0
Comment
Question by:ItsMe
2 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 30 total points
ID: 1351761
Hi ItsMe,

This is a way :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

const
  MaxPoints = 10;

type
  TForm1 = class(TForm)
    PaintBox1: TPaintBox;
    StorePoly: TButton;
    DrawPoly: TButton;
    Memo1: TMemo;
    procedure StorePolyClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure DrawPolyClick(Sender: TObject);
    procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
    StoringMode : Boolean;
    PolygonArray : Array[0..MaxPoints-1] of TPoint;
    PointNumber : 0..MaxPoints-1;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.StorePolyClick(Sender: TObject);
begin
 {clear canvas}
 with paintbox1.canvas do begin
  Brush.Color := clWhite;
  FillRect(clipRect);
 end;
 memo1.lines.clear;
{clear polygonarray}
 FillChar(PolygonArray,SizeOf(PolygonArray),0);
 PointNumber := 0;
{start storing polygon  }
 storingMode := true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 StoringMode := false;
end;

procedure TForm1.DrawPolyClick(Sender: TObject);
begin
 {draw the polygon}
 storingMode := false;
 memo1.Lines.Add('Now drawing Polygon');
 with paintbox1.canvas do begin
  Brush.Color := clBlack;
  Polygon(Slice(PolygonArray,PointNumber));
 end;
end;

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 {Store X,Y value in grid}
 if StoringMode then begin
  if PointNumber < MaxPoints then begin
   PolygonArray[PointNumber].X := X;
   PolygonArray[PointNumber].Y := Y;
   Inc(PointNumber);
   {show point}
   PaintBox1.Canvas.Pixels[X,Y] := clRed;
   {put point in memo}
   memo1.Lines.Add('Pointnr. : '+IntToStr(PointNumber)+
                   ' at position [X:'+IntToStr(X)+', Y:'+IntToStr(Y)+']');
  end
   else showmessage('sorry polygon points are limited to: '+IntToStr(MaxPoints));
  end
   else showmessage('please press StorePoly button to start defining the polygon');
end;

end.

Regards, Zif.
0
 

Author Comment

by:ItsMe
ID: 1351762
Adjusted points to 30
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question