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

The following examples show how to use java.awt.geom.Line2D#ptLineDist() . 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: FreeConnectionWidget.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Adds or removes a control point on a specified location
 * @param localLocation the local location
 */
public void addRemoveControlPoint (Point localLocation) {
    ArrayList<Point> list = new ArrayList<Point> (getControlPoints());
        if(!removeControlPoint(localLocation,list,deleteSensitivity)){
            Point exPoint=null;int index=0;
            for (Point elem : list) {
                if(exPoint!=null){
                    Line2D l2d=new Line2D.Double(exPoint,elem);
                    if(l2d.ptLineDist(localLocation)<createSensitivity){
                        list.add(index,localLocation);
                        break;
                    }
                }
                exPoint=elem;index++;
            }
        }
        setControlPoints(list,false);
}
 
Example 2
Source File: JSoftGraph.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static double dist(double x1, double y1,
                          double x2, double y2,
                          double mx, double my) {
  double _x1 = Math.min(x1, x2);
  double _x2 = Math.max(x1, x2);
  double _y1 = Math.min(y1, y2);
  double _y2 = Math.max(y1, y2);
  if(mx >= _x1 && mx <= _x2 &&
     my >= _y1 && my <= _y2) {
    return Line2D.ptLineDist(x1, y1, x2, y2, mx, my);
  }
  return Double.MAX_VALUE;
}
 
Example 3
Source File: DotPattern.java    From libreveris with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Lookup for a TextLine that embraces the provided glyph.
 *
 * @param the (dot) glyph to check
 * @return glyph the embracing line if any, otherwise null
 */
private TextLine getEmbracingLine (Glyph glyph)
{
    Rectangle glyphBox = glyph.getBounds();

    for (TextLine sentence : system.getSentences()) {
        Line2D baseline = sentence.getBaseline();

        // Check in abscissa: not before sentence beginning
        if ((glyphBox.x + glyphBox.width) <= baseline.getX1()) {
            continue;
        }

        // Check in abscissa: not too far after sentence end
        if ((glyphBox.x - baseline.getX2()) > maxLineDx) {
            continue;
        }

        // Check in abscissa: not overlapping any sentence word
        for (TextWord word : sentence.getWords()) {
            if (word.getBounds()
                    .intersects(glyphBox)) {
                continue;
            }
        }

        // Check in ordinate: distance from baseline
        double dy = baseline.ptLineDist(glyph.getAreaCenter());

        if (dy > maxLineDy) {
            continue;
        }

        // This line is OK, take it
        return sentence;
    }

    // Nothing found
    return null;
}
 
Example 4
Source File: BeamsBuilder.java    From audiveris with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Look for a compatible beam inter next to the provided one (in a same beam line).
 * They either can be merged or give a limit to other extension modes.
 *
 * @param beam     the provided beam
 * @param side     which side to look on
 * @param maxGapDx max gap width between the two beams, or default value if null
 * @return the sibling beam found if any
 */
