Total members 11889 |It is currently Fri Mar 29, 2024 8:04 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Method to get distance between to points in 2d space:

A public method named distanceTo that accepts a parameter of type Point and returns a double value
representing the distance between the two points (i.e., between the Point object on which the method
was called and the other Point object that was passed as a parameter to the method). Recall that to
compute the distance between two points in 2D space you must:
  • Find the di erence between the two x-coordinates
  • Compute the square of that di erence
  • Find the di erence between the two y-coordinates
  • Compute the square of that di erence
  • Add those two squares together
  • Take the square root of the result
and.... so far I have this
Code:
/**
     * Changes the distance between the two points
     */
    public void distanceTo(double xPosition, double yPosition)
    {
               
    }
   

Im at a lost and don't know where to start any help would be great.




Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

one possible solution would be:

Code:
import java.lang.Math;

public class xPoint {

   /**
    * Makes the class executable
    */
   public static void main(String[] args) {
      xPoint p1 = new xPoint(5, 7);
      xPoint p2 = new xPoint(8, 4);
      System.out.println("Distance = " + p1.distanceTo(p2));
   }

   /**
    * Private member containing the x coordinate
    */
   private double x;

   /**
    * Private member containing the y coordinate
    */
   private double y;
   
   /**
    * Method to compute the distance to another xPoint instance
    * @param xPosition
    * @param yPosition
    */
   public xPoint(double xPosition, double yPosition) {
      x = xPosition;
      y = yPosition;
   }

   /**
    * Method to compute the distance to another xPoint instance.
    * @param other Instance to compute distance to.
    * @return Distance between the current instance of xPoint and other.
    */
   public double distanceTo(xPoint other) {
      return Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2));
   }
}


Executing the program results in this output:

Code:
Distance = 4.242640687119285


Best regards
apeter


Author:
Newbie
User avatar Posts: 4
Have thanks: 1 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Method to Get distance between two points
 Vectorized Matlab function calculate Euclidean distance     -  
 Draw Cloud which Contains a list of points     -  
 Draw Cardinal Spline With Mouse Clicks Points     -  
 Draw Bezier Curve and selecting the points with mouse click     -  
 What is a static method     -  
 Creating a Method in jsp     -  
 What is a native method     -  
 use out object in a method at jsp     -  
 overriding method in php     -  
 What is an abstract method     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com