Java Code Examples for java.awt.geom.Line2D#getY2()

The following examples show how to use java.awt.geom.Line2D#getY2() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RingPlot.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new line by extending an existing line.
 *
 * @param line  the line (<code>null</code> not permitted).
 * @param startPercent  the amount to extend the line at the start point
 *                      end.
 * @param endPercent  the amount to extend the line at the end point end.
 *
 * @return A new line.
 */
private Line2D extendLine(Line2D line, double startPercent,
                          double endPercent) {
    if (line == null) {
        throw new IllegalArgumentException("Null 'line' argument.");
    }
    double x1 = line.getX1();
    double x2 = line.getX2();
    double deltaX = x2 - x1;
    double y1 = line.getY1();
    double y2 = line.getY2();
    double deltaY = y2 - y1;
    x1 = x1 - (startPercent * deltaX);
    y1 = y1 - (startPercent * deltaY);
    x2 = x2 + (endPercent * deltaX);
    y2 = y2 + (endPercent * deltaY);
    return new Line2D.Double(x1, y1, x2, y2);
}
 
Example 2
Source File: LocalAreaUtil.java    From mars-sim with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a thin (1 mm wide) rectangle path representing a line.
 * 
 * @param line the line.
 * @return rectangle path for the line.
 */
private static Path2D createLinePath(Line2D line) {

	// Make rectangle width 1mm.
	double width = .001D;
	double length = line.getP1().distance(line.getP2());
	double centerX = (line.getX1() + line.getX2()) / 2D;
	double centerY = (line.getY1() + line.getY2()) / 2D;

	double x1 = centerX - (width / 2D);
	double y1 = centerY - (length / 2D);
	Rectangle2D lineRect = new Rectangle2D.Double(x1, y1, width, length);

	double facing = getDirection(line.getP1(), line.getP2());

	Path2D rectPath = getPathFromRectangleRotation(lineRect, facing);

	return rectPath;
}
 
Example 3
Source File: LineUtilities.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new line by extending an existing line.
 *
 * @param line  the line (<code>null</code> not permitted).
 * @param startPercent  the amount to extend the line at the start point
 *                      end.
 * @param endPercent  the amount to extend the line at the end point end.
 *
 * @return A new line.
 * 
 * @since 1.0.18
 */
public static Line2D extendLine(Line2D line, double startPercent,
                          double endPercent) {
    ParamChecks.nullNotPermitted(line, "line");
    double x1 = line.getX1();
    double x2 = line.getX2();
    double deltaX = x2 - x1;
    double y1 = line.getY1();
    double y2 = line.getY2();
    double deltaY = y2 - y1;
    x1 = x1 - (startPercent * deltaX);
    y1 = y1 - (startPercent * deltaY);
    x2 = x2 + (endPercent * deltaX);
    y2 = y2 + (endPercent * deltaY);
    return new Line2D.Double(x1, y1, x2, y2);
}
 
Example 4
Source File: LineUtil.java    From libreveris with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Return the bisector (french: médiatrice) of the provided segment
 *
 * @param segment the provided segment
 * @return (a segment on) the bisector
 */
public static Line2D bisector (Line2D segment)
{
    double x1 = segment.getX1();
    double y1 = segment.getY1();

    double hdx = (segment.getX2() - x1) / 2;
    double hdy = (segment.getY2() - y1) / 2;

    // Use middle as reference point
    double mx = x1 + hdx;
    double my = y1 + hdy;

    double x3 = mx + hdy;
    double y3 = my - hdx;
    double x4 = mx - hdy;
    double y4 = my + hdx;

    return new Line2D.Double(x3, y3, x4, y4);
}
 
Example 5
Source File: ShapeUtilities.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a region surrounding a line segment by 'widening' the line
 * segment.  A typical use for this method is the creation of a
 * 'clickable' region for a line that is displayed on-screen.
 *
 * @param line  the line (<code>null</code> not permitted).
 * @param width  the width of the region.
 *
 * @return A region that surrounds the line.
 */