private AbstractBeamInter getSideBeam (AbstractBeamInter beam,
                                       final HorizontalSide side,
                                       Double maxGapDx)
{
    Area luArea = (maxGapDx != null) ? sideAreaOf(null, beam, side, 0, maxGapDx, 0)
            : sideAreaOf("-", beam, side, 0, params.maxSideBeamDx, 0);

    List<Inter> others = Inters.intersectedInters(rawSystemBeams, GeoOrder.NONE, luArea);
    others.remove(beam); // Safer

    if (!others.isEmpty()) {
        // Use a closer look, using colinearity
        final Line2D median = beam.getMedian();
        final Point2D endPt = (side == LEFT) ? median.getP1() : median.getP2();
        final double slope = LineUtil.getSlope(median);

        for (Iterator<Inter> it = others.iterator(); it.hasNext();) {
            AbstractBeamInter other = (AbstractBeamInter) it.next();

            // Check connection point & beam slopes are OK
            Line2D otherMedian = other.getMedian();

            if ((Math.abs(LineUtil.getSlope(otherMedian) - slope) > params.maxBeamSlopeGap)
                        || (otherMedian.ptLineDist(endPt) > params.maxBeamsGapY)) {
                it.remove();
            }
        }

        // Keep just the closest one to current beam (abscissa-wise)
        if (others.size() > 1) {
            final double endX = endPt.getX();
            Collections.sort(others, new Comparator<Inter>()
                     {
                         @Override
                         public int compare (Inter o1,
                                             Inter o2)
                         {
                             AbstractBeamInter b1 = (AbstractBeamInter) o1;
                             AbstractBeamInter b2 = (AbstractBeamInter) o2;

                             if (side == LEFT) {
                                 return Double.compare(
                                         endX - b1.getMedian().getX2(),
                                         endX - b2.getMedian().getX2());
                             } else {
                                 return Double.compare(
                                         b1.getMedian().getX1() - endX,
                                         b2.getMedian().getX1() - endX);
                             }
                         }
                     });
        }

        if (!others.isEmpty()) {
            return (AbstractBeamInter) others.get(0);
        }
    }

    return null;
}
 
Example 5
Source File: mxGraphView.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the relative point that describes the given, absolute label
 * position for the given edge state.
 */
public mxPoint getRelativePoint(mxCellState edgeState, double x, double y) {
  mxIGraphModel model = graph.getModel();
  mxGeometry geometry = model.getGeometry(edgeState.getCell());

  if (geometry != null) {
    int pointCount = edgeState.getAbsolutePointCount();

    if (geometry.isRelative() && pointCount > 1) {
      double totalLength = edgeState.getLength();
      double[] segments = edgeState.getSegments();

      // Works which line segment the point of the label is closest to
      mxPoint p0 = edgeState.getAbsolutePoint(0);
      mxPoint pe = edgeState.getAbsolutePoint(1);
      Line2D line = new Line2D.Double(p0.getPoint(), pe.getPoint());
      double minDist = line.ptSegDistSq(x, y);

      int index = 0;
      double tmp = 0;
      double length = 0;

      for (int i = 2; i < pointCount; i++) {
        tmp += segments[i - 2];
        pe = edgeState.getAbsolutePoint(i);

        line = new Line2D.Double(p0.getPoint(), pe.getPoint());
        double dist = line.ptSegDistSq(x, y);

        if (dist < minDist) {
          minDist = dist;
          index = i - 1;
          length = tmp;
        }

        p0 = pe;
      }

      double seg = segments[index];
      p0 = edgeState.getAbsolutePoint(index);
      pe = edgeState.getAbsolutePoint(index + 1);

      double x2 = p0.getX();
      double y2 = p0.getY();

      double x1 = pe.getX();
      double y1 = pe.getY();

      double px = x;
      double py = y;

      double xSegment = x2 - x1;
      double ySegment = y2 - y1;

      px -= x1;
      py -= y1;
      double projlenSq = 0;

      px = xSegment - px;
      py = ySegment - py;
      double dotprod = px * xSegment + py * ySegment;

      if (dotprod <= 0.0) {
        projlenSq = 0;
      }
      else {
        projlenSq = dotprod * dotprod / (xSegment * xSegment + ySegment * ySegment);
      }

      double projlen = Math.sqrt(projlenSq);

      if (projlen > seg) {
        projlen = seg;
      }

      double yDistance = Line2D.ptLineDist(p0.getX(), p0.getY(), pe.getX(), pe.getY(), x, y);
      int direction = Line2D.relativeCCW(p0.getX(), p0.getY(), pe.getX(), pe.getY(), x, y);

      if (direction == -1) {
        yDistance = -yDistance;
      }

      // Constructs the relative point for the label
      return new mxPoint(Math.round(((totalLength / 2 - length - projlen) / totalLength) * -2), Math.round(yDistance / scale));
    }
  }

  return new mxPoint();
}