Example #1
0
 final double getMindist(Point2D.Double pt) {
   double result = Double.MAX_VALUE;
   for (Point2D p : points.keySet()) {
     if (pt.equals(p)) {
       continue;
     }
     final double v = p.distance(pt);
     if (v < 1E-4) {
       throw new IllegalStateException();
     }
     result = Math.min(result, v);
   }
   for (Line2D line : lines) {
     if (line.getP1().equals(pt) || line.getP2().equals(pt)) {
       continue;
     }
     final double v = line.ptSegDist(pt);
     if (result < 1E-4) {
       throw new IllegalStateException("pt=" + pt + " line=" + GeomUtils.toString(line));
     }
     result = Math.min(result, v);
   }
   if (result == 0) {
     throw new IllegalStateException();
   }
   // Log.println("getMindist=" + result);
   return result;
 }
Example #2
0
  public Point2D getClosestIntersectionToStart(Shape withThisShape) {
    // first, get the sides of the shape
    List<LineSegment> shapeSides = Utilities.getSides(withThisShape);

    // go through all the sides, and see if there are any intersections
    // there may be more than 1
    List<Point2D> shapeIntersectionPoints = new ArrayList<Point2D>();

    for (Line2D curSide : shapeSides) {
      if (curSide.intersectsLine(this)) {
        Point2D intersectionPoint = Utilities.getIntersectionPoint(this, curSide);
        shapeIntersectionPoints.add(intersectionPoint);
      }
    }

    if (shapeIntersectionPoints.size() <= 0) {
      return null;
    } else if (shapeIntersectionPoints.size() == 1) {
      return shapeIntersectionPoints.get(0);
    } else {
      // get the point closest to P1
      Point2D closestIntersectionPoint = null;
      double closestIntersectionPointDistSq = java.lang.Double.MAX_VALUE;
      for (Point2D curPoint : shapeIntersectionPoints) {
        double curDistSq = curPoint.distanceSq(this.getP1());
        if (curDistSq < closestIntersectionPointDistSq) {
          closestIntersectionPointDistSq = curDistSq;
          closestIntersectionPoint = curPoint;
        }
      }
      return closestIntersectionPoint;
    }
  }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /** @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);
   }
 }
    /** @see prefuse.render.AbstractShapeRenderer#getRawShape(prefuse.visual.VisualItem) */
    @Override
    protected Shape getRawShape(VisualItem item) {
      double x1 = item.getDouble(VisualItem.X);
      double y1 = item.getDouble(VisualItem.Y);
      double x2 = item.getDouble(VisualItem.X2);
      double y2 = item.getDouble(VisualItem.Y2);
      boolean isX = item.getBoolean(DocumentGridAxisLayout.IS_X);
      double midPoint = item.getDouble(DocumentGridAxisLayout.MID_POINT);
      // horizontal or vertical coords should be manually held constant so that fisheye works
      // properly
      if (isX) {
        // vertical line
        m_line.setLine(x1, y1, x1, y2);
      } else {
        // horizontal line
        m_line.setLine(x1, y1, x2, y1);
      }

      if (!item.canGetString(VisualItem.LABEL)) {
        return m_line;
      }

      String label = item.getString(VisualItem.LABEL);
      if (label == null) {
        return m_line;
      }

      FontMetrics fm = DEFAULT_GRAPHICS.getFontMetrics(item.getFont());
      m_ascent = fm.getAscent();
      int h = fm.getHeight();
      int w = fm.stringWidth(label);

      double tx, ty;

      int labelOffset = 10;
      if (isX) {
        // vertical axis
        // get text x-coord, center at midPoint
        //            tx = x1 + (x2-x1)/2 - w/2;
        //            tx = midPoint + (x1+midPoint)/2 - w/2;
        //            tx = x1 + midPoint/2 - w/2;
        // simpler approach: just add a fixed distance
        tx = x1 + labelOffset;
        // get text y-coord
        ty = y2 - h;
      } else {
        // horiz axis
        // get text x-coord
        tx = x1 - w - 2;
        // get text y-coord, center at midPoint
        //            ty = y1 + (y2-y1)/2 - h/2;
        //            ty = y1 + midPoint/2 - h/2;
        // simpler approach: just add a fixed distance
        ty = y1 + labelOffset;
      }

      m_box.setFrame(tx, ty, w, h);
      return m_box;
    }
Example #6
0
 public void pruneArray() {
   for (Shape shape : toDraw) {
     Line2D line = (Line2D) shape;
     if (line.getY1() < 0) {
       toDraw.remove(shape);
     }
   }
 }
 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;
 }
  /**
   * Creates an object based on this description.
   *
   * @return The object.
   */
  public Object createObject() {
    final Line2D line = new Line2D.Float();

    final float x1 = getFloatParameter("x1");
    final float x2 = getFloatParameter("x2");
    final float y1 = getFloatParameter("y1");
    final float y2 = getFloatParameter("y2");
    line.setLine(x1, y1, x2, y2);
    return line;
  }
  /**
   * 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();
  }
Example #10
0
 /**
  * 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);
 }
Example #11
0
  /**
   * Tests if the shape contains a point.
   *
   * @param p coord point
   * @return true if shape contains p
   */
  public boolean contains(Point2D p) {
    double x = startPoint.getX();
    double y = startPoint.getY();

    double xEnd = endPoint.getX();
    double yEnd = endPoint.getY();

    Line2D line = new Line2D.Double(x, y, xEnd, yEnd);

    return line.intersects(p.getX(), p.getY(), 10, 10);
  }
 /**
  * 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);
 }
Example #13
0
 public boolean isZaznaczono(int x, int y) {
   for (Line2D l : this.linia) {
     if (l.intersects(
         x - SASIEDZTWO_BADANIA_PRZECIECIA,
         y - SASIEDZTWO_BADANIA_PRZECIECIA,
         SASIEDZTWO_BADANIA_PRZECIECIA * 2,
         SASIEDZTWO_BADANIA_PRZECIECIA * 2)) {
       return true;
     }
   }
   return false;
 }
  private Edge getSelectedEdge(MouseEvent mouse) {
    for (int i = 0; i < listOfNodes.size(); i++) {
      Node node = listOfNodes.get(i);
      for (Edge edge : node.adjacent) {
        Line2D line = edge.getRepLine();
        double ptToLine = line.ptSegDist(mouse.getX(), mouse.getY());

        if (ptToLine <= Constants.eps) {
          return edge;
        }
      }
    }
    return null;
  }
  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;
      }
    }
  }
Example #16
0
  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();
  }
Example #17
0
 public boolean nearby(Node n, double dist) {
   if (w == null) {
     Main.debug("way null");
     return false;
   }
   if (w.containsNode(n)) return false;
   if (n.isKeyTrue("noexit")) return false;
   EastNorth coord = n.getEastNorth();
   if (coord == null) return false;
   Point2D p = new Point2D.Double(coord.east(), coord.north());
   if (line.getP1().distance(p) > len + dist) return false;
   if (line.getP2().distance(p) > len + dist) return false;
   return line.ptSegDist(p) < dist;
 }
Example #18
0
 /**
  * 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);
 }
Example #19
0
 /**
  * Compares two lines are returns <code>true</code> if they are equal or both <code>null</code>.
  *
  * @param l1 the first line (<code>null</code> permitted).
  * @param l2 the second line (<code>null</code> permitted).
  * @return A boolean.
  */
 public static boolean equal(final Line2D l1, final Line2D l2) {
   if (l1 == null) {
     return (l2 == null);
   }
   if (l2 == null) {
     return false;
   }
   if (!l1.getP1().equals(l2.getP1())) {
     return false;
   }
   if (!l1.getP2().equals(l2.getP2())) {
     return false;
   }
   return true;
 }
  /**
   * 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));
  }
Example #21
0
 /**
  * A utility method that draws a line but only if none of the coordinates are NaN values.
  *
  * @param g2 the graphics target.
  * @param line the line object.
  * @param x0 the x-coordinate for the starting point of the line.
  * @param y0 the y-coordinate for the starting point of the line.
  * @param x1 the x-coordinate for the ending point of the line.
  * @param y1 the y-coordinate for the ending point of the line.
  */
 private void drawLine(Graphics2D g2, Line2D line, double x0, double y0, double x1, double y1) {
   if (Double.isNaN(x0) || Double.isNaN(x1) || Double.isNaN(y0) || Double.isNaN(y1)) {
     return;
   }
   line.setLine(x0, y0, x1, y1);
   g2.draw(line);
 }
