1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| // Java:
public class Point {
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double x() {
return x;
}
public double y() {
return y;
}
public Point(double x, double y, boolean addToGrid) {
this(x, y);
if (addToGrid)
grid.add(this);
}
public Point() {
this(0.0, 0.0);
}
double distanceToPoint(Point other) {
return distanceBetweenPoints(x, y,
other.x, other.y);
}
private static Grid grid = new Grid();
static double distanceBetweenPoints(
double x1, double y1,
double x2, double y2
) {
double xdist = x1 - x2;
double ydist = y1 - y2;
return Math.sqrt(xdist * xdist + ydist * ydist);
}
} |
Partager