public class Point2{ private int a; private int b; private Point startingPoint; public Point2(int a, int b, Point p){ this.a = a; this.b = b; startingPoint = p; } // Computes the distance from starting point to this public double measureDistance(){//it takes no parameter. return startingPoint.distanceTo(a, b); } /* if you can't edit distanceTo() it gets a little verbose but you must create a Point with Point2 coordinates - remember this example when you will study Inheritance public double measureDistance() { Point endingPoint = new Point(a, b); return startingPoint.distanceTo(endingPoint); } */ }
@Test public void testCreatePoint() { Point p = new Point(2.0, 3.0); assertEquals(2.0, p.x, 0.0); assertEquals(3.0, p.y, 0.0); Point p2 = Point.of(2.0, 3.0); assertEquals(2.0, p2.x, 0.0); assertEquals(3.0, p2.y, 0.0); assertEquals(p, p2); assertEquals(p.hashCode(), p2.hashCode()); Point p3 = Point.of(2, 3); assertEquals(2.0, p2.x, 0.0); assertEquals(3.0, p2.y, 0.0); assertEquals(p, p3); assertEquals(p.hashCode(), p2.hashCode()); }
@Test public void testDistanceTo() { Point p1 = Point.of(10, 10); Point p2 = Point.of(13, 14); double distance = p1.distanceTo(p2); assertEquals(5.0, distance, 0.0); distance = p2.distanceTo(p1); assertEquals(5.0, distance, 0.0); distance = p1.distanceTo(p1); assertEquals(0.0, distance, 0.0); p1 = Point.of(-10, 10); p2 = Point.of(-13, 14); distance = p1.distanceTo(p2); assertEquals(5.0, distance, 0.0); } }
/** * Computes the distance to the given Point. * * @param other - second Point * @throws IllegalArgumentException */ public double distanceTo(Point other) { if (other == null) { throw new IllegalArgumentException("The second Point cannot be null."); } return distance(this.x, this.y, other.x, other.y); }
public Bounds(Point upperLeft, double width, double height) { this.upperLeft = upperLeft; this.lowerRight = upperLeft.translate(width, height); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Bounds other = (Bounds) obj; if (lowerRight == null) { if (other.lowerRight != null) return false; } else if (!lowerRight.equals(other.lowerRight)) return false; if (upperLeft == null) { if (other.upperLeft != null) return false; } else if (!upperLeft.equals(other.upperLeft)) return false; return true; }
@Test public void testSetDockers() { Edge edge = new Edge(); List<Point> dockers = new ArrayList<>(); dockers.add(Point.of(1.0, 2.0)); dockers.add(Point.of(4.0, 5.0)); dockers.add(Point.of(6.0, 7.0)); edge.dockers(dockers); assertEquals(dockers, edge.dockers); dockers.add(Point.of(8.0, 9.0)); assertNotEquals(dockers, edge.dockers); edge.dockers(null); assertNull(edge.dockers); }
/** * Computes the distance to the given Point. * * @param other - second Point * @throws IllegalArgumentException */ public double distanceTo(Point other) { if (other == null) { throw new IllegalArgumentException("The second Point cannot be null."); } return distance(this.x, this.y, other.x, other.y); }
public Bounds(Point upperLeft, double width, double height) { this.upperLeft = upperLeft; this.lowerRight = upperLeft.translate(width, height); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Bounds other = (Bounds) obj; if (lowerRight == null) { if (other.lowerRight != null) return false; } else if (!lowerRight.equals(other.lowerRight)) return false; if (upperLeft == null) { if (other.upperLeft != null) return false; } else if (!upperLeft.equals(other.upperLeft)) return false; return true; }
public static Point of(double x, double y) { return new Point(x, y); }
/** * Validates this Bounds and returns true if the distance between the two points is bigger than 0.0 * and at least one of the x and y values is different than 0.0. */ public boolean isValid() { // upperLeft and lowerRight must exist if (upperLeft == null || lowerRight == null) { return false; } // either of the x values must be different than 0.0 if (upperLeft.x == 0.0 && lowerRight.x == 0.0) { return false; } // either of the y values must be different than 0.0 if (upperLeft.y == 0.0 && lowerRight.y == 0.0) { return false; } // the distance between the two points must be bigger than 0.0 if (upperLeft.distanceTo(lowerRight) == 0.0) { return false; } return true; }
@Test public void testAddEdgeToUninitializedDiagram() { Diagram diagram = new Diagram(); String edgeId = "e1"; String from = "n1"; String to = "n2"; diagram.addEdge(edgeId, from, to, Point.of(1, 2), Point.of(3, 4), Point.of(5, 6)); assertNotNull(diagram.canvas); assertEquals(1l, diagram.version.longValue()); assertEquals(1, diagram.edges.size()); }