Calculate Distance Between two Point - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Map

Description

Calculate Distance Between two Point

Demo Code

using System;/*  w  w w  .  ja v a 2  s.  co  m*/
class DistanceCalculator
{
   public static void Main()
   {
      Map myMap = new Map();
      Location location1 = new Location();
      Location location2 = new Location();
      location1.setX(10);
      location1.setY(10);
      location2.setX(10);
      location2.setY(20);
      Console.WriteLine("Distance integers: " + myMap.Distance(5,10,5,30));
      Console.WriteLine("Distance doubles: " + myMap.Distance(15.4, 20.6, 15.4, 30.60));
      Console.WriteLine("Distance location objects: " + myMap.Distance(location1, location2));
   }
}
class Map
{
   public double Distance (int x1, int y1, int x2, int y2)
   {
      Console.WriteLine("\nUsing the integer version");
      return Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2));
   }
   public double Distance (double x1, double y1, double x2, double y2)
   {
      Console.WriteLine("\nUsing the double version");
      return Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2));
   }
   public double Distance (Location L1, Location L2)
   {
      Console.WriteLine("\nUsing the Location objects version");
      return Math.Sqrt(Math.Pow(L1.getX() - L2.getX(), 2) +
      Math.Pow(L1.getY() - L2.getY(), 2));
   }
}
class Location
{
   private int x = 0;
   private int y = 0;
   public void setX(int newX)
   {
      x = newX;
   }
   public void setY(int newY)
   {
      y = newY;
   }
   public int getX()
   {
      return x;
   }
   public int getY()
   {
      return y;
   }
}

Result


Related Tutorials