public static Shape createLineRegion(Line2D line, float width) {
    GeneralPath result = new GeneralPath();
    float x1 = (float) line.getX1();
    float x2 = (float) line.getX2();
    float y1 = (float) line.getY1();
    float y2 = (float) line.getY2();
    if ((x2 - x1) != 0.0) {
        double theta = Math.atan((y2 - y1) / (x2 - x1));
        float dx = (float) Math.sin(theta) * width;
        float dy = (float) Math.cos(theta) * width;
        result.moveTo(x1 - dx, y1 + dy);
        result.lineTo(x1 + dx, y1 - dy);
        result.lineTo(x2 + dx, y2 - dy);
        result.lineTo(x2 - dx, y2 + dy);
        result.closePath();
    }
    else {
        // special case, vertical line
        result.moveTo(x1 - width / 2.0f, y1);
        result.lineTo(x1 + width / 2.0f, y1);
        result.lineTo(x2 + width / 2.0f, y2);
        result.lineTo(x2 - width / 2.0f, y2);
        result.closePath();
    }
    return result;
}
 
Example 6
Source File: LineUtil.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Computation of rotation from first to last point, with middle as approximate
 * middle point of the curve.
 *
 * @param line   straight line from curve start to curve stop
 * @param middle middle point of curve
 * @return central rotation angle (in radians) from curve start to curve stop.
 */
public static double rotation (Line2D line,
                               Point2D middle)
{
    double dx = line.getX2() - line.getX1();
    double dy = line.getY2() - line.getY1();
    double halfChordLengthSq = ((dx * dx) + (dy * dy)) / 4;
    double sagittaSq = line.ptLineDistSq(middle);

    return 4 * Math.atan(Math.sqrt(sagittaSq / halfChordLengthSq));
}
 
Example 7
Source File: LineModel.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Point2D getEndVector (boolean reverse)
{
    int dir = reverse ? (-1) : 1;
    Line2D l = line.toDouble();
    double length = l.getP1().distance(l.getP2());

    return new Point2D.Double(
            (dir * (l.getX2() - l.getX1())) / length,
            (dir * (l.getY2() - l.getY1())) / length);
}
 
Example 8
Source File: PointUtil.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Report the middle point of a Line2D.
 *
 * @param line provided line2D
 * @return the middle point between p1 and p2
 */
public static Point2D middle (Line2D line)
{
    return new Point2D.Double(
            (line.getX1() + line.getX2()) / 2.0,
            (line.getY1() + line.getY2()) / 2.0);
}
 
Example 9
Source File: DefaultProcessDiagramCanvas.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
/**
 * This method calculates intersections of two lines.
 * 
 * @param a
 *            Line 1
 * @param b
 *            Line 2
 * @return Intersection point
 */
private static Point getLinesIntersection(Line2D a, Line2D b) {
    double d = (a.getX1() - a.getX2()) * (b.getY2() - b.getY1()) - (a.getY1() - a.getY2()) * (b.getX2() - b.getX1());
    double da = (a.getX1() - b.getX1()) * (b.getY2() - b.getY1()) - (a.getY1() - b.getY1()) * (b.getX2() - b.getX1());
    // double db = (a.getX1()-a.getX2())*(a.getY1()-b.getY1()) - (a.getY1()-a.getY2())*(a.getX1()-b.getX1());
    double ta = da / d;
    // double tb = db/d;
    Point p = new Point();
    p.setLocation(a.getX1() + ta * (a.getX2() - a.getX1()), a.getY1() + ta * (a.getY2() - a.getY1()));
    return p;
}
 
