📄 line2d.java
字号:
* methods below. * * @see java.awt.geom.Line2D.Float * @see java.awt.geom.Line2D.Double */ protected Line2D() { } /** * Returns the X coordinate of the start point in double precision. * @return the X coordinate of this <code>Line2D</code> object's * starting point. */ public abstract double getX1(); /** * Returns the Y coordinate of the start point in double precision. * @return the Y coordinate of this <code>Line2D</code> object's * starting point. */ public abstract double getY1(); /** * Returns the starting <code>Point2D</code> of this * <code>Line2D</code>. * @return the starting <code>Point2D</code> of this * <code>Line2D</code>. */ public abstract Point2D getP1(); /** * Returns the X coordinate of the end point in double precision. * @return the X coordinate of this <code>Line2D</code> object's * starting point. */ public abstract double getX2(); /** * Returns the Y coordinate of the end point in double precision. * @return the Y coordinate of this <code>Line2D</code> object's * starting point. */ public abstract double getY2(); /** * Returns the end <code>Point2D</code> of this <code>Line2D</code>. * @return a <code>Point2D</code> that is the endpoint of this * <code>Line2D</code>. */ public abstract Point2D getP2(); /** * Sets the location of the endpoints of this <code>Line2D</code> to * the specified double coordinates. * @param X1, Y1 the first specified coordinate * @param X2, Y2 the second specified coordinate */ public abstract void setLine(double X1, double Y1, double X2, double Y2); /** * Sets the location of the endpoints of this <code>Line2D</code> to * the specified <code>Point2D</code> coordinates. * @param p1, p2 the specified <code>Point2D</code> objects */ public void setLine(Point2D p1, Point2D p2) { setLine(p1.getX(), p1.getY(), p2.getX(), p2.getY()); } /** * Sets the location of the endpoints of this <code>Line2D</code> to * the same as those endpoints of the specified <code>Line2D</code>. * @param l the specified <code>Line2D</code> */ public void setLine(Line2D l) { setLine(l.getX1(), l.getY1(), l.getX2(), l.getY2()); } /** * Returns an indicator of where the specified point * (PX, PY) lies with respect to the line segment from * (X1, Y1) to (X2, Y2). * The return value can be either 1, -1, or 0 and indicates * in which direction the specified line must pivot around its * first endpoint, (X1, Y1), in order to point at the * specified point (PX, PY). * <p>A return value of 1 indicates that the line segment must * turn in the direction that takes the positive X axis towards * the negative Y axis. In the default coordinate system used by * Java 2D, this direction is counterclockwise. * <p>A return value of -1 indicates that the line segment must * turn in the direction that takes the positive X axis towards * the positive Y axis. In the default coordinate system, this * direction is clockwise. * <p>A return value of 0 indicates that the point lies * exactly on the line segment. Note that an indicator value * of 0 is rare and not useful for determining colinearity * because of floating point rounding issues. * <p>If the point is colinear with the line segment, but * not between the endpoints, then the value will be -1 if the point * lies "beyond (X1, Y1)" or 1 if the point lies * "beyond (X2, Y2)". * @param X1, Y1 the coordinates of the beginning of the * specified line segment * @param X2, Y2 the coordinates of the end of the specified * line segment * @param PX, PY the coordinates of the specified point to be * compared with the specified line segment * @return an integer that indicates the position of the third specified * coordinates with respect to the line segment formed * by the first two specified coordinates. */ public static int relativeCCW(double X1, double Y1, double X2, double Y2, double PX, double PY) { X2 -= X1; Y2 -= Y1; PX -= X1; PY -= Y1; double ccw = PX * Y2 - PY * X2; if (ccw == 0.0) { // The point is colinear, classify based on which side of // the segment the point falls on. We can calculate a // relative value using the projection of PX,PY onto the // segment - a negative value indicates the point projects // outside of the segment in the direction of the particular // endpoint used as the origin for the projection. ccw = PX * X2 + PY * Y2; if (ccw > 0.0) { // Reverse the projection to be relative to the original X2,Y2 // X2 and Y2 are simply negated. // PX and PY need to have (X2 - X1) or (Y2 - Y1) subtracted // from them (based on the original values) // Since we really want to get a positive answer when the // point is "beyond (X2,Y2)", then we want to calculate // the inverse anyway - thus we leave X2 & Y2 negated. PX -= X2; PY -= Y2; ccw = PX * X2 + PY * Y2; if (ccw < 0.0) { ccw = 0.0; } } } return (ccw < 0.0) ? -1 : ((ccw > 0.0) ? 1 : 0); } /** * Returns an indicator of where the specified point * (PX, PY) lies with respect to this line segment. * See the method comments of * {@link #relativeCCW(double, double, double, double, double, double)} * to interpret the return value. * @param PX, PY the coordinates of the specified point * to be compared with the current line segment * @return an integer that indicates the position of the specified * coordinates with respect to the current line segment. * @see #relativeCCW(double, double, double, double, double, double) */ public int relativeCCW(double PX, double PY) { return relativeCCW(getX1(), getY1(), getX2(), getY2(), PX, PY); } /** * Returns an indicator of where the specified <code>Point2D</code> * lies with respect to this line segment. * See the method comments of * {@link #relativeCCW(double, double, double, double, double, double)} * to interpret the return value. * @param p the specified <code>Point2D</code> to be compared * with the current line segment * @return an integer that indicates the position of the * <code>Point2D</code> with respect to the current * line segment. * @see #relativeCCW(double, double, double, double, double, double) */ public int relativeCCW(Point2D p) { return relativeCCW(getX1(), getY1(), getX2(), getY2(), p.getX(), p.getY()); } /** * Tests if the line segment from (X1, Y1) to * (X2, Y2) intersects the line segment from (X3, Y3) * to (X4, Y4). * @param X1, Y1 the coordinates of the beginning of the first * specified line segment * @param X2, Y2 the coordinates of the end of the first * specified line segment * @param X3, Y3 the coordinates of the beginning of the second * specified line segment * @param X4, Y4 the coordinates of the end of the second * specified line segment * @return <code>true</code> if the first specified line segment * and the second specified line segment intersect * each other; <code>false</code> otherwise. */ public static boolean linesIntersect(double X1, double Y1, double X2, double Y2, double X3, double Y3, double X4, double Y4) { return ((relativeCCW(X1, Y1, X2, Y2, X3, Y3) * relativeCCW(X1, Y1, X2, Y2, X4, Y4) <= 0) && (relativeCCW(X3, Y3, X4, Y4, X1, Y1) * relativeCCW(X3, Y3, X4, Y4, X2, Y2) <= 0)); } /** * Tests if the line segment from (X1, Y1) to * (X2, Y2) intersects this line segment. * @param X1, Y1 the coordinates of the beginning of the * specified line segment * @param X2, Y2 the coordinates of the end of the specified * line segment * @return <true> if this line segment and the specified line segment * intersect each other; <code>false</code> otherwise. */ public boolean intersectsLine(double X1, double Y1, double X2, double Y2) { return linesIntersect(X1, Y1, X2, Y2, getX1(), getY1(), getX2(), getY2()); } /** * Tests if the specified line segment intersects this line segment. * @param l the specified <code>Line2D</code> * @return <code>true</code> if this line segment and the specified line * segment intersect each other; * <code>false</code> otherwise. */ public boolean intersectsLine(Line2D l) { return linesIntersect(l.getX1(), l.getY1(), l.getX2(), l.getY2(), getX1(), getY1(), getX2(), getY2()); } /** * Returns the square of the distance from a point to a line segment. * The distance measured is the distance between the specified * point and the closest point between the specified endpoints. * If the specified point intersects the line segment in between the * endpoints, this method returns 0.0. * @param X1, Y1 the coordinates of the beginning of the * specified line segment * @param X2, Y2 the coordinates of the end of the specified * line segment * @param PX, PY the coordinates of the specified point being * measured against the specified line segment * @return a double value that is the square of the distance from the * specified point to the specified line segment. * @see #ptLineDistSq(double, double, double, double, double, double) */ public static double ptSegDistSq(double X1, double Y1, double X2, double Y2, double PX, double PY) { // Adjust vectors relative to X1,Y1 // X2,Y2 becomes relative vector from X1,Y1 to end of segment X2 -= X1; Y2 -= Y1; // PX,PY becomes relative vector from X1,Y1 to test point PX -= X1; PY -= Y1; double dotprod = PX * X2 + PY * Y2; double projlenSq; if (dotprod <= 0.0) { // PX,PY is on the side of X1,Y1 away from X2,Y2 // distance to segment is length of PX,PY vector // "length of its (clipped) projection" is now 0.0 projlenSq = 0.0; } else { // switch to backwards vectors relative to X2,Y2 // X2,Y2 are already the negative of X1,Y1=>X2,Y2 // to get PX,PY to be the negative of PX,PY=>X2,Y2 // the dot product of two negated vectors is the same // as the dot product of the two normal vectors PX = X2 - PX; PY = Y2 - PY; dotprod = PX * X2 + PY * Y2; if (dotprod <= 0.0) { // PX,PY is on the side of X2,Y2 away from X1,Y1 // distance to segment is length of (backwards) PX,PY vector // "length of its (clipped) projection" is now 0.0 projlenSq = 0.0; } else { // PX,PY is between X1,Y1 and X2,Y2 // dotprod is the length of the PX,PY vector // projected on the X2,Y2=>X1,Y1 vector times the // length of the X2,Y2=>X1,Y1 vector projlenSq = dotprod * dotprod / (X2 * X2 + Y2 * Y2); } } // Distance to line is now the length of the relative point // vector minus the length of its projection onto the line // (which is zero if the projection falls outside the range // of the line segment). double lenSq = PX * PX + PY * PY - projlenSq; if (lenSq < 0) { lenSq = 0; } return lenSq; } /** * Returns the distance from a point to a line segment. * The distance measured is the distance between the specified * point and the closest point between the specified endpoints. * If the specified point intersects the line segment in between the * endpoints, this method returns 0.0. * @param X1, Y1 the coordinates of the beginning of the * specified line segment * @param X2, Y2 the coordinates of the end of the specified line * segment * @param PX, PY the coordinates of the specified point being * measured against the specified line segment * @return a double value that is the distance from the specified point * to the specified line segment. * @see #ptLineDist(double, double, double, double, double, double) */ public static double ptSegDist(double X1, double Y1, double X2, double Y2, double PX, double PY) { return Math.sqrt(ptSegDistSq(X1, Y1, X2, Y2, PX, PY)); } /** * Returns the square of the distance from a point to this line segment. * The distance measured is the distance between the specified * point and the closest point between the current line's endpoints. * If the specified point intersects the line segment in between the * endpoints, this method returns 0.0. * @param PX, PY the coordinates of the specified point being * measured against this line segment * @return a double value that is the square of the distance from the * specified point to the current line segment. * @see #ptLineDistSq(double, double) */ public double ptSegDistSq(double PX, double PY) { return ptSegDistSq(getX1(), getY1(), getX2(), getY2(), PX, PY);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -