Results 1 to 4 of 4

Thread: Angle between two points

  1. #1
    Join Date
    Jan 2007
    Location
    Tennessee
    Posts
    642
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default Angle between two points

    There is a way to figure out the angle between two points, as if there were a line drawn through them.

    This goes through all the angles, 0 - 360, and figures out about what angle it is, but it's not completely accurate.
    SCAR Code:
    function FindAngle(cx, cy, x, y, Radius: integer): integer;
    var
      i: integer;
    begin
      for i:= 1 to 10 do
      for Result := 0 to 360 do
      if Abs(((Round(Sin(Result * Pi / 180) * Radius) + cx) - x)) < i then
      if Abs(((Round(Cos(Result * Pi / 180) * -Radius) + cy) - y)) < i then
      Exit;
      Result:= -1;
      WriteLn('Error finding angle.');
    end;

    FindAngle(10, 10, 20, 10, 10) returns 88 but it is a 90 degree angle.
    (BTW- if you don't know what to put for the radius, it is the distance between (cx, cy) and (x, y))

    I'm pretty sure you have to use ArcTan2 to do this, but i'm not sure how.

  2. #2
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

    SCAR Code:
    D := (ArcTan2(Points[I].Y - My, Points[I].X - Mx) * i180Pi) + 90;
        if D < 0.0 then
          D := D + 360.0;

    Mx = Cx and My = Cy.

    From WizzyPlugin:

    SCAR Code:
    procedure FilterPointsPie(var Points: TPointArray; const SD, ED, MinR, MaxR: Extended; Mx, My: Integer); stdcall;
    const
      i180Pi = 57.29577951;
    var
       G: TPointArray;
       I, L, T: Integer;
       D, StartD, EndD: Extended;
       cWise: Boolean;
    begin
      T := High(Points);
      if (T < 0) then Exit;
      SetLength(G, T + 1);
      L := 0;
      StartD := SD;
      EndD := ED;
      cWise := StartD > EndD;
      while StartD > 360.0 do
        StartD := StartD - 360.0;
      while EndD > 360.0 do
        EndD := EndD - 360.0;
      while StartD < 0.0 do
        StartD := StartD + 360.0;
      while EndD < 0.0 do
        EndD := EndD + 360.0;
      if StartD > EndD then
        SwapE(StartD, EndD);
      for I := 0 to T do
      begin
        D := fSqrt(Sqr(Points[I].X - Mx) + Sqr(Points[I].Y - My));
        if( D <= MinR) or (D >= MaxR) then
          Continue;
        D := (ArcTan2(Points[I].Y - My, Points[I].X - Mx) * i180Pi) + 90;
        if D < 0.0 then
          D := D + 360.0;
        if (not ((StartD <= D) and (EndD >= D))) xor CWise then
          Continue;
        G[L] := Points[I];
        Inc(L);
      end;
      SetLength(G, L);
      Points := G;
    end;

    Tell me if you run into more problems.



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  3. #3
    Join Date
    Jan 2007
    Location
    Tennessee
    Posts
    642
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    SCAR Code:
    function FindAngle(cx, cy, x, y: integer): Extended;
    begin
      Result:= (ArcTan2(y - cx, x - cy) * (180 / Pi)) + 90;
      if Result < 0.0 then
      Result:= Result + 360.0;
    end;

    Perfect!
    Thanks!

  4. #4
    Join Date
    Jan 2007
    Location
    Tennessee
    Posts
    642
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Edit:
    Ok i figured out what was wrong.
    I switched the x and y coords...

    So this one works perfect.
    SCAR Code:
    function FindAngle(cx, cy, x, y, Radius: integer): integer;
    var
      i: integer;
      Canvas: TCanvas;
    begin
      DisplayDebugImgWindow(500, 500);
      Canvas:= GetDebugCanvas;
      Canvas.Rectangle(0, 0, 500, 500);
      Canvas.Pixels[cx, cy]:= 255;
      Canvas.Pixels[x, y]:= 255;
      Canvas.Pen.Color:= ClBlue;
      Canvas.MoveTo(cx, cy);
      Canvas.LineTo(x, y);
      Result:= Round((ArcTan2(y - cy, x - cx) * (180 / Pi)) + 90);
      if Result < 0 then
      Result:= Result + 360;
      if Result > 360 then
      Result:= Result - 360;
      WriteLn(inttostr(result));
     
      for i:= 1 to 10 do
      for Result := 0 to 360 do
      if Abs(((Round(Sin(Result * Pi / 180) * Radius) + cx) - x)) < i then
      if Abs(((Round(Cos(Result * Pi / 180) * -Radius) + cy) - y)) < i then
      begin
      WriteLn(inttostr(result));
      Exit;
      end;
      Result:= -1;
      WriteLn('Error finding angle.');
    end;


    begin
    x1:= random(500);
    x2:= random(500);
    y1:= random(500);
    y2:= random(500);
    writeln(inttostr(x1));
    writeln(inttostr(y1));
    writeln(inttostr(x2));
    writeln(inttostr(y2));
    writeln('');
    writeln(inttostr(Distance(x1, y1, x2, y2)));
    findangle(x1, y1, x2, y2, Distance(x1, y1, x2, y2));
    end.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. I need to get an array of points...
    By R0b0t1 in forum C/C++ Help and Tutorials
    Replies: 3
    Last Post: 11-05-2007, 03:19 AM
  2. SRL Member's only section and the points.
    By chuch! in forum News and General
    Replies: 50
    Last Post: 06-20-2006, 03:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •