Skip to main content
gamedev.net
🔒 Locked

basic divide by zero solution?

Started by movTheImposter Oct 28, 2004 at 7:26 PM 8 replies 750+ views
Original Post
movTheImposter
movTheImposter
I keep running into this when checking which side of a vertical line a point lies on. I can stumble my way around by checking to see if the X value in line AB is not more than and not less than zero and then act accordingly but isn't there a less awkward solution?
JohnBolton
JohnBolton
Depending on the platform, compiler and settings, dividing by 0 in that case might be ok. When you divide by 0, you will get a value that represents infinity, which is what the slope actually is. You can test for that value and act appropriately in the situations where an infinite slope requires special code.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
hplus0603
hplus0603
I'm assuming 2D, because there's no "side" of lines in 3D.

If you think about 2D as a projection of 3D, the line is really a separation of two half-planes, and you want to know which half-plane the point is on. That's easy; you just get the plane normal, normalize it, dot the point coordinates with the normal, and compare to the plane constant.

Thus, you can turn your line into an equation of the form Ax+By=C (or, alternately, Ax+By+C=0). Then plug the x,y of your coordinates into the same equation, and see if the value comes out greater than, or smaller than, C (in the first form), or 0 (in the second form).

If your function is currently y = Kx+D, then that can be turned into Ax+By+C=0 using fifth-grade algebra.

enum Bool { True, False, FileNotFound };
load_bitmap_file
load_bitmap_file
Quote:
Original post by JohnBolton
Depending on the platform, compiler and settings, dividing by 0 in that case might be ok. When you divide by 0, you will get a value that represents infinity, which is what the slope actually is. You can test for that value and act appropriately in the situations where an infinite slope requires special code.


Vertical lines have an undefined slope, it's not infinity. I put infinity down one time for a math test and got it wrong, by the way. :P
Tron3k
Tron3k
Quote:
Original post by load_bitmap_file
Quote:
Original post by JohnBolton
Depending on the platform, compiler and settings, dividing by 0 in that case might be ok. When you divide by 0, you will get a value that represents infinity, which is what the slope actually is. You can test for that value and act appropriately in the situations where an infinite slope requires special code.


Vertical lines have an undefined slope, it's not infinity. I put infinity down one time for a math test and got it wrong, by the way. :P
What he means is that as the line approaches vertical, the slope approaches infinity. That's a legal mathematical statement - what we use in ordinary language is just short-hand for that.
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
iMalc
iMalc
What you want to do is just apply a little algebra and rearrange the equation so that instead of a divide you do a multiply on the other side of the equals sign.

e.g.code]if (a/b < c/d)
becomes
if (a*d < c*b)The two are equivalent, as long as b and d are positive. If you can't get it then post the exact equation and I'll rearrange it for you. I've solved the same problem before like this but I don't have the code handy.

[edit]Damn I should have read hplus' comment. That's pretty much the same answer.
ToohrVyk
ToohrVyk
iMalc: your solution will not work if there are negative values (for instance, d > 0, b < 0). It does work when you test for equality though.
seanw
seanw
Quote:
Original post by Tron3k
What he means is that as the line approaches vertical, the slope approaches infinity. That's a legal mathematical statement - what we use in ordinary language is just short-hand for that.


When most people say it, they really do mean the line goes 0 distance across the X-axis and infinity distance up the Y-axis to get a vertical line. Infinity isn't a number though, so this is mathematically incorrect. The gradient of a line approaches infinity as the line gets steeper, but the gradient of a vertical line is undefined by definition of a gradient because you cannot divide by zero: gradient = dy/dx = dy/0 = undefined.
iMalc
iMalc
Quote:
Original post by ToohrVyk
iMalc: your solution will not work if there are negative values (for instance, d > 0, b < 0). It does work when you test for equality though.
I know that. You can't have read the bit where I wrote "as long as b and d are positive".
Andrew Russell
Andrew Russell
What JohnBolton ment was that floating point division by zero will give you floating point infinity if your compiler settings are correct. I'm pretty sure this is the default behaviour in MSVC, but I could be wrong.

I'm sure we're all aware that this isn't actually the correct in the world of maths, but it's very conveinient in computing.

In any case, the solution given by hplus0603 was the correct one for the actual problem.


Say the line is defined by AB (two points). P is the point you are testing, and N is the vector of the normal of AB (I don't actually normalize the normal - it's unnecessary for this calculation).

N = Rotated270(B-A)

same as:
Nx = (By-Ay)
Ny = -(Bx-Ax)

then:
q = (P-A) dot N

same as:
q = (Px-Ax)*Nx + (Py-Ay)*Ny

if q is 0, the point is on the line. If it's greater than 0 it's on one side, if it's less than zero, it's on the other side.

This works if you're using integers rather than floats too (unless you happen to overflow).

Cheers,
AR

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.