Example usage for java.awt.geom Line2D getX1

List of usage examples for java.awt.geom Line2D getX1

Introduction

In this page you can find the example usage for java.awt.geom Line2D getX1.

Prototype

public abstract double getX1();

Source Link

Document

Returns the X coordinate of the start point in double precision.

Usage

From source file:GeometryUtilities.java

public static double slope(Line2D line) {
    return slope(line.getX1(), line.getY1(), line.getX2(), line.getY2());
}

From source file:LineUtilities.java

/**
 * 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.
 *///from   w  ww.j  a  va 2 s. c o m
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

}

From source file:Main.java

/**
 * 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.
 *//*from  ww w  .  j  a va 2 s.c  o m*/
public static Shape createLineRegion(final Line2D line, final float width) {
    final GeneralPath result = new GeneralPath();
    final float x1 = (float) line.getX1();
    final float x2 = (float) line.getX2();
    final float y1 = (float) line.getY1();
    final float y2 = (float) line.getY2();
    if ((x2 - x1) != 0.0) {
        final double theta = Math.atan((y2 - y1) / (x2 - x1));
        final float dx = (float) Math.sin(theta) * width;
        final 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;
}

From source file:de.bund.bfr.jung.JungUtils.java

private static Line2D getOrthogonal(Line2D l) {
    float x1 = (float) l.getX1();
    float x2 = (float) l.getX2();
    float y1 = (float) l.getY1();
    float y2 = (float) l.getY2();
    float dx = x2 - x1;
    float dy = y2 - y1;

    float nx1 = x1 + dx / 2;
    float ny1 = y1 + dy / 2;
    float nx2 = nx1 - dy;
    float ny2 = ny1 + dx;

    return new Line2D.Float(nx1, ny1, nx2, ny2);
}

From source file:de.bund.bfr.jung.JungUtils.java

private static Point2D getIntersection(Line2D l1, Line2D l2) {
    float x1 = (float) l1.getX1();
    float x2 = (float) l1.getX2();
    float x3 = (float) l2.getX1();
    float x4 = (float) l2.getX2();
    float y1 = (float) l1.getY1();
    float y2 = (float) l1.getY2();
    float y3 = (float) l2.getY1();
    float y4 = (float) l2.getY2();
    float factor1 = x1 * y2 - y1 * x2;
    float factor2 = x3 * y4 - y3 * x4;
    float denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);

    float x = (factor1 * (x3 - x4) - (x1 - x2) * factor2) / denom;
    float y = (factor1 * (y3 - y4) - (y1 - y2) * factor2) / denom;

    return new Point2D.Float(x, y);
}

From source file:GeometryUtilities.java

public static Vector<Point2D> getCrossings(Line2D line0, Line2D line1) {
    Vector<Point2D> ret = new Vector<Point2D>();

    if (line0.intersectsLine(line1)) {
        Point2D.Double intersection = new Point2D.Double(0.0, 0.0);

        double xDiff0 = line0.getX2() - line0.getX1();
        double xDiff1 = line1.getX2() - line1.getX1();
        double yDiff0 = line0.getY2() - line0.getY1();
        double yDiff1 = line1.getY2() - line1.getY1();
        double xDiff2 = line0.getX1() - line1.getX1();
        double yDiff2 = line0.getY1() - line1.getY1();

        double div = yDiff1 * xDiff0 - xDiff1 * yDiff0;
        double u = (xDiff1 * yDiff2 - yDiff1 * xDiff2) / div;
        intersection.x = line0.getX1() + u * xDiff0;
        intersection.y = line0.getY1() + u * yDiff0;

        ret.add(intersection);//www  . jav  a  2  s.com
    }

    return ret;
}

From source file:Main.java

/**
 * Serialises a <code>Shape</code> object.
 *
 * @param shape  the shape object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *///from  w ww .j  a va 2s . c o  m
public static void writeShape(final Shape shape, final ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (shape != null) {
        stream.writeBoolean(false);
        if (shape instanceof Line2D) {
            final Line2D line = (Line2D) shape;
            stream.writeObject(Line2D.class);
            stream.writeDouble(line.getX1());
            stream.writeDouble(line.getY1());
            stream.writeDouble(line.getX2());
            stream.writeDouble(line.getY2());
        } else if (shape instanceof Rectangle2D) {
            final Rectangle2D rectangle = (Rectangle2D) shape;
            stream.writeObject(Rectangle2D.class);
            stream.writeDouble(rectangle.getX());
            stream.writeDouble(rectangle.getY());
            stream.writeDouble(rectangle.getWidth());
            stream.writeDouble(rectangle.getHeight());
        } else if (shape instanceof Ellipse2D) {
            final Ellipse2D ellipse = (Ellipse2D) shape;
            stream.writeObject(Ellipse2D.class);
            stream.writeDouble(ellipse.getX());
            stream.writeDouble(ellipse.getY());
            stream.writeDouble(ellipse.getWidth());
            stream.writeDouble(ellipse.getHeight());
        } else if (shape instanceof Arc2D) {
            final Arc2D arc = (Arc2D) shape;
            stream.writeObject(Arc2D.class);
            stream.writeDouble(arc.getX());
            stream.writeDouble(arc.getY());
            stream.writeDouble(arc.getWidth());
            stream.writeDouble(arc.getHeight());
            stream.writeDouble(arc.getAngleStart());
            stream.writeDouble(arc.getAngleExtent());
            stream.writeInt(arc.getArcType());
        } else if (shape instanceof GeneralPath) {
            stream.writeObject(GeneralPath.class);
            final PathIterator pi = shape.getPathIterator(null);
            final float[] args = new float[6];
            stream.writeBoolean(pi.isDone());
            while (!pi.isDone()) {
                final int type = pi.currentSegment(args);
                stream.writeInt(type);
                // TODO: could write this to only stream the values
                // required for the segment type
                for (int i = 0; i < 6; i++) {
                    stream.writeFloat(args[i]);
                }
                stream.writeInt(pi.getWindingRule());
                pi.next();
                stream.writeBoolean(pi.isDone());
            }
        } else {
            stream.writeObject(shape.getClass());
            stream.writeObject(shape);
        }
    } else {
        stream.writeBoolean(true);
    }
}

