/** * Gets the bounds of the label text * * @param g2 the graphics context * @return the bounds of the label text */ private Rectangle2D getLabelBounds() { BufferedImage dummy = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB); // need a dummy image to get a Graphics to // measure the size Graphics2D g2 = (Graphics2D) dummy.getGraphics(); label.setText("<html>" + labelText + "</html>"); label.setFont(g2.getFont()); Dimension d = label.getPreferredSize(); label.setBounds(0, 0, d.width, d.height); Line2D line = getConnectionPoints(); Point2D control = getControlPoint(); double x = control.getX() / 2 + line.getX1() / 4 + line.getX2() / 4; double y = control.getY() / 2 + line.getY1() / 4 + line.getY2() / 4; final int GAP = 3; if (line.getY1() == line.getY2()) x -= d.getWidth() / 2; else if (line.getY1() <= line.getY2()) x += GAP; else x -= d.getWidth() + GAP; if (line.getX1() == line.getX2()) y += d.getHeight() / 2; else if (line.getX1() <= line.getX2()) y -= d.getHeight() + GAP; else y += GAP; if (Math.abs(line.getX1() - line.getX2()) >= Math.abs(line.getY1() - line.getY2())) { x = x - d.getWidth() / 2; } if (Math.abs(line.getX1() - line.getX2()) <= Math.abs(line.getY1() - line.getY2())) { y = y - d.getHeight() / 2; } return new Rectangle2D.Double(x, y, d.width, d.height); }
public LineSegmentIntersection(Line2D segment, Line2D lineB) { final double x1 = segment.getX1(); final double y1 = segment.getY1(); final double x2 = segment.getX2(); final double y2 = segment.getY2(); final double x3 = lineB.getX1(); final double y3 = lineB.getY1(); final double x4 = lineB.getX2(); final double y4 = lineB.getY2(); final double den = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1); if (den == 0) { inter = null; } else { final double uA1 = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3); final double uA = uA1 / den; final double x = x1 + uA * (x2 - x1); final double y = y1 + uA * (y2 - y1); if (uA >= 0 && uA <= 1) { inter = new Point2D.Double(x, y); } else { inter = null; } } }
/** * Function to check if a direction and frame intersects * * @param direction * @param frame * @return */ public boolean intersects(Line2D direction, Line2D frame) { double d = (frame.getY2() - frame.getY1()) * (direction.getX2() - direction.getX1()) - (frame.getX2() - frame.getX1()) * (direction.getY2() - direction.getY1()); double n_a = (frame.getX2() - frame.getX1()) * (direction.getY1() - frame.getY1()) - (frame.getY2() - frame.getY1()) * (direction.getX1() - frame.getX1()); double n_b = (direction.getX2() - direction.getX1()) * (direction.getY1() - frame.getY1()) - (direction.getY2() - direction.getY1()) * (direction.getX1() - frame.getX1()); if (d == 0) return false; double ua = n_a / d; double ub = n_b / d; if (ua >= 0d && ua <= 1d && ub >= 0d && ub <= 1d) { intersection = new Point2D.Double(); intersection.setLocation( direction.getX1() + (ua * (direction.getX2() - direction.getX1())), direction.getY1() + (ua * (direction.getY2() - direction.getY1()))); return true; } return false; }
/** * Gets the control point for the quadratic spline. * * @return the control point */ private Point2D getControlPoint() { Line2D line = getConnectionPoints(); double t = Math.tan(Math.toRadians(angle)); double dx = (line.getX2() - line.getX1()) / 2; double dy = (line.getY2() - line.getY1()) / 2; return new Point2D.Double( (line.getX1() + line.getX2()) / 2 + t * dy, (line.getY1() + line.getY2()) / 2 - t * dx); }
public Line2D evaluate(Line2D v0, Line2D v1, float fraction) { double x1 = v0.getX1() + ((v1.getX1() - v0.getX1()) * fraction); double y1 = v0.getY1() + ((v1.getY1() - v0.getY1()) * fraction); double x2 = v0.getX2() + ((v1.getX2() - v0.getX2()) * fraction); double y2 = v0.getY2() + ((v1.getY2() - v0.getY2()) * fraction); Line2D value = (Line2D) v0.clone(); value.setLine(x1, y1, x2, y2); return value; }
/** @see Graphics#drawPolyline(int[], int[], int) */ public void drawPolyline(int[] x, int[] y, int nPoints) { Line2D line = new Line2D.Double(x[0], y[0], x[0], y[0]); for (int i = 1; i < nPoints; i++) { line.setLine(line.getX2(), line.getY2(), x[i], y[i]); draw(line); } }
/** * 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(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; }
/** * Gets the distance and point along a Line2D at a specified x. If the Line2D is vertical this * returns null. * * <p>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 x the value of x * @return Object[] {fractional distance from line end, Point2D} */ private Object[] getDistanceAndPointAtX(Line2D line, double x) { double dx = line.getX2() - line.getX1(); // if line is vertical, return null if (dx == 0) return null; // parametric eqn of line: P = P1 + u(P2 - P1) double u = (x - line.getX1()) / dx; double y = line.getY1() + u * (line.getY2() - line.getY1()); return new Object[] {u, new Point2D.Double(x, y)}; }
/** * Gets the distance and point along a Line2D at a specified y. If the Line2D is horizontal this * returns null. * * <p>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)}; }
private static CubicCurve2D _toCubicCurve(Shape seg, CubicCurve2D cub) { if (cub == null) { cub = new CubicCurve2D.Double(); } if (seg instanceof Line2D) { Line2D src = (Line2D) seg; cub.setCurve( src.getX1(), src.getY1(), src.getX1(), src.getY1(), src.getX2(), src.getY2(), src.getX2(), src.getY2()); } else if (seg instanceof Ellipse2D) { throw new InternalError("Can't convert Ellipse2D to CubicCurve2D"); } else if (seg instanceof Arc2D) { throw new InternalError("Can't convert Arc2D to CubicCurve2D"); } else if (seg instanceof QuadCurve2D) { QuadCurve2D src = (QuadCurve2D) seg; cub.setCurve( src.getX1(), src.getY1(), src.getCtrlX(), src.getCtrlY(), src.getCtrlX(), src.getCtrlY(), src.getX2(), src.getY2()); } else { CubicCurve2D src = (CubicCurve2D) seg; cub.setCurve( src.getX1(), src.getY1(), src.getCtrlX1(), src.getCtrlY1(), src.getCtrlX2(), src.getCtrlY2(), src.getX2(), src.getY2()); } return cub; }
/** * Creates and returns a line that is perpendicular to the specified line. * * @param line the reference line ({@code null} not permitted). * @param pt1 a point on the reference line ({@code null} not permitted). * @param size the length of the new line. * @param opposingPoint an opposing point, to define which side of the reference line the * perpendicular line will extend ({@code null} not permitted). * @return The perpendicular line. */ public static Line2D createPerpendicularLine( Line2D line, Point2D pt1, double size, Point2D opposingPoint) { double dx = line.getX2() - line.getX1(); double dy = line.getY2() - line.getY1(); double length = Math.sqrt(dx * dx + dy * dy); double pdx = dy / length; double pdy = -dx / length; int ccw = line.relativeCCW(opposingPoint); Point2D pt2 = new Point2D.Double(pt1.getX() - ccw * size * pdx, pt1.getY() - ccw * size * pdy); return new Line2D.Double(pt1, pt2); }
/** * Translate the terminal. This is implemented since it is the most efficient way for figures that * contain terminals to translate themselves. However, this method does not call repaint(), on the * assumption that the parent figure will do so anyway. */ @Override public void translate(double x, double y) { //// repaint(); _line.setLine(_line.getX1() + x, _line.getY1() + y, _line.getX2() + x, _line.getY2() + y); if (_end != null) { _end.translate(x, y); } //// repaint(); }
public Polyline2D(Line2D line) { super(); npoints = 2; xpoints = new double[2]; ypoints = new double[2]; xpoints[0] = line.getX1(); xpoints[1] = line.getX2(); ypoints[0] = line.getY1(); ypoints[1] = line.getY2(); calculatePath(); }
/** * 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) { 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); }
/** * Sets the parameters of this description object to match the supplied object. * * @param o the object (should be an instance of <code>Line2D</code>). * @throws ObjectFactoryException if the object is not an instance of <code>Line2D</code>. */ public void setParameterFromObject(final Object o) throws ObjectFactoryException { if (!(o instanceof Line2D)) { throw new ObjectFactoryException("The given object is no java.awt.geom.Line2D."); } final Line2D line = (Line2D) o; final float x1 = (float) line.getX1(); final float x2 = (float) line.getX2(); final float y1 = (float) line.getY1(); final float y2 = (float) line.getY2(); setParameter("x1", new Float(x1)); setParameter("x2", new Float(x2)); setParameter("y1", new Float(y1)); setParameter("y2", new Float(y2)); }
/** * 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); }
/** @see prefuse.render.Renderer#setBounds(prefuse.visual.VisualItem) */ @Override public void setBounds(VisualItem item) { if (!m_manageBounds) { return; } Shape shape = getShape(item); if (shape == null) { item.setBounds(item.getX(), item.getY(), 0, 0); } else if (shape == m_line) { GraphicsLib.setBounds(item, shape, getStroke(item)); } else { m_box.add(m_line.getX1(), m_line.getY1()); m_box.add(m_line.getX2(), m_line.getY2()); item.setBounds(m_box.getMinX(), m_box.getMinY(), m_box.getWidth(), m_box.getHeight()); } }
double length(Line2D l1) { return sqrt(pow(l1.getX1() - l1.getX2(), 2) + pow(l1.getY1() - l1.getY2(), 2)); }
/** * Determines whether a line segment is null. A line segment with length 0 is null, * * @param seg the line segment * @return <code>true</code> if the line segment is null, <code>false</code> if not */ public static boolean isNull(Line2D seg) { double dx = seg.getX2() - seg.getX1(); double dy = seg.getY2() - seg.getY1(); return mEpsSqr > dx * dx + dy * dy; }
void drawHisto(ROI2D roi, Graphics2D g, Sequence sequence, final IcyCanvas canvas) { if (!roiPairDict.containsKey(roi)) return; IntensityPaint ip = roiPairDict.get(roi); String currentValueX = ""; String currentValueV = ""; String maxValue = ""; String minValue = ""; for (int component = 0; component < sequence.getSizeC(); component++) { AffineTransform originalTransform = g.getTransform(); g.setColor(new Color(236, 10, 170)); if (sequence.getSizeC() != 1) { if (component == 0) g.setColor(Color.red); if (component == 1) g.setColor(Color.green); if (component == 2) g.setColor(Color.blue); } Rectangle2D rectBox = ((ROI2DRectangle) ip.displayRectangle).getRectangle(); Rectangle2D polyBox = ip.drawPolygon[component].getBounds2D(); try { if (ip.paintMode == PaintMode.line) { Line2D line = ((ROI2DLine) roi).getLine(); Point2D Lp; Point2D Rp; if (line.getX2() > line.getX1()) { Lp = line.getP1(); Rp = line.getP2(); } else { Lp = line.getP2(); Rp = line.getP1(); } int pos; if (Math.min(line.getX1(), line.getX2()) >= cursorPos.x) pos = 0; else if (Math.max(line.getX1(), line.getX2()) <= cursorPos.x) pos = ip.dataCount; else { pos = (int) ((cursorPos.x - Lp.getX()) / (Rp.getX() - Lp.getX()) * ip.dataCount); try { currentValueX = String.format("X:%.1f", cursorPos.x); currentValueV += String.format("%.1f ", ip.dataArr.get(component)[pos]); } catch (Exception e2) { } } ip.cursor1.setLine(pos, 0, pos, polyBox.getHeight()); } else { int pos = (int) cursorPos.z; ip.cursor1.setLine(pos, 0, pos, polyBox.getHeight()); try { currentValueX = String.format("Z:%.1f", cursorPos.z); currentValueV += String.format("%.1f ", ip.dataArr.get(component)[pos]); } catch (Exception e2) { } } double sx = rectBox.getWidth() / polyBox.getWidth(); double sy = rectBox.getHeight() / polyBox.getHeight(); if (sx < 100 && sy < 100) { g.translate(rectBox.getMinX(), rectBox.getMaxY()); g.scale(sx, -sy); g.draw(ip.drawPolygon[component]); g.setColor(new Color(100, 100, 170)); g.draw(ip.cursor1); g.setColor(new Color(236, 10, 170)); } else { char[] c = "Exceeding display limit!".toCharArray(); g.drawChars(c, 0, c.length, (int) rectBox.getCenterX() - 10, (int) rectBox.getCenterY()); } } finally { g.setTransform(originalTransform); // min,max double xStart, xEnd; if (ip.paintMode == PaintMode.line) { Line2D line = ((ROI2DLine) roi).getLine(); Point2D Lp; Point2D Rp; if (line.getX2() > line.getX1()) { Lp = line.getP1(); Rp = line.getP2(); } else { Lp = line.getP2(); Rp = line.getP1(); } xStart = Lp.getX(); xEnd = Rp.getX(); int pos; double yp; if (Math.min(line.getX1(), line.getX2()) >= cursorPos.x) { pos = (int) Lp.getX(); yp = Lp.getY(); } else if (Math.max(line.getX1(), line.getX2()) <= cursorPos.x) { pos = (int) Rp.getX(); yp = Rp.getY(); } else { pos = (int) cursorPos.x; yp = (cursorPos.x - Lp.getX()) / (line.getX2() - line.getX1()) * (line.getY2() - line.getY1()) + Lp.getY(); } ip.cursor2.setLine(pos, yp + 10, pos, yp - 10); g.draw(ip.cursor2); } else { xStart = 0; xEnd = ip.dataCount; } maxValue += String.format("%.1f ", ip.maxData[component]); minValue += String.format("%.1f ", ip.minData[component]); if (component == sequence.getSizeC() - 1) { char[] c = String.format("%.1f", xStart).toCharArray(); // x1 g.drawChars(c, 0, c.length, (int) rectBox.getMinX(), (int) rectBox.getMaxY() + 30); c = String.format("%.1f", xEnd).toCharArray(); // x2 g.drawChars(c, 0, c.length, (int) rectBox.getMaxX(), (int) rectBox.getMaxY() + 30); c = maxValue.toCharArray(); g.drawChars(c, 0, c.length, (int) rectBox.getMaxX() + 10, (int) rectBox.getMinY() + 10); c = minValue.toCharArray(); g.drawChars(c, 0, c.length, (int) rectBox.getMaxX() + 10, (int) rectBox.getMaxY() - 5); c = currentValueX.toCharArray(); g.drawChars( c, 0, c.length, (int) (rectBox.getMinX() + (ip.cursor1.x1 / ip.dataCount) * rectBox.getWidth()) - 20, (int) rectBox.getMaxY() + 15); c = currentValueV.toCharArray(); g.drawChars( c, 0, c.length, (int) (rectBox.getMinX() + (ip.cursor1.x1 / ip.dataCount) * rectBox.getWidth()) - 20, (int) rectBox.getMinY() - 5); } } } }
public void updateArray() { for (Shape shape : toDraw) { Line2D line = (Line2D) shape; line.setLine(line.getX1(), line.getY1() - 10, line.getX2(), line.getY2() - 10); } }
/** * Returns the angle of rotation (in radians) for the specified line. * * @param line the line ({@code null} not permitted). * @return The angle of rotation (in radians). */ public static double calculateTheta(Line2D line) { double dx = line.getX2() - line.getX1(); double dy = line.getY2() - line.getY1(); return Math.atan2(dy, dx); }
public Shape getShape() { Line2D line = getConnectionPoints(); Point2D control = getControlPoint(); GeneralPath p = new GeneralPath(); p.moveTo((float) line.getX1(), (float) line.getY1()); p.quadTo( (float) control.getX(), (float) control.getY(), (float) line.getX2(), (float) line.getY2()); return p; }
public double getStartAngle() { final Line2D tan = getStartTangeante(); final double theta1 = Math.atan2(tan.getY2() - tan.getY1(), tan.getX2() - tan.getX1()); return theta1; }
/** * Returns the length of the line. * * @param line the line ({@code null} not permitted). * @return The length of the line. */ public static double length(Line2D line) { double dx = line.getX2() - line.getX1(); double dy = line.getY2() - line.getY1(); return Math.sqrt(dx * dx + dy * dy); }
/** * Calculates the intersection point between two line(segment)s. * * @param any the 1st line(segment) * @param unbounded <code>true</code> if <code>any</code> is a line, <code>false</code> if <code> * any</code> is a segment * @param any2 the 2nd line(segment) * @param unbounded2 <code>true</code> if <code>any2</code> is a line, <code>false</code> if * <code>any2</code> is a segment * @param pntDst the intersection point * @return the intersection point * @exception NoIntersectionException if the geometries do not intersect */ public static Point2D intersection( Line2D any, boolean unbounded, Line2D any2, boolean unbounded2, Point2D pntDst) throws NoIntersectionException { // Create point object if necessary if (pntDst == null) { pntDst = new Point2D.Double(); } // Bounds check if (!unbounded && !unbounded2) { _boundsCheck(any, any2); } // Segments with coincident endpoints? if (!unbounded && !unbounded2) { Point2D[] pa = new Point2D[2]; pa[0] = any.getP1(); pa[1] = any.getP2(); Point2D[] pb = new Point2D[2]; pb[0] = any2.getP1(); pb[1] = any2.getP2(); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { double dx = pb[j].getX() - pa[i].getX(); double dy = pb[j].getY() - pa[i].getY(); if ((dx * dx + dy * dy) < getEpsSqr()) { Vector2D va = new Vector2D(pa[i], pa[(i + 1) % 2]); Vector2D vb = new Vector2D(pb[j], pb[(j + 1) % 2]); try { double ang = va.getAngleSmallest(vb); if (ang > getEps()) { pntDst.setLocation(pa[i]); return pntDst; } } catch (NullVectorException e) { pntDst.setLocation(pa[i]); return pntDst; } } } } } // Linear equation double vx1 = any.getX2() - any.getX1(); double vy1 = any.getY2() - any.getY1(); double b1 = -vx1; double c1 = vx1 * any.getY1() - vy1 * any.getX1(); double vx2 = any2.getX2() - any2.getX1(); double vy2 = any2.getY2() - any2.getY1(); double b2 = -vx2; double c2 = vx2 * any2.getY1() - vy2 * any2.getX1(); // Solve equation system double d = vy1 * b2 - vy2 * b1; if (Math.abs(d) <= mEps) { throw new NoIntersectionException("Lines are parallel"); } double dx = b1 * c2 - b2 * c1; double dy = c1 * vy2 - c2 * vy1; pntDst.setLocation(dx / d, dy / d); // Do the line segments contain the intersection point? if (!unbounded && !containment(any, pntDst)) { throw new NoIntersectionException("1st line segment doesn't contain intersection point"); } if (!unbounded2 && !containment(any2, pntDst)) { throw new NoIntersectionException("2nd line segment doesn't contain intersection point"); } // Return the intersection point return pntDst; }