/** * Get the bounding rectangle * * @return minimum bounding rectangle */ public BoundingRectangle2D getBounds() { if (boundsChanged == true) { boolean first = true; double minX = 0; double maxX = 0; double minY = 0; double maxY = 0; for (Enumeration e = vertices.elements(); e.hasMoreElements(); ) { Vector2D vertex = (Vector2D) e.nextElement(); if (first) { minX = vertex.getX(); maxX = vertex.getX(); minY = vertex.getY(); maxY = vertex.getY(); first = false; } else { minX = Math.min(minX, vertex.getX()); maxX = Math.max(maxX, vertex.getX()); minY = Math.min(minY, vertex.getY()); maxY = Math.max(maxY, vertex.getY()); } } bounds.set(minX, minY, Math.abs(maxX - minX), Math.abs(maxY - minY)); boundsChanged = false; } return bounds; }
private static Shape makeLine(Vector2D base, Vector2D disp) { double x1 = base.getX(); double y1 = base.getY(); double x2 = x1 + disp.getX(); double y2 = y1 + disp.getY(); return new Line2D.Double(x1, y1, x2, y2); }
/** * Rotate the polygon clockwise about a point by an arbritray angle. * * @param x X ordinate of point to rotate about * @param y Y ordinate of point to rotate about * @param angle Angle in radians */ public void rotate(double x, double y, double angle) { double theta = -angle; for (Enumeration e = vertices(); e.hasMoreElements(); ) { Vector2D vertex = (Vector2D) e.nextElement(); // translate to origin double tmpX = vertex.getX() - x; double tmpY = vertex.getY() - y; // rotate double sin = Math.sin(theta); double cos = Math.cos(theta); double newX = tmpX * cos - tmpY * sin; double newY = tmpX * sin + tmpY * cos; // translate back to old location newX += x; newY += y; // set teh point to be where we calculated it should be vertex.setXY(newX, newY); } flagModified(); }
/** Generate the edges of this polygon */ private void generateEdges() { if (haveEdgesChanged == true) { edges.removeAllElements(); Vector2D first = null; Vector2D lastChecked = null; double x = 0; double y = 0; for (Enumeration e = vertices.elements(); e.hasMoreElements(); ) { Vector2D vertex = (Vector2D) e.nextElement(); x += vertex.getX(); y += vertex.getY(); if (lastChecked == null) { first = vertex; lastChecked = first; } else { edges.addElement(new Line2D(lastChecked, vertex)); lastChecked = vertex; } } if (first != null) { edges.addElement(new Line2D(lastChecked, first)); } } haveEdgesChanged = false; }
/** * Check if a point is contained within this polygon * * @param point * @return true if contained, false otherwise */ public boolean contains(Vector2D point) { if (!getBounds().contains(point)) { System.out.println("AAA: " + point); return false; } Line2D verticalRay = new Line2D(point, new Vector2D(point.getX(), Double.POSITIVE_INFINITY)); int intersectionCount = 0; boolean contains = false; generateEdges(); for (Enumeration e = edges.elements(); e.hasMoreElements(); ) { Line2D testLine = (Line2D) e.nextElement(); double minY = Math.min(testLine.getStart().getY(), testLine.getEnd().getY()); double maxY = Math.max(testLine.getStart().getY(), testLine.getEnd().getY()); if (testLine.getGradient() == Double.POSITIVE_INFINITY) { if (point.getX() == testLine.getStart().getX() && point.getY() >= minY && point.getY() <= maxY) { contains = true; } } else { if (verticalRay.intersects(testLine)) { intersectionCount++; } } } return (contains || (intersectionCount % 2 == 1)); }
/** * Get the midpoint of this polygon * * @return Midpoint */ public Vector2D midPoint() { if (isMidPointChanged == true) { double x = 0; double y = 0; for (Enumeration e = vertices.elements(); e.hasMoreElements(); ) { Vector2D vertex = (Vector2D) e.nextElement(); x += vertex.getX(); y += vertex.getY(); } midPoint = new Vector2D(x / vertices.size(), y / vertices.size()); isMidPointChanged = false; return midPoint; } else { return midPoint; } }
/** * Rotate the polygon clockwise about a point by an arbritray angle. * * @param v Point vector to rotate about * @param angle Angle in radians */ public void rotate(Vector2D v, double angle) { rotate(v.getX(), v.getY(), angle); }