Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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;

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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)

Posted
I realised this as I said in my post, but PlausiblyDamps solution is far more elegant.
Anybody looking for a graduate programmer (Midlands, England)?
Posted
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).

Nothing is as illusive as 'the last bug'.
Posted

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.

Anybody looking for a graduate programmer (Midlands, England)?

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...