177

I have a set of points. I want to separate them into 2 distinct sets. To do this, I choose two points (a and b) and draw an imaginary line between them. Now I want to have all points that are left from this line in one set and those that are right from this line in the other set.

How can I tell for any given point z whether it is in the left or in the right set? I tried to calculate the angle between a-z-b – angles smaller than 180 are on the right hand side, greater than 180 on the left hand side – but because of the definition of ArcCos, the calculated angles are always smaller than 180°. Is there a formula to calculate angles greater than 180° (or any other formula to chose right or left side)?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Aaginor
  • 4,516
  • 11
  • 51
  • 75
  • How is right or left defined? A) in terms of looking from P1 to P2 or B) left or right of the line in the plane. – phkahler Aug 11 '10 at 18:58
  • 2
    To clarify, to the second part of your question, you can use atan2() instead of acos() to calculate the correct angle. However, using a cross product is the best solution to this as Eric Bainville pointed out. – dionyziz Sep 04 '11 at 12:20
  • Many of the solutions below don't work because they give opposite answers if you interchange points a and b (the points that we are using to define our line). I give a solution in Clojure that sorts the two points lexicographically first before comparing them to the third point. – Purplejacket Feb 23 '15 at 00:28

16 Answers16

298

Try this code which makes use of a cross product. Given a line a--b and point c:

public bool isLeft(Point a, Point b, Point c) {
  return (b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x) > 0;
}

If the formula is equal to 0, the points are colinear (c lines up with a--b).

If the line is horizontal, then this returns true if the point is above the line.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
jethro
  • 18,177
  • 7
  • 46
  • 42
  • 14
    @lzprgmr: No, this is a cross product, equivalently the determinant of a 2D matrix. Consider the 2D matrix defined by rows (a,b) and (c,d). The determinant is ad - bc. The form above is transforming a line represented by 2 points into a one vector, (a,b), and then defining *another* vector using PointA and PointC to get (c, d): (a,b) = (PointB.x - PointA.x, PointB.y - PointA.y) (c,d) = (PointC.x - PointA.x, PointC.y - PointA.y) The determinant is therefore just as its stated in the post. – AndyG Jun 05 '13 at 18:59
  • 6
    I think the confusion over whether this is a cross product or dot product is because it is in two dimensions. It **is** the cross product, in two dimensions: http://mathworld.wolfram.com/CrossProduct.html – brianmearns Sep 04 '13 at 00:53
  • 11
    For what it's worth, this can be slightly simplified to `return (b.x - a.x)*(c.y - a.y) > (b.y - a.y)*(c.x - a.x);`, but the compiler probably optimizes that anyway. – Nicu Stiurca Oct 07 '13 at 20:16
  • @Jayen of course that after swapping the returned value will be changed. If you want to determine if point P is on the left side of a line L, then you have to know where the observer is. – pkacprzak Aug 22 '15 at 14:22
  • 1
    This solution depends on the order of points A,B. The solution provided in this answer math.stackexchange.com/questions/757591/… (which implies calculating the line AB equation) is independent of the points A,B order. – rkachach Mar 22 '18 at 12:17
  • 1
    What about case a = (1; 0), b = (-1; 0), c = (0; 2)? It will return false, though point c to the left of line (above should be considered left of line, bcz it forms left turn). Proof: ((b.X - a.X)*(c.Y - a.Y) - (b.Y - a.Y)*(c.X - a.X)) = ((-1 - 1) * (2 - 0) - (0 - 0) * (0 - 1)) = -2 * 2 = -4 < 0. – metamaker Apr 10 '18 at 16:39
  • Picking a = (0, 0), b = (2, 2), c = (0, 2) (also above line) and substituting to the formula above provides ((b.X - a.X)*(c.Y - a.Y) - (b.Y - a.Y)*(c.X - a.X)) = ((2-0)*(2-0) - (2-0)*(0-0)) = 4 > 0, which is correct. There has to be something with order of variables that this function accepts. – metamaker Apr 10 '18 at 16:47
  • 1
    @metamaker It's not to the left of the line. For A=(1,0) and B=(-1,0) and C=(0,2), point C is to the RIGHT of line AB and to the LEFT of line BA. Order matters. – Phlucious May 29 '18 at 21:01
255

Use the sign of the determinant of vectors (AB,AM), where M(X,Y) is the query point:

position = sign((Bx - Ax) * (Y - Ay) - (By - Ay) * (X - Ax))