Example 10
Source File: SigPainter.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void visit (HeadInter head)
{
    final Line2D midLine = head.getMidLine();

    if (midLine != null) {
        if (splitMirrors()) {
            // Draw head proper half
            int width = head.getBounds().width;
            int xDir = midLine.getY2() > midLine.getY1() ? -1 : +1;
            Path2D p = new Path2D.Double();
            p.append(midLine, false);
            p.lineTo(midLine.getX2() + xDir * width, midLine.getY2());
            p.lineTo(midLine.getX1() + xDir * width, midLine.getY1());
            p.closePath();

            java.awt.Shape oldClip = g.getClip();
            g.clip(p);
            visit((Inter) head);
            g.setClip(oldClip);
        } else {
            visit((Inter) head);
        }

        // Draw midLine using complementary color of head
        Color compColor = UIUtil.complementaryColor(g.getColor());
        Stroke oldStroke = UIUtil.setAbsoluteStroke(g, 1f);
        g.setColor(compColor);
        g.draw(midLine);
        g.setStroke(oldStroke);
    } else {
        visit((Inter) head);
    }
}
 
Example 11
Source File: LineUtilities.java    From ccu-historian with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Clips the specified line to the given rectangle.
 *
 * @param line  the line (<code>null</code> not permitted).
 * @param rect  the clipping rectangle (<code>null</code> not permitted).
 *
 * @return <code>true</code> if the clipped line is visible, and
 *     <code>false</code> otherwise.
 */
public static boolean clipLine(Line2D line, Rectangle2D rect) {

    double x1 = line.getX1();
    double y1 = line.getY1();
    double x2 = line.getX2();
    double y2 = line.getY2();

    double minX = rect.getMinX();
    double maxX = rect.getMaxX();
    double minY = rect.getMinY();
    double maxY = rect.getMaxY();

    int f1 = rect.outcode(x1, y1);
    int f2 = rect.outcode(x2, y2);

    while ((f1 | f2) != 0) {
        if ((f1 & f2) != 0) {
            return false;
        }
        double dx = (x2 - x1);
        double dy = (y2 - y1);
        // update (x1, y1), (x2, y2) and f1 and f2 using intersections
        // then recheck
        if (f1 != 0) {
            // first point is outside, so we update it against one of the
            // four sides then continue
            if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
                    && dx != 0.0) {
                y1 = y1 + (minX - x1) * dy / dx;
                x1 = minX;
            }
            else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
                    && dx != 0.0) {
                y1 = y1 + (maxX - x1) * dy / dx;
                x1 = maxX;
            }
            else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
                    && dy != 0.0) {
                x1 = x1 + (maxY - y1) * dx / dy;
                y1 = maxY;
            }
            else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
                    && dy != 0.0) {
                x1 = x1 + (minY - y1) * dx / dy;
                y1 = minY;
            }
            f1 = rect.outcode(x1, y1);
        }
        else if (f2 != 0) {
            // second point is outside, so we update it against one of the
            // four sides then continue
            if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
                    && dx != 0.0) {
                y2 = y2 + (minX - x2) * dy / dx;
                x2 = minX;
            }
            else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
                    && dx != 0.0) {
                y2 = y2 + (maxX - x2) * dy / dx;
                x2 = maxX;
            }
            else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
                    && dy != 0.0) {
                x2 = x2 + (maxY - y2) * dx / dy;
                y2 = maxY;
            }
            else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
                    && dy != 0.0) {
                x2 = x2 + (minY - y2) * dx / dy;
                y2 = minY;
            }
            f2 = rect.outcode(x2, y2);
        }
    }

    line.setLine(x1, y1, x2, y2);
    return true;  // the line is visible - if it wasn't, we'd have
                  // returned false from within the while loop above

}
 
Example 12
Source File: GeometryUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static double getShiftByX(Line2D aLine, double aPointDeltaX) {
  double width = aLine.getX2() - aLine.getX1();
  double height = aLine.getY2() - aLine.getY1();
  return aPointDeltaX * (height / width);
}
 
Example 13
Source File: CrosshairOverlay.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
/**
 * Calculates the anchor point for a label.
 *
 * @param line  the line for the crosshair.
 * @param anchor  the anchor point.
 * @param deltaX  the x-offset.
 * @param deltaY  the y-offset.
 *
 * @return The anchor point.
 */
private Point2D calculateLabelPoint(Line2D line, RectangleAnchor anchor,
        double deltaX, double deltaY) {
    double x, y;
    boolean left = (anchor == RectangleAnchor.BOTTOM_LEFT 
            || anchor == RectangleAnchor.LEFT 
            || anchor == RectangleAnchor.TOP_LEFT);
    boolean right = (anchor == RectangleAnchor.BOTTOM_RIGHT 
            || anchor == RectangleAnchor.RIGHT 
            || anchor == RectangleAnchor.TOP_RIGHT);
    boolean top = (anchor == RectangleAnchor.TOP_LEFT 
            || anchor == RectangleAnchor.TOP 
            || anchor == RectangleAnchor.TOP_RIGHT);
    boolean bottom = (anchor == RectangleAnchor.BOTTOM_LEFT
            || anchor == RectangleAnchor.BOTTOM
            || anchor == RectangleAnchor.BOTTOM_RIGHT);
    Rectangle rect = line.getBounds();
    
    // we expect the line to be vertical or horizontal
    if (line.getX1() == line.getX2()) {  // vertical
        x = line.getX1();
        y = (line.getY1() + line.getY2()) / 2.0;
        if (left) {
            x = x - deltaX;
        }
        if (right) {
            x = x + deltaX;
        }
        if (top) {
            y = Math.min(line.getY1(), line.getY2()) + deltaY;
        }
        if (bottom) {
            y = Math.max(line.getY1(), line.getY2()) - deltaY;
        }
    }
    else {  // horizontal
        x = (line.getX1() + line.getX2()) / 2.0;
        y = line.getY1();
        if (left) {
            x = Math.min(line.getX1(), line.getX2()) + deltaX;
        }
        if (right) {
            x = Math.max(line.getX1(), line.getX2()) - deltaX;
        }
        if (top) {
            y = y - deltaY;
        }
        if (bottom) {
            y = y + deltaY;
        }
    }
    return new Point2D.Double(x, y);
}
 
Example 14
Source File: ShapeUtilities.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Clips the specified line to the given rectangle.
 *
 * @param line  the line (<code>null</code> not permitted).
 * @param rect  the clipping rectangle (<code>null</code> not permitted).
 *
 * @return <code>true</code> if the clipped line is visible, and
 *     <code>false</code> otherwise.
 *
 * @since 1.2.0
 */
public static boolean clipLine(Line2D line, Rectangle2D rect) {

    double x1 = line.getX1();
    double y1 = line.getY1();
    double x2 = line.getX2();
    double y2 = line.getY2();

    double minX = rect.getMinX();
    double maxX = rect.getMaxX();
    double minY = rect.getMinY();
    double maxY = rect.getMaxY();

    int f1 = rect.outcode(x1, y1);
    int f2 = rect.outcode(x2, y2);

    while ((f1 | f2) != 0) {
        if ((f1 & f2) != 0) {
            return false;
        }
        double dx = (x2 - x1);
        double dy = (y2 - y1);
        // update (x1, y1), (x2, y2) and f1 and f2 using intersections
        // then recheck
        if (f1 != 0) {
            // first point is outside, so we update it against one of the
            // four sides then continue
            if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
                    && dx != 0.0) {
                y1 = y1 + (minX - x1) * dy / dx;
                x1 = minX;
            }
            else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
                    && dx != 0.0) {
                y1 = y1 + (maxX - x1) * dy / dx;
                x1 = maxX;
            }
            else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
                    && dy != 0.0) {
                x1 = x1 + (maxY - y1) * dx / dy;
                y1 = maxY;
            }
            else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
                    && dy != 0.0) {
                x1 = x1 + (minY - y1) * dx / dy;
                y1 = minY;
            }
            f1 = rect.outcode(x1, y1);
        }
        else if (f2 != 0) {
            // second point is outside, so we update it against one of the
            // four sides then continue
            if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
                    && dx != 0.0) {
                y2 = y2 + (minX - x2) * dy / dx;
                x2 = minX;
            }
            else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
                    && dx != 0.0) {
                y2 = y2 + (maxX - x2) * dy / dx;
                x2 = maxX;
            }
            else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
                    && dy != 0.0) {
                x2 = x2 + (maxY - y2) * dx / dy;
                y2 = maxY;
            }
            else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
                    && dy != 0.0) {
                x2 = x2 + (minY - y2) * dx / dy;
                y2 = minY;
            }
            f2 = rect.outcode(x2, y2);
        }
    }

    line.setLine(x1, y1, x2, y2);
    return true;  // the line is visible - if it wasn't, we'd have
                  // returned false from within the while loop above

}
 
Example 15
Source File: Intersection.java    From dsworkbench with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the intersection point of two lines.
 *
 * @param   line1   First line
 * @param   line2   Second line
 * @return  The Point object where the two lines intersect. This method
 * returns null if the two lines do not intersect.
 * @throws  <tt>MultipleIntersectionException</tt> when the two lines
 * have more than one intersection point.
 */
static public Point getIntersection(Line2D line1, Line2D line2)
        throws Exception {
    double dyline1, dxline1;
    double dyline2, dxline2, e, f;
    double x1line1, y1line1, x2line1, y2line1;
    double x1line2, y1line2, x2line2, y2line2;

    if (!line1.intersectsLine(line2)) {
        return null;
    }

    /* first, check to see if the segments intersect by parameterization
    on s and t; if s and t are both between [0,1], then the
    segments intersect */
    x1line1 =  line1.getX1();
    y1line1 =  line1.getY1();
    x2line1 =  line1.getX2();
    y2line1 =  line1.getY2();

    x1line2 =  line2.getX1();
    y1line2 =  line2.getY1();
    x2line2 =  line2.getX2();
    y2line2 =  line2.getY2();

    /* check to see if the segments have any endpoints in common. If they do,
    then return the endpoints as the intersection point */
    if ((x1line1 == x1line2) && (y1line1 == y1line2)) {
        return (new Point((int) x1line1, (int) y1line1));
    }
    if ((x1line1 == x2line2) && (y1line1 == y2line2)) {
        return (new Point((int) x1line1, (int) y1line1));
    }
    if ((x2line1 == x1line2) && (y2line1 == y1line2)) {
        return (new Point((int) x2line1, (int) y2line1));
    }
    if ((x2line1 == x2line2) && (y2line1 == y2line2)) {
        return (new Point((int) x2line1, (int) y2line1));
    }

    dyline1 = -(y2line1 - y1line1);
    dxline1 = x2line1 - x1line1;

    dyline2 = -(y2line2 - y1line2);
    dxline2 = x2line2 - x1line2;

    e = -(dyline1 * x1line1) - (dxline1 * y1line1);
    f = -(dyline2 * x1line2) - (dxline2 * y1line2);

    /* compute the intersection point using
    ax+by+e = 0 and cx+dy+f = 0
    
    If there is more than 1 intersection point between two lines,
     */
    if ((dyline1 * dxline2 - dyline2 * dxline1) == 0) {
        throw new Exception("ZERO!");
    }
    return (new Point(
            (int) (-(e * dxline2 - dxline1 * f) / (dyline1 * dxline2 - dyline2 * dxline1)),
            (int) (-(dyline1 * f - dyline2 * e) / (dyline1 * dxline2 - dyline2 * dxline1))));
}
 
Example 16
Source File: GeometryUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static double getShiftByY(Line2D aLine, double aPointDeltaY) {
  return aPointDeltaY * ((aLine.getX2() - aLine.getX1()) / (aLine.getY2() - aLine.getY1()));
}
 
Example 17
Source File: LineUtilities.java    From buffer_bci with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Clips the specified line to the given rectangle.
 *
 * @param line  the line (<code>null</code> not permitted).
 * @param rect  the clipping rectangle (<code>null</code> not permitted).
 *
 * @return <code>true</code> if the clipped line is visible, and
 *     <code>false</code> otherwise.
 */
