Tino APCS

Point2D.Double

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 subclasses of Point2D, but you will generally only want to use Point2D.Double.

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.

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.

Last modified: December 12, 2022

Back to Drawingtool

Dark Mode

Outline