/** @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; }
/** * 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); }
/** @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()); } }
/** * Check if a line intersects with this polygon * * @param line * @return True if intersects, false otherwise */ public boolean intersects(Line2D line) { if (!line.getBounds().intersects(getBounds())) { return false; } generateEdges(); for (Enumeration e = edges.elements(); e.hasMoreElements(); ) { Line2D current = (Line2D) e.nextElement(); if (current.intersects(line)) { return true; } } return contains(line.getStart()) || contains(line.getEnd()); }
/** * Check if a point is contained within this polygon * * @param point * @return true if contained, false otherwise */ public boolean contains(Vector2D point) { if (!getBounds().contains(point)) { System.out.println("AAA: " + point); return false; } Line2D verticalRay = new Line2D(point, new Vector2D(point.getX(), Double.POSITIVE_INFINITY)); int intersectionCount = 0; boolean contains = false; generateEdges(); for (Enumeration e = edges.elements(); e.hasMoreElements(); ) { Line2D testLine = (Line2D) e.nextElement(); double minY = Math.min(testLine.getStart().getY(), testLine.getEnd().getY()); double maxY = Math.max(testLine.getStart().getY(), testLine.getEnd().getY()); if (testLine.getGradient() == Double.POSITIVE_INFINITY) { if (point.getX() == testLine.getStart().getX() && point.getY() >= minY && point.getY() <= maxY) { contains = true; } } else { if (verticalRay.intersects(testLine)) { intersectionCount++; } } } return (contains || (intersectionCount % 2 == 1)); }
/** * Check if another polygon intersects with this polygon * * @param polygon * @return true if intersects, false otherwise */ public boolean intersects(Polygon2D polygon) { if (!polygon.getBounds().intersects(getBounds())) { return false; } for (Enumeration e1 = edges(); e1.hasMoreElements(); ) { Line2D current = (Line2D) e1.nextElement(); for (Enumeration e2 = polygon.edges(); e2.hasMoreElements(); ) { Line2D test = (Line2D) e2.nextElement(); if (current.intersects(test)) { return true; } } } return false; }
boolean pointInLine(Point p, int fx, int fy, int tx, int ty) { double d = Line2D.ptLineDist( (double) fx, (double) fy, (double) tx, (double) ty, (double) p.x, (double) p.y); return d <= 6.0; }
/** * 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; }