This is the syntax highlighted version of Point.java.
/*************************************************************************
* Compilation: javac Point.java
* Execution: java Point
*
* Implementation of 2D point using rectangular coordinates.
*
*************************************************************************/
import java.awt.Color;
class Point {
private double x;
private double y;
public Point() { x = Math.random(); y = Math.random(); }
public Point(double x, double y) { this.x = x; this.y = y; }
public double x() { return x; }
public double y() { return y; }
public double r() { return Math.sqrt(x*x + y*y); }
public double theta() { return Math.atan2(y, x); }
public double dist(Point p) {
double dx = this.x() - p.x();
double dy = this.y() - p.y();
return Math.sqrt(dx*dx + dy*dy);
}
public Point avg(Point p) {
return new Point((x + p.x) / 2, (y + p.y) / 2);
}
public String toString() { return "(" + x + ", " + y + ")"; }
// test client
public static void main(String[] args) {
Point p = new Point();
System.out.println("p = " + p);
System.out.println(" x = " + p.x());
System.out.println(" y = " + p.y());
System.out.println(" r = " + p.r());
System.out.println(" theta = " + p.theta());
System.out.println();
Point q = new Point(0.5, 0.5);
System.out.println("q = " + q);
System.out.println("dist(p, q) = " + p.dist(q) + " = " + q.dist(p));
}
}
Last updated: Fri Mar 12 22:15:03 EST 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.