Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi iam make an app for 2d automatic drawing by importin data. Not my points are draw, i want to calculate distance between two points and show it automaticaly and the screen.
I would need to move it if necessary for drawing quality view

What I have tried:

double

 dblDistX = Math.Abs(pt1.X - pt2.X);
 double
 dblDistY = Math.Abs(pt1.Y - pt2.Y);
Posted
Updated 8-Feb-19 10:13am

1 solution

Looks like a good start; finding the deltas for the X & Y coordinates.
C#
double dblDistX = Math.Abs(pt1.X - pt2.X);
double dblDistY = Math.Abs(pt1.Y - pt2.Y);
Now I'm guessing that you want the diagonal distance (Z) between these 2 points. Time to break out the HS Geometry book and look up my buddy Pythagoreas and see what he has for theorems.
Pythagoreas wrote:
In a right angled triangle: the square of the hypotenuse is equal to the sum of the squares of the other two sides.

Update 02/14/2019
This is how it would be in handwritten math:
z2 = x2 + y2
z = √(x2 + y2)

My original answer included this code which was in no specific language. From the comments below I was informed that this would work for VB
VB
' VB Solution
double Z = (dblDistX^2 + dblDistY^2)^.5

C# is what the OP is using so here are 2 versions of it; the long long version and the 1 liner
C#
// C# version
// long version
double dblSqrdDistX = Math.Pow(dblDistX,2);
double dblSqrdDistY = Math.Pow(dblDistY,2);
double dblSqrdHypotenuseZ = dblSqrdDistX + dblSqrdDistY;
double Z = Math.Sqrt(dblSqrdHypotenuseZ);

// short version
double Z = Math.Sqrt(Math.Pow(dblDistX,2) + Math.Pow(dblDistY,2));
 
Share this answer
 
v2
Comments
[no name] 11-Feb-19 7:45am    
Worth to mention that Math.Abs is superfluous for calculating the distance. Nevertheless +5.
Member 14126364 12-Feb-19 9:21am    
the problem is not calcultating the distance. I need to show it on my canvas.
Exemple if i draw a rectangle i want he user tu see arrow showing the width or depth of the rectangle
MadMyche 12-Feb-19 9:32am    
The post was titled "Get distance mesure after drawing".
Richard Deeming 12-Feb-19 12:57pm    
NB: ^ is the exponentiation operator in VB; in C#, it's the Xor operator. :)
Member 14126364 13-Feb-19 22:47pm    
Please explain

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900