public static boolean clipLine(Line2D line, Rectangle2D rect) {

    double x1 = line.getX1();
    double y1 = line.getY1();
    double x2 = line.getX2();
    double y2 = line.getY2();

    double minX = rect.getMinX();
    double maxX = rect.getMaxX();
    double minY = rect.getMinY();
    double maxY = rect.getMaxY();

    int f1 = rect.outcode(x1, y1);
    int f2 = rect.outcode(x2, y2);

    while ((f1 | f2) != 0) {
        if ((f1 & f2) != 0) {
            return false;
        }
        double dx = (x2 - x1);
        double dy = (y2 - y1);
        // update (x1, y1), (x2, y2) and f1 and f2 using intersections
        // then recheck
        if (f1 != 0) {
            // first point is outside, so we update it against one of the
            // four sides then continue
            if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
                    && dx != 0.0) {
                y1 = y1 + (minX - x1) * dy / dx;
                x1 = minX;
            }
            else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
                    && dx != 0.0) {
                y1 = y1 + (maxX - x1) * dy / dx;
                x1 = maxX;
            }
            else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
                    && dy != 0.0) {
                x1 = x1 + (maxY - y1) * dx / dy;
                y1 = maxY;
            }
            else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
                    && dy != 0.0) {
                x1 = x1 + (minY - y1) * dx / dy;
                y1 = minY;
            }
            f1 = rect.outcode(x1, y1);
        }
        else if (f2 != 0) {
            // second point is outside, so we update it against one of the
            // four sides then continue
            if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
                    && dx != 0.0) {
                y2 = y2 + (minX - x2) * dy / dx;
                x2 = minX;
            }
            else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
                    && dx != 0.0) {
                y2 = y2 + (maxX - x2) * dy / dx;
                x2 = maxX;
            }
            else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
                    && dy != 0.0) {
                x2 = x2 + (maxY - y2) * dx / dy;
                y2 = maxY;
            }
            else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
                    && dy != 0.0) {
                x2 = x2 + (minY - y2) * dx / dy;
                y2 = minY;
            }
            f2 = rect.outcode(x2, y2);
        }
    }

    line.setLine(x1, y1, x2, y2);
    return true;  // the line is visible - if it wasn't, we'd have
                  // returned false from within the while loop above

}
 
Example 18
Source File: CrosshairOverlay.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calculates the anchor point for a label.
 *
 * @param line  the line for the crosshair.
 * @param anchor  the anchor point.
 * @param deltaX  the x-offset.
 * @param deltaY  the y-offset.
 *
 * @return The anchor point.
 */
private Point2D calculateLabelPoint(Line2D line, RectangleAnchor anchor,
        double deltaX, double deltaY) {
    double x = 0.0;
    double y = 0.0;
    boolean left = (anchor == RectangleAnchor.BOTTOM_LEFT
            || anchor == RectangleAnchor.LEFT
            || anchor == RectangleAnchor.TOP_LEFT);
    boolean right = (anchor == RectangleAnchor.BOTTOM_RIGHT
            || anchor == RectangleAnchor.RIGHT
            || anchor == RectangleAnchor.TOP_RIGHT);
    boolean top = (anchor == RectangleAnchor.TOP_LEFT
            || anchor == RectangleAnchor.TOP
            || anchor == RectangleAnchor.TOP_RIGHT);
    boolean bottom = (anchor == RectangleAnchor.BOTTOM_LEFT
            || anchor == RectangleAnchor.BOTTOM
            || anchor == RectangleAnchor.BOTTOM_RIGHT);
    Rectangle rect = line.getBounds();

    // we expect the line to be vertical or horizontal
    if (line.getX1() == line.getX2()) {  // vertical
        x = line.getX1();
        y = (line.getY1() + line.getY2()) / 2.0;
        if (left) {
            x = x - deltaX;
        }
        if (right) {
            x = x + deltaX;
        }
        if (top) {
            y = Math.min(line.getY1(), line.getY2()) + deltaY;
        }
        if (bottom) {
            y = Math.max(line.getY1(), line.getY2()) - deltaY;
        }
    }
    else {  // horizontal
        x = (line.getX1() + line.getX2()) / 2.0;
        y = line.getY1();
        if (left) {
            x = Math.min(line.getX1(), line.getX2()) + deltaX;
        }
        if (right) {
            x = Math.max(line.getX1(), line.getX2()) - deltaX;
        }
        if (top) {
            y = y - deltaY;
        }
        if (bottom) {
            y = y + deltaY;
        }
    }
    return new Point2D.Double(x, y);
}
 
