# Calculate the Distance between two 2D or 3D points.

Page 1 of 1

## 1 Replies - 8908 Views - Last Post: 20 May 2012 - 05:09 AM

### #1 gbertoli3 Reputation: 41
• Posts: 1,166
• Joined: 23-June 08

# Calculate the Distance between two 2D or 3D points.

Posted 21 September 2009 - 01:43 PM

Description: Use: //Gets the distance between two points on a 2D surface MessageBox.Show(Distance2D(-2, -3, -4, 4).ToString()); //Gets the distance between two points on a 3D surface MessageBox.Show(Distance3D(3, -4, 7, -4, 9, 13).ToString());Gets the distance between two 2D or 3D points.
```        ///
/// Finds the distance between two points on a 2D surface.
///
/// The point on the x-axis of the first point
/// The point on the x-axis of the second point
/// The point on the y-axis of the first point
/// The point on the y-axis of the second point
///
public int Distance2D(int x1, int y1, int x2, int y2)
{
//     ______________________
//d = √ (x2-x1)^2 + (y2-y1)^2
//

//Our end result
int result = 0;
//Take x2-x1, then square it
double part1 = Math.Pow((x2 - x1), 2);
//Take y2-y1, then sqaure it
double part2 = Math.Pow((y2 - y1), 2);
//Add both of the parts together
double underRadical = part1 + part2;
//Get the square root of the parts
//Return our result
return result;
}

///
/// Finds the distance between two points on a 3D surface.
///
/// The point on the x-axis of the first point
/// The point on the x-axis of the second point
/// The point on the y-axis of the first point
/// The point on the y-axis of the second point
/// The point on the z-axis of the first point
/// The point on the z-axis of the second point
///
public int Distance3D(int x1, int y1, int z1, int x2, int y2, int z2 )
{
//     __________________________________
//d = √ (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2
//

//Our end result
int result = 0;
//Take x2-x1, then square it
double part1 = Math.Pow((x2 - x1), 2);
//Take y2-y1, then sqaure it
double part2 = Math.Pow((y2 - y1), 2);
//Take z2-z1, then square it
double part3 = Math.Pow((z2 - z1), 2);
//Add both of the parts together
double underRadical = part1 + part2 + part3;
//Get the square root of the parts
//Return our result
return result;
}
```

Is This A Good Question/Topic? 0

## Replies To: Calculate the Distance between two 2D or 3D points.

### #2 DanielLeone Reputation: 22
• Posts: 177
• Joined: 04-February 12

## Re: Calculate the Distance between two 2D or 3D points.

Posted 20 May 2012 - 05:09 AM

Nice, works perfectly, thanks!

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }