You can use the java awt.geom
classes for that. Particularly, look at Point2D here
Note, that these classes are designed for graphical interfaces, and use plot points starting from the top left at 0,0
, increasing as the go further right and down. So for x=2, y=3
, this point is three points
down, and two points
right of the top-left of the plot.
You can both use the class in a static way:
Point2D.distance(x1, y1, x2, y2);
But also in an oo way:
Point2D point1 = new Point2D.Double(x1, y1);
point1.distance(x2, y2);
point1.distance(new Point2D.Double(x2, y2));
I mention these libraries not because they're the easiest to use (Math
library is the most straight forward to find the distance between two points), but you're talking about 2D graphical programs. And there's a lot of good stuff in the java geom
libraries aimed at this sort of thing. So if you want to work out whether your point is inside a square (or any other shape for instance), you can do that. Or if you want to work out if one shape intersects with another, or if two lines cross, you can do that as well.