Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
Calculate which side of a point another point is on

I need to calculate which side of a point another point is on and I've been googling around but I have absolutely no idea what all those math equations mean or how they translate into code or JavaScript more specifically.

I have the following information:

  • Point A: x, y and angle (direction which the point is going to, north clockwise)
  • Point B: x and y

Now how do I know whether point B is on the left or the right side of point A? Answers in JavaScript highly appreciated.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Consider sign of expression (if angle is in degrees, multiply it by Math.PI/180 to get radians)

cross = (B.x - A.x)*Math.sin(angle)-(B.y - A.y)*Math.cos(angle)

Edit: for coordinate system OX north, clockwise it is necessary to exchange cos and sin

cross = (B.x - A.x)*Math.cos(angle)-(B.y - A.y)*Math.sin(angle)

Positive value for right side, negative value for left side (or vice versa depending on your coordinate system orientation)

about 4 years ago · Juan Pablo Isaza Report

0

You have:

  • a point A(A.x, A.y);
  • a moving direction for A given as an angle A.angle;
  • a point B(B.x, B.y).

You want to compare the moving direction of A with the direction of vector AB.

Coordinates of vector AB can be computed with a simple subtraction:

AB.x = B.x - A.x
AB.y = B.y - A.y

You can compute the angle corresponding to direction vector AB using atan2. Conveniently, that function is part of most programming languages' standard math library.

When using atan2, we have to be careful about the convention. In the comments, you specified that you wanted the north-clockwise convention. For other conventions, see Wikipedia: atan2 and conventions. We also have to convert from radians to degrees, which can be done easily with the conversion factor 180 / pi.

AB.angle = atan2(AB.x, AB.y) * 180 / pi
if AB.angle < 0:
    AB.angle = AB.angle + 360

Then all we have to do is check whether AB.angle is in interval [A.angle - 180°, A.angle] (left), or in interval [A.angle, A.angle + 180°] (right), while being careful because all calculations are modulo 180°.

// assuming A.angle > 0 && A.angle < 360
if A.angle > 180:
    if AB.angle > A.angle - 180 && AB.angle < A.angle:
        return "Left"
    else:
        return "Right"
else: // A.angle < 180
    if AB.angle > A.angle && AB.angle < A.angle + 180:
        return "Right"
    else:
        return "Left"
about 4 years ago · Juan Pablo Isaza Report

0

Let's break this up.

  • We've got a directed line L, going through point A in direction a (for angle).
  • We've got a point B.
  • And the question is: Is B left or right of L?

What we need is the angle of the line (against 'north', a.k.a. positive y-axis) that goes through A and B. This allows us to compare those angles:

  • if the difference is in -180°..0° or 180°..360° (-pi..0 or pi..2pi in radians), B is to the left of L,
  • if the difference is in -360°..-180° or 0°..180° (-2pi..-pi or 0..pi in radians), B is to the right of L.

It would be easier to find the right math if your 0°-angle would be 'east, counterclockwise' (as is mathematical convention), but we'll just swap x and y and we're good to go (they're already swapped in the following lines!).

We get that angle (let's call it b) from the points alone, but at first, it'll be in radians (not in degrees):

b_rad = Math.atan((A.x - B.x) / (A.y - B.y));

You did not specify whether the angle is in degrees or radians. Assuming degrees, the result needs to be converted from radians:

b_deg = b_rad * (180 / Math.PI);

Now you only need to take the difference:

delta = a_deg - b_deg;

and use that delta in the comparison I outlined above.

(If I didn't think right, this math gives the opposite results of what is needed -- in this case, you need to swap a_deg and b_deg in the delta calculation)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!