Mega Code Archive

 
Categories / Delphi / Graphic
 

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) - 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;