Java Geometry Algorithm makeCornerTo(GeneralPath gp, Point2D cornerPoint, Point2D nextCornerPoint, float radius)

Here you can find the source of makeCornerTo(GeneralPath gp, Point2D cornerPoint, Point2D nextCornerPoint, float radius)

Description

Draws a corner relative to the current point of the GeneralPath.

License

Open Source License

Declaration

private static void makeCornerTo(GeneralPath gp, Point2D cornerPoint, Point2D nextCornerPoint, float radius) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.geom.GeneralPath;

import java.awt.geom.Point2D;

public class Main {
    /**//from   w w  w .j ava2 s .  co  m
    * Draws a corner relative to the current point of the GeneralPath.  Note the radius denotes the 
    * distance from the cornerPoint to where the curve starts on the line formed from the cornerPoint to the
    * current point on the gp and where the curve ends on the line from the cornerPoint to the nextCornerPoint.
    * In other words, 
    */
    private static void makeCornerTo(GeneralPath gp, Point2D cornerPoint, Point2D nextCornerPoint, float radius) {
        Point2D currentPoint = gp.getCurrentPoint();

        //get fractional to the corner where the line first starts to curve
        double distance = currentPoint.distance(cornerPoint);
        double fraction = (distance - radius) / distance;

        //calculate these distance from the current point
        double xDistance = (cornerPoint.getX() - currentPoint.getX()) * fraction;
        double yDistance = (cornerPoint.getY() - currentPoint.getY()) * fraction;

        //draw a line to the point where the line first starts to curve
        lineToRelative(gp, (float) xDistance, (float) yDistance);

        Point2D startCurvePoint = gp.getCurrentPoint();

        //get fractional to the corner where the line first starts to curve
        double distanceFromCornerToNextCorner = cornerPoint.distance(nextCornerPoint);
        double fractionToNextCorner = radius / distanceFromCornerToNextCorner;

        //calculate these distance from the current point
        double xDistanceFromCornerToEndCurve = (nextCornerPoint.getX() - cornerPoint.getX()) * fractionToNextCorner;
        double yDistanceFromCornerToEndCurve = (nextCornerPoint.getY() - cornerPoint.getY()) * fractionToNextCorner;

        Point2D endCurvePoint = new Point2D.Double(cornerPoint.getX() + xDistanceFromCornerToEndCurve,
                cornerPoint.getY() + yDistanceFromCornerToEndCurve);

        //finally draw the cornerShape
        cornerShape(gp,
                //start at:
                (float) startCurvePoint.getX(), (float) startCurvePoint.getY(),
                //corner at:
                (float) cornerPoint.getX(), (float) cornerPoint.getY(),
                //end at:
                (float) endCurvePoint.getX(), (float) endCurvePoint.getY());

        //          System.out.println("StartCurve at: " + startCurvePoint);
        //          System.out.println("Corner at: " + cornerPoint);
        //          System.out.println("EndCurve at: " + endCurvePoint);
        //          System.out.println("NextCorner at: " + nextCornerPoint);
    }

    /**
    * Draws a line segment relative to the current point of the GeneralPath.
    */
    public static void lineToRelative(GeneralPath gp, float x, float y) {
        Point2D currentPoint = gp.getCurrentPoint();
        gp.lineTo((float) currentPoint.getX() + x, (float) currentPoint.getY() + y);
    }

    /** Assumes we are at (x1,y1), the corner point is (x2,y2), and we end at (x3, y3) */
    public static void cornerShape(GeneralPath gp, float x1, float y1, float x2, float y2, float x3, float y3) {
        gp.curveTo((x1 + x2) / 2, (y1 + y2) / 2, (x2 + x3) / 2, (y2 + y3) / 2, x3, y3);
    }

    public static void curveTo(GeneralPath gp, float x1, float y1, float x2, float y2, float x3, float y3) {
        Point2D currentPoint = gp.getCurrentPoint();
        gp.curveTo(x1 + (float) currentPoint.getX(), y1 + (float) currentPoint.getY(),
                x2 + (float) currentPoint.getX(), y2 + (float) currentPoint.getY(),
                x3 + (float) currentPoint.getX(), y3 + (float) currentPoint.getY());
    }
}

Related

  1. interceptLineAndBox(Point2D startPosition, Point2D endPosition, RectangularShape boundingBox)
  2. interpol(Point p1, Point p2, float factor)
  3. invVec(final Point2D v)
  4. lonLatToString(Point2D.Double pt)
  5. makeCircle(double xCenter, double yCenter, double r, int nPoints)
  6. makeLine(Point2D.Double center, Point2D.Double north, Point2D.Double east)
  7. makeNeighbor(Point theHex, int direction)
  8. manhattanDistance(Point a, Point b)
  9. mirrorMoveVertically(Point move, int size)