Example #22
0
  private double calculateDistance(Node node1, Node node2) {

    double totalDistance = 0;
    double node1x = node1.getX();
    double node1y = node1.getX();
    double node2x = node2.getX();
    double node2y = node2.getX();

    for (double[][][] RADAR_LINE : RADARS) {

      for (double[][] LINE : RADAR_LINE) {

        if (Line2D.linesIntersect(
            LINE[POINT_1][X],
            LINE[POINT_1][Y],
            LINE[POINT_2][X],
            LINE[POINT_2][Y],
            node1x,
            node1y,
            node2x,
            node2y)) {

          totalDistance += FEE_IN_RADAR_RANGE;
        }
      }
    }

    double distanceX = (node1.getX() - node2.getX());
    double distanceY = (node1.getY() - node2.getY());
    double distance =
        Math.sqrt(distanceX * distanceX + distanceY * distanceY) * FEE_DISTANCE_FACTOR;

    return distance + totalDistance;
  }
Example #23
0
  /**
   * Finds the intersection of two line segments
   *
   * @return EastNorth null if no intersection was found, the EastNorth coordinates of the
   *     intersection otherwise
   */
  public static EastNorth getSegmentSegmentIntersection(
      EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4) {
    double x1 = p1.getX();
    double y1 = p1.getY();
    double x2 = p2.getX();
    double y2 = p2.getY();
    double x3 = p3.getX();
    double y3 = p3.getY();
    double x4 = p4.getX();
    double y4 = p4.getY();

    // TODO: do this locally.
    if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null;

    // Convert line from (point, point) form to ax+by=c
    double a1 = y2 - y1;
    double b1 = x1 - x2;
    double c1 = x2 * y1 - x1 * y2;

    double a2 = y4 - y3;
    double b2 = x3 - x4;
    double c2 = x4 * y3 - x3 * y4;

    // Solve the equations
    double det = a1 * b2 - a2 * b1;
    if (det == 0) return null; // Lines are parallel

    double x = (b1 * c2 - b2 * c1) / det;
    double y = (a2 * c1 - a1 * c2) / det;

    return new EastNorth(x, y);
  }
