//
Pfeilspitzen erzeugen.
// Getestet mit RS 10.4 unter
Win11
// Variante 1:
(nur wwagerecht / senkrecht)

uses System.Types, System.UITypes;
type
art = (aLeft, aUp, aRight, aDown, aUp45);
procedure Arrow(cnv: TCanvas; x, y, long, thick: Integer; which: art;
P, B: TColor);
var
Points: array [0 .. 2] of TPoint;
t2: Integer;
begin
t2 := thick div 2;
with cnv do
begin
Pen.Color := P;
Brush.Color := B;
Points[0] := Point(x, y);
case which of
aLeft:
begin
Points[1].x := x + long;
Points[1].y := y - t2;
Points[2].x := x + long;
Points[2].y := y + t2;
end;
aRight:
begin
Points[1].x := x - long;
Points[1].y := y - t2;
Points[2].x := x - long;
Points[2].y := y + t2;
end;
aUp:
begin
Points[1].x := x - t2;
Points[1].y := y + long;
Points[2].x := x + t2;
Points[2].y := y + long;
end;
aDown:
begin
Points[1].x := x - t2;
Points[1].y := y - long;
Points[2].x := x + t2;
Points[2].y := y - long;
end;
end;
Polygon(Points);
end;
end;
// --- Beispiele --------------------------------------
// aLeft
procedure TForm1.Button1Click(Sender: TObject);
var
x, y, long, Distance: Integer;
begin
x := 100;
y := 100;
long := 20;
Distance := 150; // Strecke (einschl. Pfeil) = 150 Pixel
with canvas do
begin
Pen.Color := clBlack;
Pen.width := 1;
// Linie zeichnen
moveto(x + long, y);
lineto(x + Distance, y);
// --------------
Arrow(canvas, x, y, long, 12, aLeft, clBlack, clBlack);
end;
end;
// aRight
procedure TForm1.Button2Click(Sender: TObject);
var
x, y, long, Distance: Integer;
begin
x := 100;
y := 140;
long := 20;
Distance := 150;
with canvas do
begin
Pen.Color := clBlue;
Pen.width := 1;
moveto(x, y);
lineto(x + Distance - long, y);
Arrow(canvas, x + Distance, y, long, 12, aRight, Pen.Color, clYellow);
end;
end;
// aUp
procedure TForm1.Button3Click(Sender: TObject);
var
x, y, long, Distance: Integer;
begin
x := 150;
y := 50; // oben
long := 30;
Distance := 120;
with canvas do
begin
Pen.Color := clRed;
Pen.width := 1;
moveto(x, y + long);
lineto(x, y + Distance);
Arrow(canvas, x, y, long, 10, aUp, clRed, clRed);
end;
end;
// aDown
procedure TForm1.Button4Click(Sender: TObject);
var
x, y, long, Distance: Integer;
begin
x := 200;
y := 180; // unten
long := 20;
Distance := 120;
with canvas do
begin
Pen.Color := clGreen;
Pen.width := 3;
moveto(x, y - long);
lineto(x, y - Distance);
Arrow(canvas, x, y, long, 22, aDown, clFuchsia, clWhite);
end;
end;
//---------------------------------------------------------------
// Variante 2: (auch im Winkel)
uses System.Math, System.Types;
procedure DrawArrow(Canvas: TCanvas; x, y: Integer; Angle: Double;
Thick, Long: Integer);
var
Points: array [0 .. 2] of TPoint;
begin
Angle := DegToRad(Angle - 90);
Points[0] := Point(x, y);
Points[1].x := x - Trunc(Long * Cos(Angle - DegToRad(Thick)));
Points[1].y := y - Trunc(Long * Sin(Angle - DegToRad(Thick)));
Points[2].x := x - Trunc(Long * Cos(Angle + DegToRad(Thick)));
Points[2].y := y - Trunc(Long * Sin(Angle + DegToRad(Thick)));
Canvas.Polygon(Points);
end;
// Beispiele ---------------------
function distance(a, b: TPoint): Double;
var
dx, dy: Integer;
begin
dx := a.x - b.x;
dy := a.y - b.y;
result := sqrt(dx * dx + dy * dy);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a, h: Double;
Lang, Breit: Integer;
x1, x2, y1, y2: Integer;
begin
// Beispielsweise: --------------
Canvas.brush.Color := clYellow;
Canvas.Pen.Color := clBlue;
Canvas.Pen.Width := 3;
// Pfeilspitze: -----------------
Lang := 30;
Breit := 20;
// Strich: ----------------------
x1 := 100;
y1 := 150;
x2 := 200;
y2 := 250;
// ------------------------------
Canvas.MoveTo(x1, y1);
Canvas.LineTo(x2, y2);
h := distance(Point(x1, y1), Point(x2, y2)); // Strichlänge
a := (180 / pi) * arccos((x2 - x1) / h) - 90; // Winkelberechnung
DrawArrow(Canvas, x1, y1, a, Breit, Lang); // oben
// auch an der anderen Strichseite
a := a - 180;
DrawArrow(Canvas, x2, y2, a, Breit, Lang);
end;
// waagerecht, Spitze rechts, 90°
procedure TForm1.Button2Click(Sender: TObject);
var
Lang, Breit: Integer;
x1, x2, y1, y2: Integer;
begin
x1 := 100;
y1 := 200;
x2 := 200; // Pfeil dort
y2 := 200;
Canvas.Pen.Width := 2;
Canvas.Pen.Color := clBlack;
Canvas.brush.Color := clBlack;
Canvas.MoveTo(x1, y1);
Canvas.LineTo(x2, y2);
Breit := 20;
Lang := 20;
DrawArrow(Canvas, x2, y2, 90, Breit, Lang);
end;
// senkrecht, Spitze oben, 0°
procedure TForm1.Button3Click(Sender: TObject);
var
Lang, Breit: Integer;
x1, x2, y1, y2: Integer;
begin
x1 := 150;
y1 := 150;
x2 := 150;
y2 := 250;
Canvas.Pen.Width := 5;
Canvas.Pen.Color := clRed;
Canvas.brush.Color := clRed;
Canvas.MoveTo(x1, y1);
Canvas.LineTo(x2, y2);
Breit := 20;
Lang := 20;
DrawArrow(Canvas, x1, y1, 0, Breit, Lang);
end;
|