Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

ok for a simple example, lets say i have two numbers, x=10, y=2.

i want to check if z=5 is between theese ranges.

 

if(x>z && y < z)
{
//z is in the range
}

now this will fail if x is smaller than y, even though z would still be in the range.

 

i have to do this with points. i have a point. i have to check if this point is between 4 other points. before anyone starts, im not asking to find if two lines intersect, i can do that already. my problem is that my algorithm checks if infinite lines intersect, and i have to then check if the intersection point found by my program is within the range of both line segments. as you can see this would lead to quite a large if statement. also, as you can see if the line is sloped funny ( x is before y) then the algorithm wont detect a collision. i tried swapping points but its hard to call if one line is bigger or smaller than another. anyone got any ideas?

Posted (edited)
what does the math.abs function do??
Math.Abs() returns the "absolute value" of the input. In short, all negative numbers beccome positive. Some examples:

 

Abs(-5) = 5

Abs(3) = 3

Abs(0) = 0

Abs(-11) = 11

 

However that equation does not work. For example, 15 is between 10 and 20. So z= 15, x = 10, y = 20 should return True. But f(Math.Abs(x-y)>z) would retun False.

 

Use PlausiblyDamp's equation instead:

 

if((x>z && y < z) || (y >z && x<z))

 

:)

Edited by Mike_R

Posting Guidelines

 

Avatar by Lebb

  • Leaders
Posted

Abs() makes a number positive.

 

To check if a number is in a range (including the boundaries), go for Plauisbly's suggestion, or

if ( x > y) {
 if (x>z && y<z) {
 }
} else {
 if (y>z && x<z) {
 }
}

:)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • *Experts*
Posted

Maybe it's just me, but I like to read the code like I think about it. I'd put z on the left of each comparison, like so: "if (z>x && z<y)"

 

Meaning, I want to compare z to something, not compare x and y to something. In my head I say "If z is greater than x and less than y" so it's nicer to read the code that way.

 

Or even better, refactor to a function:

private bool Between(int compareNumber, int boundA, int boundB)
{
   ....
}
if(Between(z, x, y))
{
   ....
}

 

Maybe not the best names (Between, boundA, etc.), but still... make the code readable. Then the swapping of x and y can be hidden in the function to handle when boundA > boundB or when boundA < boundB.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...