Example #1
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;
 }
Example #2
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 #3
0
  /**
   * Calculates the relative position between a line and a cubic curve. If the line is degenerated
   * (coincident start and end point) then -1 is returned if the line is outside the bounding box of
   * the cubic curve.
   *
   * @param lin the line
   * @param cub the cubic curve
   * @return the position of the curve relative to the line -1: curve is left of line +1: curve is
   *     right of line 0: curve (eventually) intersects the line
   */
  private static int _relativePosition(Line2D lin, CubicCurve2D cub) {
    // Is the line degenerated?
    if (isNull(lin)) {
      Rectangle2D rct = cub.getBounds2D();
      double x = lin.getX1() - 0.5 * mEps;
      double y = lin.getY1() - 0.5 * mEps;
      return rct.intersects(x, y, mEps, mEps) ? 0 : -1;
    }

    // Create a normalized vector perpendicular to the line
    final Vector2D vec1Left = new Vector2D(lin.getP1(), lin.getP2());
    vec1Left.left();
    vec1Left.normalize();

    // All four points/ control points must be left or right the line
    final Vector2D vec = new Vector2D();

    // P1
    vec.setLocation(lin.getP1(), cub.getP1());
    int ii = _relativePosition(vec1Left, vec);
    if (ii == 0) {
      return 0;
    }

    // P2
    vec.setLocation(lin.getP1(), cub.getP2());
    int i = _relativePosition(vec1Left, vec);
    if (i == 0 || i + ii == 0) {
      return 0;
    }

    // CtrlP1
    vec.setLocation(lin.getP1(), cub.getCtrlP1());
    i = _relativePosition(vec1Left, vec);
    if (i == 0 || i + ii == 0) {
      return 0;
    }

    // CtrlP2
    vec.setLocation(lin.getP1(), cub.getCtrlP2());
    i = _relativePosition(vec1Left, vec);
    if (i == 0 || i + ii == 0) {
      return 0;
    }

    return ii;
  }
 /** {@inheritDoc} */
 protected void startDragSegment(double xcoord, double ycoord) {
   copyConnectionPointsToEditPoints();
   editedSegment = getConnection().getSegmentAtPoint(xcoord, ycoord);
   if (editedSegment != null) {
     anchor = new Point2D.Double(xcoord, ycoord);
     dragIndex = getConnection().getPoints().indexOf(editedSegment.getP1());
     setIsDragSegment(true);
   }
 }
Example #5
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 #6
0
  /**
   * Compute the point intersecting the lines given. Return Point(-1.0f, -1.0f) if liens are //.
   *
   * @param line1 the first line
   * @param line2 the second line
   * @return the intersection point of the two lines
   */
  public static Point2D ptIntersectsLines(Line2D line1, Line2D line2) {
    // convert line2D to point
    final Point p1 = new Point((int) line1.getP1().getX(), (int) line1.getP1().getY());
    final Point p2 = new Point((int) line1.getP2().getX(), (int) line1.getP2().getY());
    final Point p3 = new Point((int) line2.getP1().getX(), (int) line2.getP1().getY());
    final Point p4 = new Point((int) line2.getP2().getX(), (int) line2.getP2().getY());

    // compute intersection point between two line
    // (http://en.wikipedia.org/wiki/Line-line_intersection)
    final int denom = (p1.x - p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x - p4.x);

    // no intersection (lines //)
    if (denom == 0) return new Point2D.Float(-1.0f, -1.0f);

    final int x =
        ((p1.x * p2.y - p1.y * p2.x) * (p3.x - p4.x) - (p1.x - p2.x) * (p3.x * p4.y - p3.y * p4.x))
            / denom;
    final int y =
        ((p1.x * p2.y - p1.y * p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x * p4.y - p3.y * p4.x))
            / denom;

    return new Point2D.Float(x, y);
  }
Example #7
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;
 }
  protected void updateContactPoints() {
    Line2D connectionPoints = getConnectionPoints();

    Point2D startingPoint = connectionPoints.getP1();
    Point2D endingPoint = connectionPoints.getP2();

    if (getStartNode().equals(getEndNode())) {
      Rectangle2D nodeBounds = getStartNode().getBounds();
      Point2D nodeLocation = getStartNode().getLocationOnGraph();
      contactPoints = new Point2D[5];
      contactPoints[0] =
          new Point2D.Double(
              nodeLocation.getX() + nodeBounds.getWidth(),
              nodeLocation.getY() + nodeBounds.getHeight() / 2);
      contactPoints[1] =
          new Point2D.Double(contactPoints[0].getX() + SELF_LOOP_GAP_X, contactPoints[0].getY());
      contactPoints[2] =
          new Point2D.Double(
              contactPoints[1].getX(),
              nodeLocation.getY() + nodeBounds.getHeight() + SELF_LOOP_GAP_Y);
      contactPoints[3] =
          new Point2D.Double(
              contactPoints[0].getX() - nodeBounds.getWidth() / 2, contactPoints[2].getY());
      contactPoints[4] =
          new Point2D.Double(contactPoints[3].getX(), contactPoints[2].getY() - SELF_LOOP_GAP_Y);
    } else {
      List<Point2D> points = new ArrayList<Point2D>();

      points.add(startingPoint);
      points.addAll(Arrays.asList(getTransitionPoints()));
      points.add(endingPoint);

      Point2D[] bentStylePointsAsArray = points.toArray(new Point2D[points.size()]);
      points = getBentStyle().getPath(bentStylePointsAsArray);
      contactPoints = new Point2D[points.size()];
      points.toArray(contactPoints);
    }
  }