Example #24
0
 /** @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());
   }
 }
Example #25
0
  private static Map<SgroupBracket, IBond> bracketBondPairs(
      Collection<SgroupBracket> brackets, Collection<IBond> bonds) {
    Map<SgroupBracket, IBond> pairs = new HashMap<>();

    for (SgroupBracket bracket : brackets) {
      IBond crossingBond = null;
      for (IBond bond : bonds) {
        IAtom a1 = bond.getAtom(0);
        IAtom a2 = bond.getAtom(1);
        if (Line2D.linesIntersect(
            bracket.getFirstPoint().x, bracket.getFirstPoint().y,
            bracket.getSecondPoint().x, bracket.getSecondPoint().y,
            a1.getPoint2d().x, a1.getPoint2d().y,
            a2.getPoint2d().x, a2.getPoint2d().y)) {
          // more than one... not good
          if (crossingBond != null) return new HashMap<>();
          crossingBond = bond;
        }
      }
      if (crossingBond == null) return new HashMap<>();
      pairs.put(bracket, crossingBond);
    }

    return pairs;
  }
Example #26
0
 /**
  * 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);
 }
  /**
   * Draws the tick marks. This method is called during chart rendering, you normally would not call
   * this method yourself.
   *
   * @param g2 the graphics target ({@code null} not permitted)
   * @param cursor the current offset from the edge of the dataArea
   * @param dataArea the area used for plotting data ({@code null} not permitted)
   * @param edge the location of the axis ({@code null} not permitted)
   * @param state axis state information ({@code null} not permitted)
   * @since 1.0.13
   */
  public void drawTickMarks(
      Graphics2D g2, double cursor, Rectangle2D dataArea, RectangleEdge edge, AxisState state) {

    Plot p = getPlot();
    if (p == null) {
      return;
    }
    CategoryPlot plot = (CategoryPlot) p;
    double il = getTickMarkInsideLength();
    double ol = getTickMarkOutsideLength();
    Line2D line = new Line2D.Double();
    List<Comparable> categories = plot.getCategoriesForAxis(this);
    g2.setPaint(getTickMarkPaint());
    g2.setStroke(getTickMarkStroke());
    if (edge.equals(RectangleEdge.TOP)) {
      for (Comparable key : categories) {
        double x = getCategoryMiddle(key, categories, dataArea, edge);
        line.setLine(x, cursor, x, cursor + il);
        g2.draw(line);
        line.setLine(x, cursor, x, cursor - ol);
        g2.draw(line);
      }
      state.cursorUp(ol);
    } else if (edge.equals(RectangleEdge.BOTTOM)) {
      for (Comparable key : categories) {
        double x = getCategoryMiddle(key, categories, dataArea, edge);
        line.setLine(x, cursor, x, cursor - il);
        g2.draw(line);
        line.setLine(x, cursor, x, cursor + ol);
        g2.draw(line);
      }
      state.cursorDown(ol);
    } else if (edge.equals(RectangleEdge.LEFT)) {
      for (Comparable key : categories) {
        double y = getCategoryMiddle(key, categories, dataArea, edge);
        line.setLine(cursor, y, cursor + il, y);
        g2.draw(line);
        line.setLine(cursor, y, cursor - ol, y);
        g2.draw(line);
      }
      state.cursorLeft(ol);
    } else if (edge.equals(RectangleEdge.RIGHT)) {
      for (Comparable key : categories) {
        double y = getCategoryMiddle(key, categories, dataArea, edge);
        line.setLine(cursor, y, cursor - il, y);
        g2.draw(line);
        line.setLine(cursor, y, cursor + ol, y);
        g2.draw(line);
      }
      state.cursorRight(ol);
    }
  }
Example #28
0
 MyWaySegment(Way w, Node n1, Node n2) {
   this.w = w;
   String railway = w.get("railway");
   String highway = w.get("highway");
   this.isAbandoned = "abandoned".equals(railway) || w.isKeyTrue("disused");
   this.highway = (highway != null || railway != null) && !isAbandoned;
   this.isBoundary = !this.highway && "administrative".equals(w.get("boundary"));
   line =
       new Line2D.Double(
           n1.getEastNorth().east(),
           n1.getEastNorth().north(),
           n2.getEastNorth().east(),
           n2.getEastNorth().north());
   len = line.getP1().distance(line.getP2());
   this.n1 = n1;
   this.n2 = n2;
 }
Example #29
0
 public LinkedList<LineWorldObject> getLineWorldObjectsOnLine(Line2D line, double radius) {
   LinkedList<LineWorldObject> objectsInSelection = new LinkedList<LineWorldObject>();
   for (LineWorldObject o : lineObjects) {
     if (o.getXInCM() == o.getX2InCM()) {
       if (line.intersectsLine(
           o.getXInCM(), o.getYInCM() - radius, o.getX2InCM(), o.getY2InCM() + radius)) {
         objectsInSelection.add(o);
       }
     } else {
       if (line.intersectsLine(
           o.getXInCM() - radius, o.getYInCM(), o.getX2InCM() + radius, o.getY2InCM())) {
         objectsInSelection.add(o);
       }
     }
   }
   return objectsInSelection;
 }
Example #30
0
 @Override
 public void mouseMoved(MouseEvent e) {
   Point p = e.getPoint();
   if (line.ptSegDist(p) <= 1.0) {
     tipLabel.setText(tooltip);
   }
   parent.repaint();
 }