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; }
@Override public void mouseMoved(MouseEvent e) { Point p = e.getPoint(); if (line.ptSegDist(p) <= 1.0) { tipLabel.setText(tooltip); } parent.repaint(); }
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; }
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; }
/** * Search the intersection point between the border of a rectangle and the line defined by first * and next point. The rectangle is decomposed in for lines and each line go to infinite. So all * lines intersect an edge of the rectangle. We must compute if segments intersect each others or * not. * * @param bounds the rectangle * @param first the first point * @param next the next point * @return the intersection point; or null if no points found */ public static Point searchNearestEgde(Rectangle bounds, Point first, Point next) { // One offset needed to avoid intersection with the wrong line. if (bounds.x + bounds.width < first.x) first.x = bounds.x + bounds.width - 1; else if (bounds.x > first.x) first.x = bounds.x + 1; if (bounds.y + bounds.height < first.y) first.y = bounds.height + bounds.y - 1; else if (bounds.y > first.y) first.y = bounds.y + 1; final Line2D relationLine = new Line2D.Float(first.x, first.y, next.x, next.y); final Line2D lineTop = new Line2D.Float(bounds.x, bounds.y, bounds.x + bounds.width, bounds.y); final Line2D lineRight = new Line2D.Float( bounds.x + bounds.width, bounds.y, bounds.x + bounds.width, bounds.y + bounds.height); final Line2D lineBottom = new Line2D.Float( bounds.x + bounds.width, bounds.y + bounds.height, bounds.x, bounds.y + bounds.height); final Line2D lineLeft = new Line2D.Float(bounds.x, bounds.y + bounds.height, bounds.x, bounds.y); final Point2D ptIntersectTop = ptIntersectsLines(relationLine, lineTop); final Point2D ptIntersectRight = ptIntersectsLines(relationLine, lineRight); final Point2D ptIntersectBottom = ptIntersectsLines(relationLine, lineBottom); final Point2D ptIntersectLeft = ptIntersectsLines(relationLine, lineLeft); // line is to infinite, we must verify that the point find interst the // correct edge and the relation. final int distTop = (int) lineTop.ptSegDist(ptIntersectTop) + (int) relationLine.ptSegDist(ptIntersectTop); final int distRight = (int) lineRight.ptSegDist(ptIntersectRight) + (int) relationLine.ptSegDist(ptIntersectRight); final int distBottom = (int) lineBottom.ptSegDist(ptIntersectBottom) + (int) relationLine.ptSegDist(ptIntersectBottom); final int distLeft = (int) lineLeft.ptSegDist(ptIntersectLeft) + (int) relationLine.ptSegDist(ptIntersectLeft); if (ptIntersectTop != null && distTop == 0) return new Point((int) ptIntersectTop.getX(), (int) ptIntersectTop.getY()); else if (ptIntersectRight != null && distRight == 0) return new Point((int) ptIntersectRight.getX(), (int) ptIntersectRight.getY()); else if (ptIntersectBottom != null && distBottom == 0) return new Point((int) ptIntersectBottom.getX(), (int) ptIntersectBottom.getY()); else if (ptIntersectLeft != null && distLeft == 0) return new Point((int) ptIntersectLeft.getX(), (int) ptIntersectLeft.getY()); else return null; // no point found! }
/** Accuracy Test of the <code>contains(int, int)</code> method. */ public void testContains() { // check the edge contains the point or not. assertTrue("The edge should contain this point.", test.contains(2, 3)); // check the edge contains the point or not. assertTrue("The edge should contain this point.", test.contains(15, 7)); // check the edge contains the point or not. assertTrue("The edge should contain this point.", test.contains(33, 0)); double number = Line2D.ptSegDist(0, 0, 10, 10, -3, 0); System.out.println("number" + number); // set the active width. test.setActiveWidth(1); // check the edge contains the point or not. assertFalse("The edge should not contain this point.", test.contains(15, 7)); }
public boolean contact(GraphSettings gInfo, Graph g, Point p, int bottom) { // snap to year int yearWidth = gInfo.getYearWidth(); int firstYearIdx = (p.x / yearWidth) - 1; int nYears = 3; // Check three years' worth of data: the year prior to and after the year the mouse is inside Year startYear = gInfo.getDrawBounds().getStart().add(firstYearIdx); List<Point2D> points = this.getPointsFrom(gInfo, g, startYear, nYears, bottom); int nLines = points.size() - 1; for (int i = 0; i < nLines; i++) { Line2D line = new Line2D.Float(points.get(i), points.get(i + 1)); int distance = Math.round((float) line.ptSegDist(p)); if (distance <= NEAR) return true; } return false; }
/** * This utility function makes it easy to check whether the coordinates of the user's mouse click * (px,py) are on (or very close to) the line between the two points, (x1,y1) and (x2,y2). * * @param px x coordinate of mouse click * @param py y coordinate of mouse click * @param x1 x point of the first coordinate of object to check for mouse click proximity * @param y1 y point of the first coordinate of object to check for mouse click proximity * @param x2 x point of the second coordinate of object to check for mouse click proximity * @param y2 y point of the second coordinate of object to check for mouse click proximity * @return true if the point where the user clicked is on or close to the line between the two * points,(x1,y1) and (x2,y2) */ public static boolean clickHitLine(int px, int py, int x1, int y1, int x2, int y2) { return Line2D.ptSegDist(x1, y1, x2, y2, px, py) < SELECTION_DISTANCE; }