Example #9
0
 public Vector(Line2D l) {
   this(l.getP1(), l.getP2());
 }
 /**
  * Describe what the method does
  *
  * @todo-javadoc Write javadocs for method
  * @todo-javadoc Write javadocs for method parameter
  * @todo-javadoc Write javadocs for method parameter
  * @param g Describe what the parameter does
  * @param line Describe what the parameter does
  */
 private final void drawLineUnlessItsLengthIsZero(final Graphics2D g, final Line2D line) {
   // Any coord is NaN if the length is zero
   if (!Double.isNaN(line.getP1().getX())) {
     g.draw(line);
   }
 }
  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 computeData() {
      computing = true;
      cancelCompute = false;
      try {
        maxData = new double[sequence.getSizeC()];
        minData = new double[sequence.getSizeC()];
        drawPolygon = new Polygon[sequence.getSizeC()];

        if (paintMode == PaintMode.line) {
          Line2D line = ((ROI2DLine) guideRoi).getLine();
          dataCount = (int) line.getP1().distance(line.getP2());
        } else {
          dataCount = sequence.getSizeZ();
        }
        dataArr = new ArrayList<double[]>();

        for (int component = 0; component < sequence.getSizeC(); component++) {
          double[] data = new double[dataCount];
          dataArr.add(data);
        }

        if (paintMode == PaintMode.line) {
          Line2D line = ((ROI2DLine) guideRoi).getLine();
          dataCount = (int) line.getP1().distance(line.getP2());

          ShapeUtil.consumeShapeFromPath(
              ((ROI2DShape) guideRoi).getPathIterator(null),
              new ShapeConsumer() {
                @Override
                public boolean consume(Shape shape) {
                  if (shape instanceof Line2D) {

                    Line2D line = (Line2D) shape;
                    Point2D Lp;
                    Point2D Rp;
                    if (line.getX2() > line.getX1()) {
                      Lp = line.getP1();
                      Rp = line.getP2();
                    } else {
                      Lp = line.getP2();
                      Rp = line.getP1();
                    }

                    for (int component = 0; component < sequence.getSizeC(); component++) {
                      // create histo data
                      int distance = dataCount;

                      double vx = (Rp.getX() - Lp.getX()) / distance;
                      double vy = (Rp.getY() - Lp.getY()) / distance;

                      double[] data = dataArr.get(component);

                      double x = Lp.getX();
                      double y = Lp.getY();
                      IcyBufferedImage image = canvas.getCurrentImage();

                      if (image.isInside((int) x, (int) y)) {
                        maxData[component] =
                            Array1DUtil.getValue(
                                image.getDataXY(component),
                                image.getOffset((int) x, (int) y),
                                image.isSignedDataType());
                      } else {
                        maxData[component] = 0;
                      }
                      minData[component] = maxData[component];

                      for (int i = 0; i < dataCount; i++) {
                        if (cancelCompute) break;
                        if (image.isInside((int) x, (int) y)) {
                          data[i] =
                              Array1DUtil.getValue(
                                  image.getDataXY(component),
                                  image.getOffset((int) x, (int) y),
                                  image.isSignedDataType());
                        } else {
                          data[i] = 0;
                        }
                        if (data[i] > maxData[component]) maxData[component] = data[i];
                        if (data[i] < minData[component]) minData[component] = data[i];
                        x += vx;
                        y += vy;
                      }
                      Polygon polygon = new Polygon();
                      polygon.addPoint(0, 0);
                      for (int i = 0; i < dataCount; i++) {
                        polygon.addPoint(i, (int) (data[i] - minData[component]));
                      }
                      polygon.addPoint(dataCount, 0);
                      drawPolygon[component] = polygon;
                    }
                  }
                  return true; // continue
                }
              });

        } else {

          for (int component = 0; component < sequence.getSizeC(); component++) {
            double[] data = dataArr.get(component);

            if (paintMode == PaintMode.point) {
              Point p = guideRoi.getPosition();
              if (p.x < sequence.getSizeX() && p.y < sequence.getSizeY()) {
                maxData[component] = sequence.getData(0, 0, component, p.y, p.x);
                minData[component] = maxData[component];
                for (int i = 0; i < dataCount; i++) {
                  if (cancelCompute) break;
                  data[i] = sequence.getData(0, i, component, p.y, p.x);
                  if (data[i] > maxData[component]) maxData[component] = data[i];
                  if (data[i] < minData[component]) minData[component] = data[i];
                }
              }
            } else {
              maxData[component] = ROIUtil.getMeanIntensity(sequence, guideRoi, 0, -1, component);
              ;
              minData[component] = maxData[component];
              for (int i = 0; i < dataCount; i++) {
                if (cancelCompute) break;
                data[i] = ROIUtil.getMeanIntensity(sequence, guideRoi, i, -1, component);
                if (data[i] > maxData[component]) maxData[component] = data[i];
                if (data[i] < minData[component]) minData[component] = data[i];
              }
            }
            Polygon polygon = new Polygon();

            polygon.addPoint(0, 0);
            for (int i = 0; i < dataCount; i++)
              // pity polygon does not support this with double...
              polygon.addPoint(i, (int) (data[i] - minData[component]));
            polygon.addPoint(dataCount, 0);
            drawPolygon[component] = polygon;
          }
        }
      } catch (Exception e) {
        System.out.print(e);
      }
      computing = false;
    }
Example #13
0
 public org.geogebra.common.awt.GPoint2D getP1() {
   java.awt.geom.Point2D p1 = impl.getP1();
   if (p1 == null) return null;
   return new GPoint2DD(p1.getX(), p1.getY());
 }
Example #14
0
  /**
   * 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;
  }