• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
GuyWithBeard

Determine which side of a line a point is

14 posts in this topic

Hi! I need to check whether a point (given as x,y) is above or below a line (given as two points). I found a few solutions on the net, but they all seemed kind of overkill for this, or they wanted the data in another format. I am working in JavaME, and this is all purely 2D. How would you do this? Thanks!
0

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
You can determine which side of a line a point is on by converting the line to hyperplane form (implicitly or explicitly) and then computing the perpendicular (pseudo)distance from the point to the hyperplane.

Here's an example implementation (pseudocode, not compiled or tested):
int side(vector2 p1, vector2 p2, vector2 p)
{
vector2 diff = p2 - p1;
vector2 perp(-diff.y, diff.x);
float d = dot(p - p1, perp);
return sign(d);
}
1

Share this post


Link to post
Share on other sites
Quote:
I used the first technique.
Just for the record, the techniques are exactly the same, just written differently. Here's how to get from my version to ToohrVyk's version:
diff.x = Bx - Ax
diff.y = By - Ay
perp.x = Ay - By
perp.y = Bx - Ax

dot(p - p1, perp) =
(Cx - Ax) * (Ay - By) + (Cy - Ay) * (Bx - Ax) =
(Bx - Ax) * (Cy - Ay) - (By - Ay) * (Cx - Ax)
1

Share this post


Link to post
Share on other sites
this is a straight approach, with simple geometry

a rectangular R=(Bx-Ax)*(By-Ay)

two rectangulars

R1=(Bx-Ax)*(Cy-Ay)

R2=(Bx-Cx)*(By-Ay)

R-R1-R2 = (Bx - Ax) * (By - Cy) - (By - Ay) * (Bx - Cx) (1)

instead of previous (Bx - Ax) * (Cy - Ay) - (By - Ay) * (Cx - Ax)

if (1) = 0 "on"
if (1) < 0 "left"
if (1) > 0 "right"

no corrections needed
1

Share this post


Link to post
Share on other sites
Quote:
Original post by TSIROS
this is a straight approach, with simple geometry

a rectangular R=(Bx-Ax)*(By-Ay)

two rectangulars

R1=(Bx-Ax)*(Cy-Ay)

R2=(Bx-Cx)*(By-Ay)

R-R1-R2 = (Bx - Ax) * (By - Cy) - (By - Ay) * (Bx - Cx) (1)

instead of previous (Bx - Ax) * (Cy - Ay) - (By - Ay) * (Cx - Ax)

if (1) = 0 "on"
if (1) < 0 "left"
if (1) > 0 "right"

no corrections needed
Wait...how is that different than what ToohrVyk and I posted? (Aside from being arranged differently, that is.)

Also, what do you mean by 'no corrections needed'?
0

Share this post


Link to post
Share on other sites
i am sorry

no corrections needed

there is a previous post ....


differently arranged

you use dot product , i use simple geometry, there is a difference.
0

Share this post


Link to post
Share on other sites
Actually 'simple geometry' is much harder than algebra to do right.

as soon as any new geometric math is invented the first thing mathematicians do is try to find a way to talk about it in algebraic terms. cause its just much easier.
0

Share this post


Link to post
Share on other sites
Quote:
you use dot product , i use simple geometry, there is a difference.
Your method performs a dot product as well.

In any case, the only difference I can see between your method and the other proposed solutions is a conceptual one (in other words, it's just an alternate way of expressing the same solution). Or am I missing something?
0

Share this post


Link to post
Share on other sites
a simple problem.

let a line (L) defined by two points
(Ax,Ay) and (Bx,By)

and two vertical lines on A and B

and a point (Mx,My).

when the point M is between the vertical lines?
0

Share this post


Link to post
Share on other sites
Hello!
Sorry for reviving and old thread. I used the method described here and it works prefectly, but i am trying to undestand the use of dot product here. I have defined my lines with a point and a direction, so half of the math is already done in before hand. My case is as is:

[code]
Vector2f myFirstPoint = new Vector2f(100,100);
Vector2f myDirection = new Vector2f(1,0);

public boolean inside(Vector2f aPosition) {
final Vector2f lineToPosition = new Vector2f(aPosition);
lineToPosition.sub(myFirstPoint);

float dotProduct = (myDirection.x) * (lineToPosition.y) - (myDirection.y) * (lineToPosition.x);
if (dotProduct > 0) {
return false;
}
return true;
}[/code]

As you can see the only differance here from the original post by ToohrVyk is that i have (Bx - Ax) and (By - Ay) caluclated before hand in the form of myDirection. (Cy - Ay) and (Cx - Ax)are calculated with the sub() method which stands for substact. But the mathematical definition of dot product isn't as the form used here. According to Wikipedia:

[img]http://upload.wikimedia.org/math/c/3/2/c329bf86e747d74f55ed2e17c36fd83f.png[/img]

According to this what the formula here calculates is not a dot product. I would really appriciate if someone helped me to undestand how dot product is used here, perhaps how the forumla is derived from it?

Thanks everyone who already contributed here for making my life easier :)
0

Share this post


Link to post
Share on other sites
It's called the perp-dot (perpendicular-dot) product; which is the exterior product in 2 dimensions. It has many properties which the cross product has in 3D; for instance | x perpdot y | = |x||y|sin theta for instance (like in 3D | x cross y | = |x||y|sin theta.

in 2D each vector (vx ; vy) has 2 perpendicular vectors (-vy ; vx), (vy ; -vx) for which we normally choose (-vy ; vx) to be the principal perpendicular, and then the perp-dot product between vectors u,v is perp(u) dot v = u.x * v.y - u.y * v.x
2

Share this post


Link to post
Share on other sites
It is the dot product between the vector perpendicular to the direction and the difference between the points. If (x, y) is a vector, (-y, x) is perpendicular to it (it's the vector rotated by 90°). It works because the projection of one vector v along the direction of another vector d is defined to be (dot(d, v)/dot(d, d))d, but since we are only interested if the projection of the difference vector has the same direction of the perpendicular vector we have chosen, it is enough to determine the sign of the dot product between these two vectors.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0