Geometry Arc Algorithm

北战南征 提交于 2019-12-08 11:44:51

问题


I searched all internet and didn't find any pseudo code that solved this problem, I want to find an Arc between two points, A and B, using 5 arguments:

  1. Start Point
  2. End Point
  3. Radius (Don't know if this is needed)
  4. Angle
  5. Quality

Example:

StartPoint = The green point on the left is the Start Point set on the arguments

EndPoint = The green point on the right is the End Point set on the arguments

Angle = Angle of the Arc(Semi Circle)

Quality = How many red circles to create

I would like to have a pseudo code to solve this problem

Thanks in advance :D


回答1:


Let start point is P0, end point P1, angle Fi. R is not needed

At first find arc center. Get middle of P0-P1 segment.

 M = (P0 + P1) / 2 
 // M.x =  (P0.x + P1.x) / 2 , same for y

And direction vector

 D = (P1 - P0) / 2

Get length of D

 lenD = Math.Hypot(D.x, D.y)  //Vector.Length, sqrt of sum of squares

Get unit vector

 uD = D / lenD

Get (left) perpendicular vector

 (P.x, P.y) = (-uD.y, ud.x)

Now circle center

 if F = Pi then
     C.x = M.x
     C.y = M.y
 else
     C.x = M.x + P.x * Len / Tan(Fi/2)
     C.y = M.y + P.y * Len / Tan(Fi/2)

Vector from center to start point:

CP0.x = P0.x - C.x
CP0.y = P0.y - C.y

Then you can calculate coordinates of N intermediate points at the arc using rotation of vector CP0 around center point

 an = i * Fi / (NSeg + 1);
 X[i] = C.x + CP0.x * Cos(an) - CP0.y * Sin(an)
 Y[i] = C.y + CP0.x * Sin(an) + CP0.y * Cos(an)

Working Delphi code

procedure ArcByStartEndAngle(P0, P1: TPoint; Angle: Double; NSeg: Integer);
var
  i: Integer;
  len, dx, dy, mx, my, px, py, t, cx, cy, p0x, p0y, an: Double;
  xx, yy: Integer;
begin
  mx := (P0.x + P1.x) / 2;
  my := (P0.y + P1.y) / 2;

  dx := (P1.x - P0.x) / 2;
  dy := (P1.y - P0.y) / 2;

  len := Math.Hypot(dx, dy);

  px := -dy / len;
  py := dx / len;

  if Angle = Pi then
    t := 0
  else
    t := len / Math.Tan(Angle / 2);

  cx := mx + px * t;
  cy := my + py * t;

  p0x := P0.x - cx;
  p0y := P0.y - cy;

  for i := 0 to NSeg + 1 do begin

   an := i * Angle / (NSeg + 1);
   xx := Round(cx + p0x * Cos(an) - p0y * Sin(an));
   yy := Round(cy + p0x * Sin(an) + p0y * Cos(an));

   Canvas.Ellipse(xx - 3, yy - 3, xx + 4, yy + 4);
  end;
end;

Result for (Point(100, 0), Point(0, 100), Pi / 2, 8 (Y-axis down at the picture)



来源:https://stackoverflow.com/questions/44877698/geometry-arc-algorithm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!