How to measure diagonal distance points?

I can calculate horizontal and vertical points, but I cannot figure out how to calculate distance using diagonal points. Can anyone help me with this.

here is the code for my horizontal and vertical measurement:

private float ComputeDistance(float point1, float point2) { float sol1 = point1 - point2; float sol2 = (float)Math.Abs(Math.Sqrt(sol1 * sol1)); return sol2; } protected override void OnMouseMove(MouseEventArgs e) { _endPoint.X = eX; _endPoint.Y = eY; if (ComputeDistance(_startPoint.X, _endPoint.X) <= 10) { str = ComputeDistance(_startPoint.Y, _endPoint.Y).ToString(); } else { if (ComputeDistance(_startPoint.Y, _endPoint.Y) <= 10) { str = ComputeDistance(_startPoint.X, _endPoint.X).ToString(); } } } 

Assuming _startPoint is already installed.

alt text

In this image, the diagonal point is obviously incorrect.

+7
c # winforms distance gdi
source share
4 answers

You need to use the Pythagorean theorem.

 d = Math.Sqrt(Math.Pow(end.x - start.x, 2) + Math.Pow(end.y - start.y, 2)) 
+17
source share

I think you are looking for the Euclidean distance formula.

In mathematics, the Euclidean distance or Euclidean metric is the β€œordinary” distance between two points, which can be measured using a ruler and is given by the Pythagorean formula.

+6
source share
+3
source share

Much later ... I would like to add that you can use some of the built-in .NET functions:

 using System.Windows; Point p1 = new Point(x1, y1); Point p2 = new Point(x2, y2); Vector v = p1 - p2; double distance = v.Length; 

or simply:

 static double Distance(double x1, double x2, double y1, double y2) { return (new Point(x1, y1) - new Point(x2, y2)).Length; } 
0
source share

All Articles