Find leftmost point in 2D

Post your questions, suggestions and experiences regarding to Image manipulation, 3d modeling and level editing for the Irrlicht engine here.
Post Reply
terier
Posts: 34
Joined: Sun Oct 05, 2008 4:46 pm

Find leftmost point in 2D

Post by terier »

Assume we have a line, defined by two points (A and B), and a set of points we have to test. Now we have to find the point where the angle PHI (see image) is the largest we can find. If we mirror a point over the line, the test should yield different results, as we want only negative-oriented points.

I guess the algorithm could first test if orientation is negative

Code: Select all

(ax-cx)*(by-cy)-(bx-cx)*(ay-cy) > 0
and then calculate the angle for all suitable points using

Code: Select all

phi = arccos(A.dot(B) / (A.length() * B.length())
but since this is computationally intense if there are many points to test, I wonder if there's any other way..

Image
nespa
Posts: 167
Joined: Wed Feb 24, 2010 12:02 pm

Post by nespa »

see getHorizontalAngle():

Code: Select all

      vector3df B,E,F,result1,result2;
      f32 PHI1,PHI2,PHI;

      result1 =E-B;
      result1 = result1.getHorizontalAngle();
      PHI1=result1.Y;

      result2 =F-B;
      result2 = result2.getHorizontalAngle();
      PHI2=result2.Y;

      PHI=PHI1-PHI2;
                
                if (PHI>180)
                 {
                     PHI=-(360-PHI)
                  };
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Not sure if it's faster. But I suppose another way of getting the result could be:

First normalize all lines. Next add AB to the secondary line vectors (normalized BC, BD, etc). For the resulting vector get the length (or faster, the square-length). If the resulting vector is left of AB you'll want the shortest length, if it's right of AB you'll want the largest length. Figuring out if it's left or right can be done similar - you add the normalized secondary vectors to the perpendicular vector of AB (AB rotated 90° to the right), and if the length of adding those increases you are on the right, when it decreases on the left.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
terier
Posts: 34
Joined: Sun Oct 05, 2008 4:46 pm

Post by terier »

Thanks for replies, guys :)
@nespa getHorizontalAngle() includes a sqrt calculation and 2 arctan calculations, so I don't think that would be a good choice. Thanks for working on my problem though ;)
@CuteAlien I will definitely try this one, not sure what impact the normalizations will have on speed, but worth experimenting a bit.
Post Reply