It is 0 on the line, and +1 on one side, -1 on the other side.

Regexident
  • 29,441
  • 10
  • 93
  • 100
Eric Bainville
  • 9,738
  • 1
  • 25
  • 27
  • 12
    +1 nice, with one thing to be aware of: rounding error can be a concern when the point is very nearly on the line. Not a problem for *most* uses, but it does bite people from time to time. – Stephen Canon Oct 13 '09 at 14:18
  • Great! This is a nice one, without sinus, arccos and such :) Rounding errors are not a problem here, as I use the separating in an algorithm to calculate the convex hull of the pointset and draw a polyline around them. If a point is just outside the hull, it is no big problem. – Aaginor Oct 13 '09 at 14:53
  • 20
    Should you find yourself in a situation where rounding error on this test is causing you problems, you will want to look up Jon Shewchuk's "Fast Robust Predicates for Computational Geometry". – Stephen Canon Oct 13 '09 at 15:13
  • 17
    For clarification, this is the same as the Z-component of the cross product between the the line (b-a) and the vector to the point from a (m-a). In your favorite vector-class: position = sign((b-a).cross(m-a)[2]) – larsmoa Feb 09 '10 at 22:52
  • Also, this is the [perp dot product](http://mathworld.wolfram.com/PerpDotProduct.html), that is, `dot(perp(A), B))`. The sign is that of the sine of the angle from A to B. – Electro Jul 20 '13 at 11:21
  • 5
    wouldn’t swapping A & B keep the same line, but change the sign of `positions`? – Jayen Jun 10 '14 at 09:07
  • 9
    Yes. A,B defines the orientation, as in "to your left when standing at A and looking at B". – Eric Bainville Jun 10 '14 at 22:27
  • 2
    For a mathematical explanation of how this is achieved, you can see the thread in the following link. http://math.stackexchange.com/questions/757591/how-to-determine-the-side-on-which-a-point-lies – Alma Rahat Dec 07 '15 at 19:25
  • @EricBainville I wonder if there is any constraint of the method? For example, if PA = (10, 50); PB(10, 5) and P (10, 25). The d should be 0 as P is on the line formed by PA and PB, but it's not. Did I do anything wrong? – linzhang.robot Oct 18 '16 at 17:33
  • 4
    In case the `sign` function is new to anyone (as it was to me): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign – Rokit Oct 25 '17 at 09:18
  • 3
    This solution depends on the order of points A,B. The solution provided in this answer https://math.stackexchange.com/questions/757591/how-to-determine-the-side-on-which-a-point-lies (which implies calculating the line AB equation) is independent of the points A,B order. – rkachach Mar 22 '18 at 12:16
  • 4
    For anyone wondering which side is which, a value > 0 means the point is on the left of the line, and a value < 0 means it is on the right of the line when looking in the direction of the line. – Stefan Fabian Jun 01 '20 at 17:48
  • @rkachach In the linked solution one of the two possible normal vectors is used arbitrarily to define which side is plus. – Basti Feb 08 '22 at 15:54
  • any idea how you can create a matrix from two vectors, so that the determinant of the matrix can be calculated? – BenKoshy Oct 27 '22 at 02:41
49

You look at the sign of the determinant of

| x2-x1  x3-x1 |
| y2-y1  y3-y1 |

It will be positive for points on one side, and negative on the other (and zero for points on the line itself).

tzot
  • 92,761
  • 29
  • 141
  • 204
AVB
  • 3,994
  • 22
  • 21
  • 5
    Expanding on this answer, In case people don't know what the cross product looks like. The next visual step is ( (x2-x1) * (y3-y1) ) - ( (y2 - y1) * (x3-x1) ) – Franky Rivera Feb 20 '19 at 20:56
11

The vector (y1 - y2, x2 - x1) is perpendicular to the line, and always pointing right (or always pointing left, if you plane orientation is different from mine).

You can then compute the dot product of that vector and (x3 - x1, y3 - y1) to determine if the point lies on the same side of the line as the perpendicular vector (dot product > 0) or not.

Regexident
  • 29,441
  • 10
  • 93
  • 100
Victor Nicollet
  • 24,361
  • 4
  • 58
  • 89
9

Using the equation of the line ab, get the x-coordinate on the line at the same y-coordinate as the point to be sorted.

  • If point's x > line's x, the point is to the right of the line.
  • If point's x < line's x, the point is to the left of the line.
  • If point's x == line's x, the point is on the line.
mbeckish
  • 10,485
  • 5
  • 30
  • 55
  • This is wrong, because as you can see from Aaginor's comment on the first answer, we don't want to figure out whether the point is on the left or right of the DIRECTED line AB, i.e. if you're standing on A and looking towards B is it on your left or on your right? – dionyziz Sep 04 '11 at 12:18
  • 3
    @dionyziz - Huh? My answer does not assign a "direction" to the line through AB. My answer assumes "left" is the -x direction of the corrdinate system. The accepted answer chose to define a *vector* AB, and define left using cross product. The original question does not specify what is meant by "left". – mbeckish Sep 06 '11 at 19:29
  • 3
    NOTE: If you use this approach (rather than the cross-product one that was approved as answer), be aware of a pitfall as the line approaches horizontal. Math errors increase, and hits infinity if exactly horizontal. The solution is to use whichever axis has the greater delta between the two points. (Or maybe smaller delta .. this is off the top of my head.) – ToolmakerSteve Jul 10 '13 at 05:20
  • 1
    this is totally what i was looking for. i don't want to know if A is above or below B. i just want to know if it's left (negative x direction) of the line! – Jayen Jun 10 '14 at 09:22
  • Also exactly what I was looking for. Very simple and to the point. Thank you! – Jonah Kunz Jun 14 '21 at 21:57
7

I wanted to provide with a solution inspired by physics.

Imagine a force applied along the line and you are measuring the torque of the force about the point. If the torque is positive (counterclockwise) then the point is to the "left" of the line, but if the torque is negative the point is the "right" of the line.

So if the force vector equals the span of the two points defining the line

fx = x_2 - x_1
fy = y_2 - y_1

you test for the side of a point (px,py) based on the sign of the following test

var torque = fx*(py-y_1)-fy*(px-x_1)
if  torque>0  then
     "point on left side"
else if torque <0 then
     "point on right side"  
else
     "point on line"
end if
John Alexiou
  • 28,472
  • 11
  • 77
  • 133
7

I implemented this in java and ran a unit test (source below). None of the above solutions work. This code passes the unit test. If anyone finds a unit test that does not pass, please let me know.

Code: NOTE: nearlyEqual(double,double) returns true if the two numbers are very close.

/*
 * @return integer code for which side of the line ab c is on.  1 means
 * left turn, -1 means right turn.  Returns
 * 0 if all three are on a line
 */
public static int findSide(
        double ax, double ay, 
        double bx, double by,
        double cx, double cy) {
    if (nearlyEqual(bx-ax,0)) { // vertical line
        if (cx < bx) {
            return by > ay ? 1 : -1;
        }
        if (cx > bx) {
            return by > ay ? -1 : 1;
        } 
        return 0;
    }
    if (nearlyEqual(by-ay,0)) { // horizontal line
        if (cy < by) {
            return bx > ax ? -1 : 1;
        }
        if (cy > by) {
            return bx > ax ? 1 : -1;
        } 
        return 0;
    }
    double slope = (by - ay) / (bx - ax);
    double yIntercept = ay - ax * slope;
    double cSolution = (slope*cx) + yIntercept;
    if (slope != 0) {
        if (cy > cSolution) {
            return bx > ax ? 1 : -1;
        }
        if (cy < cSolution) {
            return bx > ax ? -1 : 1;
        }
        return 0;
    }
    return 0;
}

Here's the unit test:

@Test public void testFindSide() {
    assertTrue("1", 1 == Utility.findSide(1, 0, 0, 0, -1, -1));
    assertTrue("1.1", 1 == Utility.findSide(25, 0, 0, 0, -1, -14));
    assertTrue("1.2", 1 == Utility.findSide(25, 20, 0, 20, -1, 6));
    assertTrue("1.3", 1 == Utility.findSide(24, 20, -1, 20, -2, 6));

    assertTrue("-1", -1 == Utility.findSide(1, 0, 0, 0, 1, 1));
    assertTrue("-1.1", -1 == Utility.findSide(12, 0, 0, 0, 2, 1));
    assertTrue("-1.2", -1 == Utility.findSide(-25, 0, 0, 0, -1, -14));
    assertTrue("-1.3", -1 == Utility.findSide(1, 0.5, 0, 0, 1, 1));

    assertTrue("2.1", -1 == Utility.findSide(0,5, 1,10, 10,20));
    assertTrue("2.2", 1 == Utility.findSide(0,9.1, 1,10, 10,20));
    assertTrue("2.3", -1 == Utility.findSide(0,5, 1,10, 20,10));
    assertTrue("2.4", -1 == Utility.findSide(0,9.1, 1,10, 20,10));

    assertTrue("vertical 1", 1 == Utility.findSide(1,1, 1,10, 0,0));
    assertTrue("vertical 2", -1 == Utility.findSide(1,10, 1,1, 0,0));
    assertTrue("vertical 3", -1 == Utility.findSide(1,1, 1,10, 5,0));
    assertTrue("vertical 3", 1 == Utility.findSide(1,10, 1,1, 5,0));

    assertTrue("horizontal 1", 1 == Utility.findSide(1,-1, 10,-1, 0,0));
    assertTrue("horizontal 2", -1 == Utility.findSide(10,-1, 1,-1, 0,0));
    assertTrue("horizontal 3", -1 == Utility.findSide(1,-1, 10,-1, 0,-9));
    assertTrue("horizontal 4", 1 == Utility.findSide(10,-1, 1,-1, 0,-9));

    assertTrue("positive slope 1", 1 == Utility.findSide(0,0, 10,10, 1,2));
    assertTrue("positive slope 2", -1 == Utility.findSide(10,10, 0,0, 1,2));
    assertTrue("positive slope 3", -1 == Utility.findSide(0,0, 10,10, 1,0));
    assertTrue("positive slope 4", 1 == Utility.findSide(10,10, 0,0, 1,0));

    assertTrue("negative slope 1", -1 == Utility.findSide(0,0, -10,10, 1,2));
    assertTrue("negative slope 2", -1 == Utility.findSide(0,0, -10,10, 1,2));
    assertTrue("negative slope 3", 1 == Utility.findSide(0,0, -10,10, -1,-2));
    assertTrue("negative slope 4", -1 == Utility.findSide(-10,10, 0,0, -1,-2));

    assertTrue("0", 0 == Utility.findSide(1, 0, 0, 0, -1, 0));
    assertTrue("1", 0 == Utility.findSide(0,0, 0, 0, 0, 0));
    assertTrue("2", 0 == Utility.findSide(0,0, 0,1, 0,2));
    assertTrue("3", 0 == Utility.findSide(0,0, 2,0, 1,0));
    assertTrue("4", 0 == Utility.findSide(1, -2, 0, 0, -1, 2));
}
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Al Globus
  • 95
  • 1
  • 1
5

First check if you have a vertical line:

if (x2-x1) == 0
  if x3 < x2
     it's on the left
  if x3 > x2
     it's on the right
  else
     it's on the line

Then, calculate the slope: m = (y2-y1)/(x2-x1)

Then, create an equation of the line using point slope form: y - y1 = m*(x-x1) + y1. For the sake of my explanation, simplify it to slope-intercept form (not necessary in your algorithm): y = mx+b.

Now plug in (x3, y3) for x and y. Here is some pseudocode detailing what should happen:

if m > 0
  if y3 > m*x3 + b
    it's on the left
  else if y3 < m*x3 + b
    it's on the right
  else
    it's on the line
else if m < 0
  if y3 < m*x3 + b
    it's on the left
  if y3 > m*x3+b
    it's on the right
  else
    it's on the line
else
  horizontal line; up to you what you do
maksim
  • 806
  • 2
  • 9
  • 16
  • 3
    Fail: Slope calculation invalid for vertical lines. Endless if/else stuff. Not sure if that's what the OP meant by left/right - if so looking at it rotated 90 degrees would cut this code in half since "above" would be right or left. – phkahler Aug 11 '10 at 18:57
  • 1
    This answer has several problems. Vertical lines cause a divide by zero. Worse, it fails because it does not worry about whether the slope of the line is positive or negative. –  Aug 12 '10 at 03:36
  • 2
    @phkahler, fixed the vertical line issue. Definitely not a failure for forgetting one test case but thanks for the kind words. "Endless if/else" is to explain the mathematical theory; nothing in OP's question mentions programming. @woodchips, fixed the vertical line issue. The slope is the variable m; I do check when it is positive or negative. – maksim Aug 12 '10 at 22:46
3

Assuming the points are (Ax,Ay) (Bx,By) and (Cx,Cy), you need to compute:

(Bx - Ax) * (Cy - Ay) - (By - Ay) * (Cx - Ax)

This will equal zero if the point C is on the line formed by points A and B, and will have a different sign depending on the side. Which side this is depends on the orientation of your (x,y) coordinates, but you can plug test values for A,B and C into this formula to determine whether negative values are to the left or to the right.

1

@AVB's answer in ruby

det = Matrix[
  [(x2 - x1), (x3 - x1)],
  [(y2 - y1), (y3 - y1)]
].determinant

If det is positive its above, if negative its below. If 0, its on the line.

boulder_ruby
  • 38,457
  • 9
  • 79
  • 100
1

Here's a version, again using the cross product logic, written in Clojure.

(defn is-left? [line point]
  (let [[[x1 y1] [x2 y2]] (sort line)
        [x-pt y-pt] point]
    (> (* (- x2 x1) (- y-pt y1)) (* (- y2 y1) (- x-pt x1)))))

Example usage:

(is-left? [[-3 -1] [3 1]] [0 10])
true

Which is to say that the point (0, 10) is to the left of the line determined by (-3, -1) and (3, 1).

NOTE: This implementation solves a problem that none of the others (so far) does! Order matters when giving the points that determine the line. I.e., it's a "directed line", in a certain sense. So with the above code, this invocation also produces the result of true:

(is-left? [[3 1] [-3 -1]] [0 10])
true

That's because of this snippet of code:

(sort line)

Finally, as with the other cross product based solutions, this solution returns a boolean, and does not give a third result for collinearity. But it will give a result that makes sense, e.g.:

(is-left? [[1 1] [3 1]] [10 1])
false
Purplejacket
  • 1,808
  • 2
  • 25
  • 43
1

basically, I think that there is a solution which is much easier and straight forward, for any given polygon, lets say consist of four vertices(p1,p2,p3,p4), find the two extreme opposite vertices in the polygon, in another words, find the for example the most top left vertex (lets say p1) and the opposite vertex which is located at most bottom right (lets say ). Hence, given your testing point C(x,y), now you have to make double check between C and p1 and C and p4:

if cx > p1x AND cy > p1y ==> means that C is lower and to right of p1 next if cx < p2x AND cy < p2y ==> means that C is upper and to left of p4

conclusion, C is inside the rectangle.

Thanks :)

Mohamed
  • 11
  • 1
  • 1
    (1) Answers a different question than was asked? Sounds like "bounding box" test, when a rectangle is aligned with both axes. (2) In more detail: makes assumption about the possible relationships between 4 points. For example, take a rectangle, and rotate it 45 degrees, so that you have a diamond. There is no such thing as a "top-left point" in that diamond. The leftmost point is neither topmost or bottom most. And of course, 4 points can form even stranger shapes. For example, 3 points could be far off in one direction, and the 4th point in another direction. Keep trying! – ToolmakerSteve Jul 10 '13 at 05:28
1

Issues with the existing solution:

While I found Eric Bainville's answer to be correct, I found it entirely inadequate to comprehend:

  • How can two vectors have a determinant? I thought that applied to matrices?
  • What is sign?
  • How do I convert two vectors into a matrix?

position = sign((Bx - Ax) * (Y - Ay) - (By - Ay) * (X - Ax))

  • What is Bx?
  • What is Y? Isn't Y meant to be a Vector, rather than a scalar?
  • Why is the solution correct - what is the reasoning behind it?

Moreover, my use case involved complex curves rather than a simple line, hence it requires a little re-jigging:

Reconstituted Answer

Point a = new Point3d(ax, ay, az); // point on line
Point b = new Point3d(bx, by, bz); // point on line

If you want to see whether your points are above/below a curve, then you would need to get the first derivative of the particular curve you are interested in - also known as the tangent to the point on the curve. If you can do so, then you can highlight your points of interest. Of course, if your curve is a line, then you just need the point of interest without the tangent. The tangent IS the line.

Vector3d lineVector = curve.GetFirstDerivative(a); // where "a" is a point on the curve. You may derive point b with a simple displacement calculation:

Point3d b = new Point3d(a.X, a.Y, a.Z).TransformBy(
                 Matrix3d.Displacement(curve.GetFirstDerivative(a))
                );

Point m = new Point3d(mx, my, mz) // the point you are interested in.

The Solution:

return (b.X - a.X) * (m.Y - a.Y) - (b.Y - a.Y) * (m.X - a.X) < 0; // the answer

Works for me! See the proof in the photo above. Green bricks satisfy the condition, but the bricks outside were filtered out! In my use case - I only want the bricks that are touching the circle.

All green items are within the curve

Theory behind the answer

I will return to explain this. Someday. Somehow...

BenKoshy
  • 33,477
  • 14
  • 111
  • 80
  • 1
    If you can write this answer, you can write a *good* question. I can think of 100 ways to handle changing environments. Groceries and airlines change prices but don't charge you a different price at checkout than the one on the sticker/search results. Perhaps you can include all changing parameters in a calculation. Perhaps you can restrict changes to a schedule. Perhaps you can use a double lock. – Panagiotis Kanavos Oct 31 '22 at 08:48
  • @PanagiotisKanavos hey appreciate the comment. I thought it a fairly straightforward question on loops etc. without the need of locks / environment and was curious how others would handle it, and threw up the first method name that came to my head - some funny / some harmless banter -- big mistake. I will review some first year control / flow tutorials and see how i can simplify it. – BenKoshy Oct 31 '22 at 08:58
0

An alternative way of getting a feel of solutions provided by netters is to understand a little geometry implications.

Let pqr=[P,Q,R] are points that forms a plane that is divided into 2 sides by line [P,R]. We are to find out if two points on pqr plane, A,B, are on the same side.

Any point T on pqr plane can be represented with 2 vectors: v = P-Q and u = R-Q, as:

T' = T-Q = i * v + j * u

Now the geometry implications:

  1. i+j =1: T on pr line
  2. i+j <1: T on Sq
  3. i+j >1: T on Snq
  4. i+j =0: T = Q
  5. i+j <0: T on Sq and beyond Q.

i+j: <0 0 <1 =1 >1 ---------Q------[PR]--------- <== this is PQR plane ^ pr line

In general,

  • i+j is a measure of how far T is away from Q or line [P,R], and
  • the sign of i+j-1 implicates T's sideness.

The other geometry significances of i and j (not related to this solution) are:

  • i,j are the scalars for T in a new coordinate system where v,u are the new axes and Q is the new origin;
  • i, j can be seen as pulling force for P,R, respectively. The larger i, the farther T is away from R (larger pull from P).

The value of i,j can be obtained by solving the equations:

i*vx + j*ux = T'x
i*vy + j*uy = T'y
i*vz + j*uz = T'z

So we are given 2 points, A,B on the plane:

A = a1 * v + a2 * u B = b1 * v + b2 * u

If A,B are on the same side, this will be true:

sign(a1+a2-1) = sign(b1+b2-1)

Note that this applies also to the question: Are A,B in the same side of plane [P,Q,R], in which:

T = i * P + j * Q + k * R

and i+j+k=1 implies that T is on the plane [P,Q,R] and the sign of i+j+k-1 implies its sideness. From this we have:

A = a1 * P + a2 * Q + a3 * R B = b1 * P + b2 * Q + b3 * R

and A,B are on the same side of plane [P,Q,R] if

sign(a1+a2+a3-1) = sign(b1+b2+b3-1)

runsun
  • 424
  • 5
  • 8
0

equation of line is y-y1 = m(x-x1)

here m is y2-y1 / x2-x1

now put m in equation and put condition on y < m(x-x1) + y1 then it is left side point

eg.

for i in rows:

  for j in cols:

    if j>m(i-a)+b:

      image[i][j]=0
DevilWhite
  • 51
  • 6
0

A(x1,y1) B(x2,y2) a line segment with length L=sqrt( (y2-y1)^2 + (x2-x1)^2 )

and a point M(x,y)

making a transformation of coordinates in order to be the point A the new start and B a point of the new X axis

we have the new coordinates of the point M

which are newX = ((x-x1)(x2-x1)+(y-y1)(y2-y1)) / L
from (x-x1)*cos(t)+(y-y1)*sin(t) where cos(t)=(x2-x1)/L, sin(t)=(y2-y1)/L

newY = ((y-y1)(x2-x1)-(x-x1)(y2-y1)) / L
from (y-y1)*cos(t)-(x-x1)*sin(t)

because "left" is the side of axis X where the Y is positive, if the newY (which is the distance of M from AB) is positive, then it is on the left side of AB (the new X axis) You may omit the division by L (allways positive), if you only want the sign

P.Tsiros
  • 11
  • 2