/***************************************************************** * File: Point2d.java * Synopsis: * Basic Point Class for 2-dimensional geometry * * Visualization Class, Spring 2003 * Author: Chee Yap (yap@cs.nyu.edu) * * $Id: Point2d.java,v 1.2 2003/02/20 16:59:57 yap Exp yap $ *****************************************************************/ class Point2d extends Vector2d { // already in Vector2d // public double x, y; public static Point2d origin = new Point2d(0.0, 0.0); //Constructors public Point2d(double a, double b) { super(POINT_TYPE, 0); x=a; y=b; }; public Point2d() { super(POINT_TYPE, 0); x=0.0; y=0.0; }; public Point2d( Point2d p) { super(POINT_TYPE, 0); x = p.x; y = p.y; }; //Methods public boolean isEqual( Point2d p) { return ((x == p.x) && (y == p.y)); } public boolean isOrigin() { return ((x == 0.0) && (y == 0.0)); } // There is a set in Vector2d already // public Point2d set(double a, double b) { // x = a; y = b; // return this; // } public Point2d set( Point2d p) { x = p.x; y = p.y; return this; } // returns the Euclidean distance between p and this double distance(Point2d p) { double xdiff = p.x - x; double ydiff = p.y - y; return Math.sqrt(xdiff * xdiff + ydiff * ydiff); } // returns distance between this and origin double distance() { return distance(new Point2d()); } Point2d add( Point2d p) { return new Point2d(x + p.x, y + p.y); }; Point2d add( double a, double b) { return new Point2d(x + a, y + b); }; Point2d sub( Point2d p) { return new Point2d(x - p.x, y - p.y); }; Point2d sub( double a, double b) { return new Point2d(x - a, y - b); }; // rotate q by angle of 90 degrees void rotate90( Point2d q) { x = -q.y; y = q.x; }; /* bool operator==( Point2d) ; bool operator!=( Point2d p) {return !operator==(p); } void read(); // reads the x and y coordinates of point p from standard input */ // write point p to output stream, printing message m public int dump(String pre, String post) { System.out.print(pre); System.out.print("(" + x + ", " + y + ")"); System.out.print(post); return(0); }; public int dump(String pre){ return dump(pre, "\n"); }; // dump without message public int dump(){ return dump("", "\n"); }; public static void main (String[] args){ Point2d p = new Point2d(); p.add(4.0, 3.0); p.dump("dumping p(4, 3) :: "); p.sub(1.5, 0.5); p.dump("dumping p(2.5, 2.5) :: "); } }; /* // OTHER: Point2d center(Point2d a, Point2d b); int orientation2d( Point2d a, Point2d b, Point2d c); double area( Point2d a, Point2d b, Point2d c); bool collinear( Point2d a, Point2d b, Point2d c); */