How to Calculate the Distance Between Two Points in Java

How to Calculate the Distance Between Two Points in Java thumbnail
How to Calculate the Distance Between Two Points in Java

Finding the distances between points in either 2D or 3D space is actually a simple operation using the \"Point2d\" and \"Point3d\" classes that are included in the Java API's \"javax.vecmath\" library. You do not even have to haul out your old geometry or algebra textbooks, because the API does all the math work for you.

Things You'll Need

  • Java 1.3.2 or later
Show More

Instructions

    • 1

      Create a \"DistanceFinder\" class and place it in a file named \"DistanceFinder.java.\" Add the following basic class structure:<br />import javax.vecmath.Point2d<br />GO<br />import javax.vecmath.Point3d<br />GO<br /><br />/**<br /> * Provides methods to find the distances between two points.<br /> * @author Kevin Walker<br /> */<br />public class DistanceFinder {<br /><br />}<br />If you use a Java IDE such as Netbeans for your work, this task can be done automatically for you by selecting either the \"New File\" or \"New Class\" option from the \"File\" menu.

    • 2

      Add a method to find the distance between two points in 2D space:<br /> /**<br /> * Finds the distance between two points in two-dimensional space.<br /> * @param p1 The first point.<br /> * @param p2 The second point.<br /> * @return The distance<br /> */<br /> public static double distance(Point2d p1, Point2d p2) {<br /> return p1.distance(p2)<br />GO<br /> }<br />The method begins with a \"JavaDoc\" comment that describes what the method will do, what parameters it will take and what it returns. The method itself is then declared. It is \"public,\" which means it is acceptable for use in programs outside this class. It is \"static,\" meaning that it can be used without first creating a \"DistanceFinder\" object. And it returns the distance using the \"double\" primitive type.

    • 3

      Add a method find the distance between two points in 3D space:<br /> /**<br /> * Finds the distance between two points in three-dimensional space.<br /> * @param p1 The first point.<br /> * @param p2 The second point.<br /> * @return The distance<br /> */<br /> public static double distance(Point3d p1, Point3d p2) {<br /> return p1.distance(p2)<br />GO<br /> }

    • 4

      Add a main method to create a few points and test the application:<br /> /**<br /> * A simple test application.<br /> */<br /> public static void main(String[] args) {<br /><br /> Point2d p1, p2<br />GO<br /><br /> p1 = new Point2d(5.0, 3.5)<br />GO<br /> p2 = new Point2d(12.7, 8.9)<br />GO<br /> <br /> System.out.println(distance(p1,p2))<br />GO<br /><br /> Point3d p3, p4<br />GO<br /> p3 = new Point3d(1.0,5.6,3.2)<br />GO<br /> p4 = new Point3d(1.8,8.9,6.2)<br />GO<br /><br /> System.out.println(distance(p3,p4))<br />GO<br /> <br /> }<br />This simple test application creates four points, two each in 2D and 3D space, and then uses the functions provided to find the distance between them.

Tips & Warnings

  • Don't confuse the Point2d class from the javax.vecmath library with the Point2D class from the java.awt.geom library.

Related Searches:

References

  • Photo Credit Photodisc/Photodisc/Getty Images

Comments

You May Also Like

Related Ads

Featured