Cags Posted November 28, 2005 Posted November 28, 2005 I'm trying to work out the distance between 2 points which is obviously done by doing X1 - X2. As the points can be either way around and I need a positive number i'm currently using an if statement to find out which is biggest. Then taking the smallest from the largest. Is there an easier way of ensuring the number is positive. Mathematically speaking I could just square then square root the number, but this doesn't seem any more elegant than an if statement. My current code... if(e.X > pSelStart.X) iWidth = e.X - pSelStart.X; else iWidth = pSelStart.X - e.X; Quote Anybody looking for a graduate programmer (Midlands, England)?
Administrators PlausiblyDamp Posted November 28, 2005 Administrators Posted November 28, 2005 iWidth = Math.Abs(e.X - pSelStart.X); should do the trick Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Cags Posted November 28, 2005 Author Posted November 28, 2005 Excellent, thats exactly what I was looking for. Quote Anybody looking for a graduate programmer (Midlands, England)?
jo0ls Posted November 29, 2005 Posted November 29, 2005 For the distance between two points you will square the x and y differences - which means it wont matter if there are negatives. dx = x1 - x2 dy = y1 - y2 distance = squareroot (dx*dx + dy*dy) Quote
Cags Posted November 29, 2005 Author Posted November 29, 2005 I realised this as I said in my post, but PlausiblyDamps solution is far more elegant. Quote Anybody looking for a graduate programmer (Midlands, England)?
Wile Posted November 29, 2005 Posted November 29, 2005 I realised this as I said in my post' date=' but PlausiblyDamps solution is far more elegant.[/quote'] The two solutions are not equal. The solution of PlausiblyDamp works great for 2 points on a straight line, but stops working when you're working with x and y coordinates. If you're using 2d coordinates (x and y) you have to use the x^2 + y^2 = z^2 rule. The result will be (a lot) different from abs(x difference) + abs(y difference). Quote Nothing is as illusive as 'the last bug'.
Cags Posted November 29, 2005 Author Posted November 29, 2005 Yes that would give a differen't result, but I have no intention of doing that. As the title stated I was trying to find the distance between 2 X co-ordinates not the difference between 2 points. The reason I'm doing this is because I'm creating a drag select for a program thus needing a rect object of the selected area (similar to the one in explorer). Todo this I need to work out how far the pointer has travelled in either dimension not in total. Otherwise I would have done as you suggested and applied pythagoras's theorum. :) Thus given Point pStart; Point pCurrent; the rectangle will be width = Math.Abs(pStart.X - pCurrent.X) height = Math.Abs(pStart.Y - pCurrent.Y) x = Math.Min(pStart.X, pCurrent.X) y = Math.Min(pStart.Y, pStart.Y) Rectangle rectSelection = new Rectangle(x, y, width, height); This method is far neater than the original rough method I was using to test selection which involved a couple of if else statements. Quote Anybody looking for a graduate programmer (Midlands, England)?
jo0ls Posted November 29, 2005 Posted November 29, 2005 "I'm trying to work out the distance between 2 points which is obviously..." hence the misunderstanding :-\ Quote
IngisKahn Posted November 29, 2005 Posted November 29, 2005 He never said a point in what dimension. Now we all know that you're 2D prejudiced. :p 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.