📄 line2d.java
字号:
{ return Math.sqrt(ptSegDistSq(x1, y1, x2, y2, px, py)); } /** * Measures the square of the shortest distance from the reference point * to a point on this line segment. If the point is on the segment, the * result will be 0. * * @param px the x coordinate of the point * @param py the y coordinate of the point * @return the square of the distance from the point to the segment * @see #ptSegDistSq(double, double, double, double, double, double) */ public double ptSegDistSq(double px, double py) { return ptSegDistSq(getX1(), getY1(), getX2(), getY2(), px, py); } /** * Measures the square of the shortest distance from the reference point * to a point on this line segment. If the point is on the segment, the * result will be 0. * * @param p the point * @return the square of the distance from the point to the segment * @throws NullPointerException if p is null * @see #ptSegDistSq(double, double, double, double, double, double) */ public double ptSegDistSq(Point2D p) { return ptSegDistSq(getX1(), getY1(), getX2(), getY2(), p.getX(), p.getY()); } /** * Measures the shortest distance from the reference point to a point on * this line segment. If the point is on the segment, the result will be 0. * * @param px the x coordinate of the point * @param py the y coordinate of the point * @return the distance from the point to the segment * @see #ptSegDist(double, double, double, double, double, double) */ public double ptSegDist(double px, double py) { return ptSegDist(getX1(), getY1(), getX2(), getY2(), px, py); } /** * Measures the shortest distance from the reference point to a point on * this line segment. If the point is on the segment, the result will be 0. * * @param p the point * @return the distance from the point to the segment * @throws NullPointerException if p is null * @see #ptSegDist(double, double, double, double, double, double) */ public double ptSegDist(Point2D p) { return ptSegDist(getX1(), getY1(), getX2(), getY2(), p.getX(), p.getY()); } /** * Measures the square of the shortest distance from the reference point * to a point on the infinite line extended from the segment. If the point * is on the segment, the result will be 0. If the segment is length 0, * the distance is to the common endpoint. * * @param x1 the first x coordinate of the segment * @param y1 the first y coordinate of the segment * @param x2 the second x coordinate of the segment * @param y2 the second y coordinate of the segment * @param px the x coordinate of the point * @param py the y coordinate of the point * @return the square of the distance from the point to the extended line * @see #ptLineDist(double, double, double, double, double, double) * @see #ptSegDistSq(double, double, double, double, double, double) */ public static double ptLineDistSq(double x1, double y1, double x2, double y2, double px, double py) { double pd2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); double x, y; if (pd2 == 0) { // Points are coincident. x = x1; y = y2; } else { double u = ((px - x1) * (x2 - x1) + (py - y1) * (y2 - y1)) / pd2; x = x1 + u * (x2 - x1); y = y1 + u * (y2 - y1); } return (x - px) * (x - px) + (y - py) * (y - py); } /** * Measures the shortest distance from the reference point to a point on * the infinite line extended from the segment. If the point is on the * segment, the result will be 0. If the segment is length 0, the distance * is to the common endpoint. * * @param x1 the first x coordinate of the segment * @param y1 the first y coordinate of the segment * @param x2 the second x coordinate of the segment * @param y2 the second y coordinate of the segment * @param px the x coordinate of the point * @param py the y coordinate of the point * @return the distance from the point to the extended line * @see #ptLineDistSq(double, double, double, double, double, double) * @see #ptSegDist(double, double, double, double, double, double) */ public static double ptLineDist(double x1, double y1, double x2, double y2, double px, double py) { return Math.sqrt(ptLineDistSq(x1, y1, x2, y2, px, py)); } /** * Measures the square of the shortest distance from the reference point * to a point on the infinite line extended from this segment. If the point * is on the segment, the result will be 0. If the segment is length 0, * the distance is to the common endpoint. * * @param px the x coordinate of the point * @param py the y coordinate of the point * @return the square of the distance from the point to the extended line * @see #ptLineDistSq(double, double, double, double, double, double) */ public double ptLineDistSq(double px, double py) { return ptLineDistSq(getX1(), getY1(), getX2(), getY2(), px, py); } /** * Measures the square of the shortest distance from the reference point * to a point on the infinite line extended from this segment. If the point * is on the segment, the result will be 0. If the segment is length 0, * the distance is to the common endpoint. * * @param p the point * @return the square of the distance from the point to the extended line * @throws NullPointerException if p is null * @see #ptLineDistSq(double, double, double, double, double, double) */ public double ptLineDistSq(Point2D p) { return ptLineDistSq(getX1(), getY1(), getX2(), getY2(), p.getX(), p.getY()); } /** * Measures the shortest distance from the reference point to a point on * the infinite line extended from this segment. If the point is on the * segment, the result will be 0. If the segment is length 0, the distance * is to the common endpoint. * * @param px the x coordinate of the point * @param py the y coordinate of the point * @return the distance from the point to the extended line * @see #ptLineDist(double, double, double, double, double, double) */ public double ptLineDist(double px, double py) { return ptLineDist(getX1(), getY1(), getX2(), getY2(), px, py); } /** * Measures the shortest distance from the reference point to a point on * the infinite line extended from this segment. If the point is on the * segment, the result will be 0. If the segment is length 0, the distance * is to the common endpoint. * * @param p the point * @return the distance from the point to the extended line * @throws NullPointerException if p is null * @see #ptLineDist(double, double, double, double, double, double) */ public double ptLineDist(Point2D p) { return ptLineDist(getX1(), getY1(), getX2(), getY2(), p.getX(), p.getY()); } /** * Test if a point is contained inside the line. Since a line has no area, * this returns false. * * @param x the x coordinate * @param y the y coordinate * @return false; the line does not contain points */ public boolean contains(double x, double y) { return false; } /** * Test if a point is contained inside the line. Since a line has no area, * this returns false. * * @param p the point * @return false; the line does not contain points */ public boolean contains(Point2D p) { return false; } /** * Tests if this line intersects the interior of the specified rectangle. * * @param x the x coordinate of the rectangle * @param y the y coordinate of the rectangle * @param w the width of the rectangle * @param h the height of the rectangle * @return true if the line intersects the rectangle */ public boolean intersects(double x, double y, double w, double h) { if (w <= 0 || h <= 0) return false; double x1 = getX1(); double y1 = getY1(); double x2 = getX2(); double y2 = getY2(); if (x1 >= x && x1 <= x + w && y1 >= y && y1 <= y + h) return true; if (x2 >= x && x2 <= x + w && y2 >= y && y2 <= y + h) return true; double x3 = x + w; double y3 = y + h; return (linesIntersect(x1, y1, x2, y2, x, y, x, y3) || linesIntersect(x1, y1, x2, y2, x, y3, x3, y3) || linesIntersect(x1, y1, x2, y2, x3, y3, x3, y) || linesIntersect(x1, y1, x2, y2, x3, y, x, y)); } /** * Tests if this line intersects the interior of the specified rectangle. * * @param r the rectangle * @return true if the line intersects the rectangle * @throws NullPointerException if r is null */ public boolean intersects(Rectangle2D r) { return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight()); } /** * Tests if the line contains a rectangle. Since lines have no area, this * always returns false. * * @param x the x coordinate of the rectangle * @param y the y coordinate of the rectangle * @param w the width of the rectangle * @param h the height of the rectangle * @return false; the line does not contain points */ public boolean contains(double x, double y, double w, double h) { return false; } /** * Tests if the line contains a rectangle. Since lines have no area, this * always returns false. * * @param r the rectangle * @return false; the line does not contain points */ public boolean contains(Rectangle2D r) { return false; } /** * Gets a bounding box (not necessarily minimal) for this line. * * @return the integer bounding box * @see #getBounds2D() */ public Rectangle getBounds() { return getBounds2D().getBounds(); } /** * Return a path iterator, possibly applying a transform on the result. This * iterator is not threadsafe. * * @param at the transform, or null * @return a new path iterator */ public PathIterator getPathIterator(final AffineTransform at) { return new PathIterator() { /** Current coordinate. */ private int current; public int getWindingRule() { return WIND_NON_ZERO; } public boolean isDone() { return current < 2; } public void next() { current++; } public int currentSegment(float[] coords) { int result; switch (current) { case 0: coords[0] = (float) getX1(); coords[1] = (float) getY1(); result = SEG_MOVETO; break; case 1: coords[0] = (float) getX2(); coords[1] = (float) getY2(); result = SEG_LINETO; break; default: throw new NoSuchElementException("line iterator out of bounds"); } if (at != null) at.transform(coords, 0, coords, 0, 1); return result; } public int currentSegment(double[] coords) { int result; switch (current) { case 0: coords[0] = getX1(); coords[1] = getY1(); result = SEG_MOVETO; break; case 1: coords[0] = getX2(); coords[1] = getY2(); result = SEG_LINETO; break; default:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -