Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

java - Calculate on which side of a line a point is

I need to figure out how to calculate on which side of a line a point is. I'm searching a really fast and simple collision algorithm because I just need to know on what side a object is to define a collision state.

Just like:

if(x > line.x)
    return EnumSide.LEFT;

But the line needs to be diagonally. Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Given a directed line from point p0(x0, y0) to p1(x1, y1), you can use the following condition to decide whether a point p2(x2, y2) is on the left of the line, on the right, or on the same line:

value = (x1 - x0)(y2 - y0) - (x2 - x0)(y1 - y0)

if value > 0, p2 is on the left side of the line.
if value = 0, p2 is on the same line.
if value < 0, p2 is on the right side of the line.

And here's a figure to explain it all:

Which side of line is the point?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...