| | |
| | | |
| | | public class PointUtil { |
| | | public static void main(String[] args) { |
| | | double a1 = 1, b1 = 2; |
| | | double a2 = 3, b2 = 4; |
| | | double x0 = 9, y0 = 9; |
| | | Point p1 = new Point(1, 2); |
| | | Point p2 = new Point(3, 4); |
| | | Point p0 = new Point(9, 9); |
| | | int[] x = {0, 8, 8, 0}; // 四边形顶点x坐标 |
| | | int[] y = {0, 0, 8, 8}; // 四边形顶点y坐标 |
| | | int px = 9, py = 9; // 待检测点坐标 |
| | | |
| | | Point p = findIntersection2(p1, p2, p0); |
| | | System.out.println(p); |
| | | |
| | | double[] p3 = findIntersection(a1, b1, a2, b2, x0, y0); |
| | | System.out.println("Intersection Point P3: (" + p3[0] + ", " + p3[1] + ")"); |
| | | |
| | | String position = determinePosition(a1, b1, a2, b2, x0, y0); |
| | | System.out.println("Point P is on the " + position + " of the line P1P2."); |
| | | } |
| | | |
| | | public static double[] findIntersection(double a1, double b1, double a2, double b2, double x0, double y0) { |
| | | // 检查 p1p2 是否为垂直线 |
| | | if (a2 == a1) { |
| | | return new double[]{a1, y0}; |
| | | } |
| | | |
| | | // 检查 p1p2 是否为水平线 |
| | | if (b2 == b1) { |
| | | return new double[]{x0, b1}; |
| | | } |
| | | double m = (b2 - b1) / (a2 - a1); |
| | | double mPerpendicular = (a1 - a2) / (b2 - b1); |
| | | |
| | | double x = ((b1 - m * a1) - (y0 - mPerpendicular * x0)) / (mPerpendicular - m); |
| | | double y = m * x + (b1 - m * a1); |
| | | |
| | | return new double[]{x, y}; |
| | | } |
| | | |
| | | public static Point findIntersection2(Point p1, Point p2, Point p0) { |
| | | // 检查 p1p2 是否为垂直线 |
| | | int a1 = p1.getX(); |
| | | int b1 = p1.getY(); |
| | | |
| | | int a2 = p2.getX(); |
| | | int b2 = p2.getY(); |
| | | |
| | | int x0 = p0.getX(); |
| | | int y0 = p0.getY(); |
| | | |
| | | if (a2 == a1) { |
| | | return new Point(a1, y0); |
| | | } |
| | | |
| | | // 检查 p1p2 是否为水平线 |
| | | if (b2 == b1) { |
| | | return new Point(x0, b1); |
| | | } |
| | | double m = (b2 - b1) / (a2 - a1); |
| | | double mPerpendicular = (a1 - a2) / (b2 - b1); |
| | | |
| | | double x = ((b1 - m * a1) - (y0 - mPerpendicular * x0)) / (mPerpendicular - m); |
| | | double y = m * x + (b1 - m * a1); |
| | | |
| | | return new Point((int) x, (int) y); |
| | | } |
| | | public static String determinePosition(double a1, double b1, double a2, double b2, double x0, double y0) { |
| | | double cross = (a2 - a1) * (y0 - b1) - (x0 - a1) * (b2 - b1); |
| | | if (cross > 0) { |
| | | return "left"; |
| | | } else if (cross < 0) { |
| | | return "right"; |
| | | if (isPointInQuadrilateral(px, py, x, y)) { |
| | | System.out.println("点在四边形内或边上"); |
| | | } else { |
| | | return "line"; |
| | | System.out.println("点在四边形外"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 判断点是否在四边形内或边上 |
| | | * @param px 待测点的x |
| | | * @param py 待测点的y |
| | | * @param x 四个点的x数组 四个点必须是顺时针顺序,从左上角开始顺时针 |
| | | * @param y 四个点的y数组 |
| | | * @return |
| | | */ |
| | | public static boolean isPointInQuadrilateral(int px, int py, int[] x, int[] y) { |
| | | int n = 4; // 四边形的边数 |
| | | |
| | | // 检查点是否在顶点上 |
| | | for (int i = 0; i < n; i++) { |
| | | if (px == x[i] && py == y[i]) { |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | // 检查点是否在边上 |
| | | for (int i = 0; i < n; i++) { |
| | | int j = (i + 1) % n; |
| | | if (isPointOnSegment(px, py, x[i], y[i], x[j], y[j])) { |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | // 使用射线法判断点是否在凸四边形内 |
| | | int intersections = 0; |
| | | |
| | | for (int i = 0; i < n; i++) { |
| | | int j = (i + 1) % n; |
| | | if (rayIntersectsSegment(px, py, x[i], y[i], x[j], y[j])) { |
| | | intersections++; |
| | | } |
| | | } |
| | | |
| | | return intersections % 2 != 0; |
| | | } |
| | | |
| | | private static boolean isPointOnSegment(int px, int py, int x1, int y1, int x2, int y2) { |
| | | // 检查点是否在x1, y1 和 x2, y2 形成的线段上 |
| | | if (px < Math.min(x1, x2) || px > Math.max(x1, x2) || py < Math.min(y1, y2) || py > Math.max(y1, y2)) { |
| | | return false; |
| | | } |
| | | |
| | | // 向量叉积 |
| | | int crossProduct = (px - x1) * (y2 - y1) - (py - y1) * (x2 - x1); |
| | | |
| | | // 向量点积 |
| | | int dotProduct1 = (px - x1) * (x2 - x1) + (py - y1) * (y2 - y1); |
| | | int dotProduct2 = (px - x2) * (x1 - x2) + (py - y2) * (y1 - y2); |
| | | |
| | | return crossProduct == 0 && dotProduct1 >= 0 && dotProduct2 >= 0; |
| | | } |
| | | |
| | | private static boolean rayIntersectsSegment(int px, int py, int x1, int y1, int x2, int y2) { |
| | | // 确保 y1 <= y2 |
| | | if (y1 > y2) { |
| | | return rayIntersectsSegment(px, py, x2, y2, x1, y1); |
| | | } |
| | | |
| | | // 射线不与线段平行且点在y范围内 |
| | | if (py > y1 && py <= y2) { |
| | | // 计算交点x坐标 |
| | | double xIntersection = (double) (px - x1) * (y2 - y1) / (double) (py - y1) + x1; |
| | | |
| | | // 射线从左到右,且交点在点的右侧 |
| | | return xIntersection >= px; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | } |
| | | |