Mator

[Pascal] [xEdit] DrawArrowHead procedure for PerkTreeUI

Aug 6th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.54 KB | None | 0 0
  1. //=========================================================================
  2. // Draw arrow head procedure
  3. procedure Form1.DrawArrowHead(n1, n2: TImage);
  4. var
  5.   halfX, halfY, vecX, vecY, sideAX, sideAY, sideBX, sideBY: integer;
  6.   unitX, unitY, orthX, orthY, backX, backY, mag: real;
  7. begin
  8.   // get the vector from one node to the next
  9.   vecX := n2.Left - n1.Left;
  10.   vecY := n2.Top - n1.Top;
  11.   // calculate magnitude of the vector and use it to get a unit vector
  12.   mag := sqrt(vecX*vecX + vecY*vecY);
  13.   unitX := vecX/mag;
  14.   unitY := vecY/mag;
  15.   // get a point halfway between the nodes, and moved up half the length of
  16.   // the arrowhead so the arrowhead is centered
  17.   halfX := ((n1.Left - 5) + (n2.Left - 5))/2 + aHeadLength*unitX*0.5;
  18.   halfY := ((n1.Top - 5) + (n2.Top - 5))/2 + aHeadLength*unitY*0.5;
  19.   // get a vector orthogonal to the unit vector
  20.   orthX := -unitY;
  21.   orthY := unitX;
  22.   // move back from the halfway point a distance equal to the arrow head length
  23.   backX := halfX - aHeadLength*unitX;
  24.   backY := halfY - aHeadLength*unitY;
  25.   // move orthogonally (sideways) a distance equal to the arrow head width
  26.   // we calculate "sideA" and "sideB" for both sides of the arrow head / and \
  27.   sideAX := backX + orthX*aHeadWidth;
  28.   sideAY := backY + orthY*aHeadWidth;
  29.   sideBX := backX - orthX*aHeadWidth;
  30.   sideBY := backY - orthY*aHeadWidth;
  31.   // draw the arrowhead lines
  32.   Image2.Canvas.MoveTo(halfX, halfY);
  33.   Image2.Canvas.LineTo(sideAX, sideAY);
  34.   Image2.Canvas.MoveTo(halfX, halfY);
  35.   Image2.Canvas.LineTo(sideBX, sideBY);
  36. end;
Advertisement
Add Comment
Please, Sign In to add comment