Title: How to draw an arrow (II)
{ .... }
var
BeginPoint: TPoint;
{ .... }
uses Math;
{ .... }
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
BeginPoint.X := X;
BeginPoint.Y := Y;
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
B, deltaX, deltaY: Extended;
begin
Image1.Canvas.PenPos := BeginPoint;
// Beginpoint is the point from where the use drew the line
Image1.Canvas.LineTo(X, Y);
if BeginPoint.X X then // checks for division by zero
begin
if (BeginPoint.X X) then
B := DegToRad(135) ?C ArcTan((BeginPoint.Y ?C Y) / (BeginPoint.X ?C X))
else
B := DegToRad(45) ?C ArcTan((BeginPoint.Y ?C Y) / (BeginPoint.X ?C X));
// the arrow will have a 45 deg corner
deltaX := 15 * Cos(B); // 15 is the length of the arrow
deltaY := 15 * Sin(B);
if (BeginPoint.X X) then
begin
Image1.Canvas.PenPos := Point(X, Y);
Image1.Canvas.LineTo(X ?C Trunc(deltaX), Y + Trunc(deltaY));
Image1.Canvas.PenPos := Point(X, Y);
Image1.Canvas.LineTo(X + Trunc(deltaY), Y + Trunc(deltaX));
end
else
begin
Image1.Canvas.PenPos := Point(X, Y);
Image1.Canvas.LineTo(X ?C Trunc(deltaX), Y + Trunc(deltaY));
Image1.Canvas.PenPos := Point(X, Y);
Image1.Canvas.LineTo(X ?C Trunc(deltaY), Y ?C Trunc(deltaX));
end;
end
else
begin
if BeginPoint.Y Y then
begin
Image1.Canvas.PenPos := Point(X, Y);
Image1.Canvas.LineTo(X + 10, Y + 10);
Image1.Canvas.PenPos := Point(X, Y);
Image1.Canvas.LineTo(X ?C 10, Y + 10);
end
else
begin
Image1.Canvas.PenPos := Point(X, Y);
Image1.Canvas.LineTo(X + 10, Y ?C 10);
Image1.Canvas.PenPos := Point(X, Y);
Image1.Canvas.LineTo(X ?C 10, Y ?C 10);
end;
end;
end;