/* (non-Javadoc) * @see math.geom2d.circulinear.CirculinearCurve2D#transform(math.geom2d.transform.CircleInversion2D) */ public CircleLine2D transform(CircleInversion2D inv) { // Extract inversion parameters Point2D center = inv.center(); Point2D c1 = this.center(); // If circles are concentric, creates directly the new circle if (center.distance(c1) < Shape2D.ACCURACY) { double r0 = inv.radius(); double r2 = r0 * r0 / this.r; return new Circle2D(center, r2, this.direct); } // line joining centers of the two circles StraightLine2D centersLine = new StraightLine2D(center, c1); // get the two intersection points with the line joining the circle centers Collection<Point2D> points = this.intersections(centersLine); if (points.size() < 2) { throw new RuntimeException( "Intersection of circle with line through center has less than 2 points"); } Iterator<Point2D> iter = points.iterator(); Point2D p1 = iter.next(); Point2D p2 = iter.next(); // If the circle contains the inversion center, it transforms into a // straight line if (this.distance(center) < Shape2D.ACCURACY) { // choose the intersection point that is not the center double dist1 = center.distance(p1); double dist2 = center.distance(p2); Point2D p0 = dist1 < dist2 ? p2 : p1; // transform the point, and return the perpendicular p0 = p0.transform(inv); return StraightLine2D.createPerpendicular(centersLine, p0); } // For regular cases, the circle transforms into an other circle // transform the two extreme points of the circle, // resulting in a diameter of the new circle p1 = p1.transform(inv); p2 = p2.transform(inv); // compute center and diameter of transformed circle double diam = p1.distance(p2); c1 = Point2D.midPoint(p1, p2); // create the transformed circle boolean direct = !this.isDirect() ^ this.isInside(inv.center()); return new Circle2D(c1, diam / 2, direct); }
/** * Computes the radical axis of the two circles. * * @since 0.11.1 * @return the radical axis of the two circles. * @throws IllegalArgumentException if the two circles have same center */ public static StraightLine2D radicalAxis(Circle2D circle1, Circle2D circle2) { // extract center and radius of each circle double r1 = circle1.radius(); double r2 = circle2.radius(); Point2D p1 = circle1.center(); Point2D p2 = circle2.center(); // compute horizontal angle of joining line double angle = Angle2D.horizontalAngle(p1, p2); // distance between centers double dist = p1.distance(p2); if (dist < Shape2D.ACCURACY) { throw new IllegalArgumentException("Input circles must have distinct centers"); } // position of the radical axis on the joining line double d = (dist * dist + r1 * r1 - r2 * r2) * .5 / dist; // pre-compute trigonometric functions double cot = Math.cos(angle); double sit = Math.sin(angle); // compute parameters of the line double x0 = p1.x() + d * cot; double y0 = p1.y() + d * sit; double dx = -sit; double dy = cot; // update state of current line return new StraightLine2D(x0, y0, dx, dy); }
/** @deprecated replaced by circlesIntersections(Circle2D, Circle2D) (0.11.1) */ @Deprecated public static Collection<Point2D> getIntersections(Circle2D circle1, Circle2D circle2) { ArrayList<Point2D> intersections = new ArrayList<Point2D>(2); // extract center and radius of each circle Point2D center1 = circle1.center(); Point2D center2 = circle2.center(); double r1 = circle1.radius(); double r2 = circle2.radius(); double d = Point2D.distance(center1, center2); // case of no intersection if (d < abs(r1 - r2) || d > (r1 + r2)) return intersections; // Angle of line from center1 to center2 double angle = Angle2D.horizontalAngle(center1, center2); // position of intermediate point double d1 = d / 2 + (r1 * r1 - r2 * r2) / (2 * d); Point2D tmp = Point2D.createPolar(center1, d1, angle); // Add the 2 intersection points double h = sqrt(r1 * r1 - d1 * d1); intersections.add(Point2D.createPolar(tmp, h, angle + PI / 2)); intersections.add(Point2D.createPolar(tmp, h, angle - PI / 2)); return intersections; }
public Polygon scalePolygon(Polygon polygon) { int xs[] = new int[polygon.npoints]; int ys[] = new int[polygon.npoints]; math.geom2d.Point2D p; math.geom2d.Point2D p1; int sumX = 0; int sumY = 0; for (int i = 0; i < polygon.npoints; i++) { p = new math.geom2d.Point2D(polygon.xpoints[i], polygon.ypoints[i]); p1 = p.scale(0.5); sumX += p1.getX(); sumY += p1.getY(); xs[i] = (int) p1.getX(); ys[i] = (int) p1.getY(); p.clone(); } Polygon poly = new Polygon(xs, ys, polygon.npoints); poly.translate(sumX / polygon.npoints, sumY / polygon.npoints); Polygon scalePolygon = new Polygon(); for (int i = 0; i < poly.npoints; i++) { p = new math.geom2d.Point2D(poly.xpoints[i], poly.ypoints[i]); if (i + 1 < poly.npoints) { if (p.distance(poly.xpoints[i + 1], poly.ypoints[i + 1]) > (0.1 * world.getMapWidth())) { scalePolygon.addPoint(poly.xpoints[i], poly.ypoints[i]); } else { continue; } } else if (i + 1 == poly.npoints) { if (p.distance(poly.xpoints[0], poly.ypoints[0]) > (0.1 * world.getMapWidth())) { scalePolygon.addPoint(poly.xpoints[i], poly.ypoints[i]); } else { continue; } } } return scalePolygon; }
/** * Computes the circumscribed circle of the 3 input points. * * @return the circle that contains the three input points * @throws ColinearPoints2DException if the 3 points are colinear */ public static Circle2D circumCircle(Point2D p1, Point2D p2, Point2D p3) { // Computes circum center, possibly throwing ColinearPoints2DException Point2D center = circumCenter(p1, p2, p3); // compute radius double radius = Point2D.distance(center, p2); // return the created circle return new Circle2D(center, radius); }
/* * (non-Javadoc) * * @see math.geom2d.Shape2D#distance(double, double) */ public double distance(double x, double y) { // basic checkup if (points.isEmpty()) return Double.NaN; // find smallest distance double dist = Double.MAX_VALUE; for (Point2D point : points) dist = Math.min(dist, point.distance(x, y)); // return distance to closest point return dist; }
/** * Transforms this circle by an affine transform. If the transformed shape is a circle (ellipse * with equal axis lengths), returns an instance of Circle2D. The resulting ellipse is direct if * this ellipse and the transform are either both direct or both indirect. */ public EllipseShape2D transform(AffineTransform2D trans) { // When the transform is not a similarity, should switch to EllipseArc // computation if (!AffineTransform2D.isSimilarity(trans)) { return this.asEllipse().transform(trans); } // If transform is a similarity, the result is a circle Point2D center = this.center().transform(trans); Point2D p1 = this.firstPoint().transform(trans); boolean direct = !this.direct ^ trans.isDirect(); Circle2D result = new Circle2D(center, center.distance(p1), direct); return result; }
/** * Computes intersections of a circle with a line. Returns an array of Point2D, of size 0, 1 or 2 * depending on the distance between circle and line. If there are 2 intersections points, the * first one in the array is the first one on the line. * * @return a collection of intersection points * @since 0.11.1 */ public static Collection<Point2D> lineCircleIntersections( LinearShape2D line, CircularShape2D circle) { // initialize array of points (maximum 2 intersections) ArrayList<Point2D> intersections = new ArrayList<Point2D>(2); // extract parameters of the circle Circle2D parent = circle.supportingCircle(); Point2D center = parent.center(); double radius = parent.radius(); // Compute line perpendicular to the test line, and going through the // circle center StraightLine2D perp = StraightLine2D.createPerpendicular(line, center); // Compute distance between line and circle center Point2D inter = perp.intersection(new StraightLine2D(line)); if (inter == null) { throw new RuntimeException( "Could not compute intersection point when computing line-cicle intersection"); } double dist = inter.distance(center); // if the distance is the radius of the circle, return the // intersection point if (abs(dist - radius) < Shape2D.ACCURACY) { if (line.contains(inter) && circle.contains(inter)) intersections.add(inter); return intersections; } // compute angle of the line, and distance between 'inter' point and // each intersection point double angle = line.horizontalAngle(); double d2 = sqrt(radius * radius - dist * dist); // Compute position and angle of intersection points Point2D p1 = Point2D.createPolar(inter, d2, angle + Math.PI); Point2D p2 = Point2D.createPolar(inter, d2, angle); // add points to the array only if they belong to the line if (line.contains(p1) && circle.contains(p1)) intersections.add(p1); if (line.contains(p2) && circle.contains(p2)) intersections.add(p2); // return the result return intersections; }
/** * Creates a circle containing 3 points. * * @deprecated replaced by createCircle(Point2D, Point2D, Point2D) (0.11.1) */ @Deprecated public static Circle2D create(Point2D p1, Point2D p2, Point2D p3) { if (Point2D.isColinear(p1, p2, p3)) throw new ColinearPoints2DException(p1, p2, p3); // create two median lines StraightLine2D line12 = StraightLine2D.createMedian(p1, p2); StraightLine2D line23 = StraightLine2D.createMedian(p2, p3); // check medians are not parallel assert !AbstractLine2D.isParallel(line12, line23) : "If points are not colinear, medians should not be parallel"; // Compute intersection of the medians, and circle radius Point2D center = AbstractLine2D.getIntersection(line12, line23); double radius = Point2D.distance(center, p2); // return the created circle return new Circle2D(center, radius); }
/** * Computes the intersections points between two circles or circular shapes. * * @param circle1 an instance of circle or circle arc * @param circle2 an instance of circle or circle arc * @return a collection of 0, 1 or 2 intersection points */ public static Collection<Point2D> circlesIntersections(Circle2D circle1, Circle2D circle2) { // extract center and radius of each circle Point2D center1 = circle1.center(); Point2D center2 = circle2.center(); double r1 = circle1.radius(); double r2 = circle2.radius(); double d = Point2D.distance(center1, center2); // case of no intersection if (d < abs(r1 - r2) || d > (r1 + r2)) return new ArrayList<Point2D>(0); // Angle of line from center1 to center2 double angle = Angle2D.horizontalAngle(center1, center2); if (d == abs(r1 - r2) || d == (r1 + r2)) { Collection<Point2D> r = new ArrayList<>(1); r.add(Point2D.createPolar(center1, r1, angle)); return r; } // position of intermediate point double d1 = d / 2 + (r1 * r1 - r2 * r2) / (2 * d); Point2D tmp = Point2D.createPolar(center1, d1, angle); // distance between intermediate point and each intersection double h = sqrt(r1 * r1 - d1 * d1); // create empty array ArrayList<Point2D> intersections = new ArrayList<Point2D>(2); // Add the 2 intersection points Point2D p1 = Point2D.createPolar(tmp, h, angle + PI / 2); intersections.add(p1); Point2D p2 = Point2D.createPolar(tmp, h, angle - PI / 2); intersections.add(p2); return intersections; }
public double distance(double x, double y) { return abs(Point2D.distance(xc, yc, x, y) - r); }
public double distance(Point2D point) { return abs(Point2D.distance(xc, yc, point.x(), point.y()) - r); }
public double signedDistance(double x, double y) { if (direct) return Point2D.distance(xc, yc, x, y) - r; else return r - Point2D.distance(xc, yc, x, y); }
/* * (non-Javadoc) * * @see math.geom2d.Shape2D#distance(double, double) */ public double distance(double x, double y) { Point2D p = point(project(new Point2D(x, y))); return p.distance(x, y); }
/* * (non-Javadoc) * * @see java.awt.Shape#contains(double, double) */ public boolean contains(double x, double y) { for (Point2D point : points) if (point.distance(x, y) < Shape2D.ACCURACY) return true; return false; }