fguihen Posted February 15, 2005 Posted February 15, 2005 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? Quote
Administrators PlausiblyDamp Posted February 15, 2005 Administrators Posted February 15, 2005 would the following not work? if((x>z && y < z) || (y >z && x{ } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
twistedm1nd Posted February 16, 2005 Posted February 16, 2005 would this work ? if(Math.Abs(x-y)>z) Quote
fguihen Posted February 16, 2005 Author Posted February 16, 2005 what does the math.abs function do?? Quote
Mike_R Posted February 16, 2005 Posted February 16, 2005 (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 February 16, 2005 by Mike_R Quote Posting Guidelines Avatar by Lebb
Leaders Iceplug Posted February 16, 2005 Leaders Posted February 16, 2005 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) { } } :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
*Experts* Nerseus Posted February 16, 2005 *Experts* Posted February 16, 2005 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 Quote "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
IngisKahn Posted February 16, 2005 Posted February 16, 2005 Yet another way: if (z > Max(x, y) && z < Min(x, y)) Quote "Who is John Galt?"
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.