this slowpoke moves

Draw a Arrow

Beispiel 1:
procedure ZeichnePfeil(I: TImage; P1, P2: TPoint);

  function GetDEG(Winkel: Extended): Extended; // gibt den Winkel im Gradmaß an
  begin
    Result := (Winkel * 2 * Pi) / 360;
  end;

  function GetRAD(Winkel: Extended): Extended; // gibt den Winkel im Winkelmaß an
  begin
    Result := (Winkel * 360) / (2 * Pi);
  end;
const
  s    = 10; // Seitenlänge der Pfeilspitze
  Beta = 50; // Spannwinkel der Pfeilspitze
var
  Punkte: array [0..2] of TPoint; // Array für die Punkte der Pfeilspitze
  Alpha, AlphaZ: Extended;        // Winkel zur horizontalen Achse durch P1
begin
  //Linie zeichnen
  I.Canvas.Brush.Color := clBlack;
  I.Canvas.Pen.Style   := psSolid;

  I.Canvas.MoveTo(P1.X, P1.Y);
  I.Canvas.LineTo(P2.X, P2.Y);

  //Pfeilspitze (1.Punkt)
  Punkte[0].X := P2.X;
  Punkte[0].Y := P2.Y;

  //Winkel ermitteln
  Alpha := 0;
  if P2.X = P1.X then
    AlphaZ := 0
  else
    AlphaZ := GetRAD(ArcTan((P2.Y - P1.Y) / (P2.X - P1.X)));

  if (P2.X > P1.X) and (P2.Y = P1.Y) then Alpha := 0          
  else if (P2.X > P1.X) and (P2.Y < P1.Y) then Alpha := 0 - AlphaZ 
  else if (P2.X = P1.X) and (P2.Y < P1.Y) then Alpha := 90          
  else if (P2.X < P1.X) and (P2.Y < P1.Y) then Alpha := 180 - AlphaZ 
  else if (P2.X < P1.X) and (P2.Y = P1.Y) then Alpha := 180          
  else if (P2.X < P1.X) and (P2.Y > P1.Y) then Alpha := 180 - AlphaZ 
  else if (P2.X = P1.X) and (P2.Y > P1.Y) then Alpha := 270          
  else if (P2.X > P1.X) and (P2.Y > P1.Y) then Alpha := 360 - AlphaZ;

  //2.Punkt
  Punkte[1].X := round(P2.X - s * cos(GetDEG(Alpha - Beta div 2)));
  Punkte[1].Y := round(P2.Y + s * sin(GetDEG(Alpha - Beta div 2)));

  //3.Punkt
  Punkte[2].X := round(P2.X - s * cos(GetDEG(Alpha + Beta div 2)));
  Punkte[2].Y := round(P2.Y + s * sin(GetDEG(Alpha + Beta div 2)));

  //Pfeil zeichnen
  I.Canvas.Brush.Color := clBlack;
  I.Canvas.Polygon(Punkte);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
	ZeichnePfeil(Image1, Point(20,20), Point(100,150));
end;
Beispiel 2:
uses Math

var
  Form1: TForm1;
  BeginPoint: TPoint;
  
//

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) - ArcTan((BeginPoint.Y - Y) / (BeginPoint.X - X))
    else
      B := DegToRad(45) - ArcTan((BeginPoint.Y - Y) / (BeginPoint.X - 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 - 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 - Trunc(deltaX), Y + Trunc(deltaY));
      Image1.Canvas.PenPos := Point(X, Y);
      Image1.Canvas.LineTo(X - Trunc(deltaY), Y - 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 - 10, Y + 10);
    end
    else
    begin
      Image1.Canvas.PenPos := Point(X, Y);
      Image1.Canvas.LineTo(X + 10, Y - 10);
      Image1.Canvas.PenPos := Point(X, Y);
      Image1.Canvas.LineTo(X - 10, Y - 10);
    end;
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate