Calculate Distance Between 2 Points

Submitted by: 
Language: 
Visitors have accessed this post 557 times.

This code allows you to calculate distance between two points.

Download the zip file or copy/paste the code below.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication3
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Console.WriteLine("Please Enter x1 and y1 coordinates:");
  14. double x1 = Convert.ToDouble(Console.ReadLine());
  15. double y1 = Convert.ToDouble(Console.ReadLine());
  16.  
  17. Console.WriteLine("Please enter x2 and y2 coordinates:");
  18. double x2 = Convert.ToDouble(Console.ReadLine());
  19. double y2 = Convert.ToDouble(Console.ReadLine());
  20.  
  21. double result = Distance(x1, x2, y1, y2);
  22. Console.WriteLine("Distance between coordinates {0},{1} and{2},{3} is{4:F}", x1, y1, x2 , y2, result);
  23.  
  24. Console.ReadKey();
  25. }
  26.  
  27. private static double Distance(double x1, double x2, double y1, double y2)
  28. {
  29. double temp1 = Math.Pow((x2 - x1),2);
  30. double temp2 = Math.Pow((y2 - y1),2);
  31. double answer = Math.Sqrt(temp1 + temp2);
  32. return answer;
  33. }
  34. }
  35. }

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

COMMENT About the new calculator app, it seems to be good

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.