// PointShape.java // Holds a point shape. // by Scot Drysdale on 5/17/06 import java.awt.Graphics; import java.awt.Color; import java.awt.geom.Point2D; public class PointShape extends Shape { Point2D p; // Holds the point. Note can be modified public PointShape(Point2D thePoint, Color c) { super(c); p = thePoint; } public PointShape(Point2D thePoint) { p = thePoint; } // Return x-coord. of point public double getX() { return p.getX(); } // Return y-coord. of point public double getY() { return p.getY(); } // Return the point public Point2D getPoint() { return p; } // Point draws itself (as a small cross) public void draw(Graphics page) { int x = (int) p.getX(); int y = (int) p.getY(); page.drawRect(x - 1, y, 3, 1); page.drawRect(x, y - 1, 1, 3); } // Compute the Euclidean distance squared between this point and point (x, y) public double euclideanSq(double x, double y) { return p.distanceSq(x, y); } // Compute the L_1 distance squared between this point and (x, y) public double lOne(double x, double y) { return lOne(p.getX(), p.getY(), x, y); } // Compute the L_1 distance squared between points (x1, y1) and (x2, y2) public static double lOne(double x1, double y1, double x2, double y2) { return Math.abs(x1 - x2) + Math.abs(y1 - y2); } // Can't have an aspect ratio with a point, so make reciprocal of infinity public double aspectRatioRecip(double x, double y) { return 0.0; } // Angle to the endpoints of a point is 0 public double findCosOfAngle(double x, double y) { return 1; // Cosine of 0 degrees } // Translate the point public void move(double deltaX, double deltaY, Point2D clickPoint) { p.setLocation(p.getX() + deltaX, p.getY() + deltaY); } // Rotate the point by 45 degrees, scaling by sqrt(2) public Shape rotate() { return rotate(p.getX(), p.getY()); } public static PointShape rotate(double x, double y) { return new PointShape(new Point2D.Double(x - y, x + y)); } // Returns true if the shape is entirely contained in the rectangle public boolean isContained(Rect r) { return r.containsPoint(this); } }