Example 19
Source File: LineUtilities.java    From openstock with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Clips the specified line to the given rectangle.
 *
 * @param line  the line (<code>null</code> not permitted).
 * @param rect  the clipping rectangle (<code>null</code> not permitted).
 *
 * @return <code>true</code> if the clipped line is visible, and
 *     <code>false</code> otherwise.
 */
public static boolean clipLine(Line2D line, Rectangle2D rect) {

    double x1 = line.getX1();
    double y1 = line.getY1();
    double x2 = line.getX2();
    double y2 = line.getY2();

    double minX = rect.getMinX();
    double maxX = rect.getMaxX();
    double minY = rect.getMinY();
    double maxY = rect.getMaxY();

    int f1 = rect.outcode(x1, y1);
    int f2 = rect.outcode(x2, y2);

    while ((f1 | f2) != 0) {
        if ((f1 & f2) != 0) {
            return false;
        }
        double dx = (x2 - x1);
        double dy = (y2 - y1);
        // update (x1, y1), (x2, y2) and f1 and f2 using intersections
        // then recheck
        if (f1 != 0) {
            // first point is outside, so we update it against one of the
            // four sides then continue
            if ((f1 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
                    && dx != 0.0) {
                y1 = y1 + (minX - x1) * dy / dx;
                x1 = minX;
            }
            else if ((f1 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
                    && dx != 0.0) {
                y1 = y1 + (maxX - x1) * dy / dx;
                x1 = maxX;
            }
            else if ((f1 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
                    && dy != 0.0) {
                x1 = x1 + (maxY - y1) * dx / dy;
                y1 = maxY;
            }
            else if ((f1 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
                    && dy != 0.0) {
                x1 = x1 + (minY - y1) * dx / dy;
                y1 = minY;
            }
            f1 = rect.outcode(x1, y1);
        }
        else if (f2 != 0) {
            // second point is outside, so we update it against one of the
            // four sides then continue
            if ((f2 & Rectangle2D.OUT_LEFT) == Rectangle2D.OUT_LEFT
                    && dx != 0.0) {
                y2 = y2 + (minX - x2) * dy / dx;
                x2 = minX;
            }
            else if ((f2 & Rectangle2D.OUT_RIGHT) == Rectangle2D.OUT_RIGHT
                    && dx != 0.0) {
                y2 = y2 + (maxX - x2) * dy / dx;
                x2 = maxX;
            }
            else if ((f2 & Rectangle2D.OUT_BOTTOM) == Rectangle2D.OUT_BOTTOM
                    && dy != 0.0) {
                x2 = x2 + (maxY - y2) * dx / dy;
                y2 = maxY;
            }
            else if ((f2 & Rectangle2D.OUT_TOP) == Rectangle2D.OUT_TOP
                    && dy != 0.0) {
                x2 = x2 + (minY - y2) * dx / dy;
                y2 = minY;
            }
            f2 = rect.outcode(x2, y2);
        }
    }

    line.setLine(x1, y1, x2, y2);
    return true;  // the line is visible - if it wasn't, we'd have
                  // returned false from within the while loop above

}
 
Example 20
Source File: TemplateMatcher.java    From osp with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Gets the distance and point along a Line2D at a specified y.
 * If the Line2D is horizontal this returns null.
 * 
 * Based on a simplification of algorithm described by Paul Burke
 * at http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/ (April 1986)
 * 
 * @param line the line
 * @param y the value of y
 * @return Object[] {fractional distance from line end, Point2D}
 */
private Object[] getDistanceAndPointAtY(Line2D line, double y) {
	double dy = line.getY2()-line.getY1();
	// if line is horizontal, return null
	if (dy==0) return null;
	// parametric eqn of line: P = P1 + u(P2 - P1)
	double u = (y-line.getY1())/dy;
double x = line.getX1() + u*(line.getX2()-line.getX1());
return new Object[] {u, new Point2D.Double(x, y)};
}