Skip to main content
ICT
Lesson A6 - Libraries and APIs
 
Main   Previous Next
 

D. Point2D.Double page 6 of 12

  1. The Point2D class is useful for storing locations on a two dimensional space. It also contains several methods that can be used to do certain calculations. There are two sub-classes of Point2D, but you will generally only want to use Point2D.Double.

  1. Consider an application where you need to track the locations of two mice in a flat-bottomed box. You could use two Point2D.Double objects to keep track of their locations.

  2. What if we want to be able to tell how far apart the two mice are at any given time? We could use the distance formula from Geometry to calculate the distance. However, if we take a quick look at the Point2D API, we can find this method:
double distance(Point2D pt)
      Returns the distance from this Point2D to a specified Point2D.

A line of code as simple as

double distance = rat1.distance(rat2);

will give us the distance between the two rats. This is much simpler than trying to do all the calculations ourselves. Remember, whenever possible we want to avoid writing code that has already been written. By doing a slight bit of research, we have saved ourselves the hassle of writing and then debugging code.

 

Main   Previous Next
Contact
 © ICT 2006, All Rights Reserved.