From source file:ShapeTransform.java

/**
 * Clips the given shape to the given bounds. If the shape is a Line2D, manual
 * clipping is performed, as the built in Area does not handle lines.
 * /*  ww w  .  j ava  2  s .  co  m*/
 * @param s
 *          the shape to be clipped
 * @param bounds
 *          the bounds to which the shape should be clipped
 * @return the clipped shape.
 */
public static Shape performCliping(final Shape s, final Rectangle2D bounds) {
    if (s instanceof Line2D) {
        final Line2D line = (Line2D) s;
        final Point2D[] clipped = getClipped(line.getX1(), line.getY1(), line.getX2(), line.getY2(), -DELTA,
                DELTA + bounds.getWidth(), -DELTA, DELTA + bounds.getHeight());
        if (clipped == null) {
            return new GeneralPath();
        }
        return new Line2D.Float(clipped[0], clipped[1]);
    }

    final Rectangle2D boundsCorrected = bounds.getBounds2D();
    boundsCorrected.setRect(-DELTA, -DELTA, DELTA + boundsCorrected.getWidth(),
            DELTA + boundsCorrected.getHeight());
    final Area a = new Area(boundsCorrected);
    if (a.isEmpty()) {
        // don't clip ... Area does not like lines
        // operations with lines always result in an empty Bounds:(0,0,0,0) area
        return new GeneralPath();
    }

    final Area clipArea = new Area(s);
    a.intersect(clipArea);
    return a;

}

From source file:net.sourceforge.processdash.ev.ui.chart.TooltipLineXYLineAndShapeRenderer.java

/**
 * Creates and returns a polygon that has the same shape as the line passed
 *  as a parameter, but ticker so it can be used as a mouse-over tooltip area
 *//*from w w w  .  j  a v a  2  s.  c  o m*/
private Shape getTooltipArea(Line2D line, int series) {
    GeneralPath area = new GeneralPath();

    float areaWidth = getAreaWidth(series);

    area.moveTo((float) line.getX1(), (float) (line.getY1() + areaWidth / 2));
    area.lineTo((float) line.getX2(), (float) (line.getY2() + areaWidth / 2));
    area.lineTo((float) line.getX2(), (float) (line.getY2() - areaWidth / 2));
    area.lineTo((float) line.getX1(), (float) (line.getY1() - areaWidth / 2));
    area.closePath();

    return area;
}

From source file:net.sourceforge.processdash.ev.ui.chart.RangeXYItemRenderer.java

private void drawItemRangeGradient(Graphics2D g2, Line2D line, Paint paint, Stroke stroke, double x2, double y2,
        double x3, double y3) {
    Line2D edge1, edge2, mainLine;
    Polygon fillArea;//from w w  w.j  a  v  a  2s  . c  o m
    Stroke mainLineStroke, edgeLineStroke;
    Paint mainLinePaint, edgeLinePaint, fillPaint;

    double x0 = line.getX1();
    double y0 = line.getY1();
    double x1 = line.getX2();
    double y1 = line.getY2();

    mainLine = new Line2D.Double(x0, y0, x1, y1);
    edge1 = new Line2D.Double(x0, y0, x2, y2);
    edge2 = new Line2D.Double(x0, y0, x3, y3);
    fillArea = new Polygon();
    fillArea.addPoint((int) Math.round(x0), (int) Math.round(y0));
    fillArea.addPoint((int) Math.round(x2), (int) Math.round(y2));
    fillArea.addPoint((int) Math.round(x3), (int) Math.round(y3));

    mainLinePaint = paint;
    if (mainLinePaint instanceof Color) {
        Color c = (Color) mainLinePaint;
        Color dark = transp(c, calcAlpha(c));
        Color light = transp(c, 0.01);
        edgeLinePaint = fillPaint = c;
        try {
            fillPaint = new GradientPaint(gradientStart(x0, y0, x1, y1, x2, y2, x3, y3), light,
                    new Point2D.Double(x1, y1), dark, true);
        } catch (Exception e) {
        }
    } else {
        edgeLinePaint = fillPaint = mainLinePaint;
    }

    if (stroke instanceof BasicStroke) {
        float lineWidth = ((BasicStroke) stroke).getLineWidth();
        edgeLineStroke = new BasicStroke(lineWidth / 4);
        mainLineStroke = new BasicStroke(lineWidth * 2);
    } else {
        mainLineStroke = edgeLineStroke = stroke;
    }

    g2.setPaint(fillPaint);
    g2.fill(fillArea);
    g2.fill(fillArea);

    g2.setStroke(edgeLineStroke);
    g2.setPaint(edgeLinePaint);
    g2.draw(edge1);
    g2.draw(edge2);

    g2.setStroke(mainLineStroke);
    g2.setPaint(mainLinePaint);
    g2.draw(mainLine);
}