在二维平面上,有一点p(x,y),有一个三角形ABC,顶点坐标为A(x1,y1),B(x2,y2),C(x3,y3),如何判断点P是否在三角形内或在三角形的边上?-灵析社区

CTang

在空间中,有两条线段,分别是线段AB和线段CD,它们的顶点坐标分别是A(x1,y1,z1),B(x2,y2,z2),C(x3,y3,z3);D(x4,y4,z4),请问如何求线段AB与线段CD的交点坐标?

阅读量:185

点赞量:12

问AI
问题一: public class GeometryUtils { public static void main(String[] args) { System.out.println(isPointInTriangle(1, 1, 0, 0, 2, 0, 0, 2, 1, 1)); System.out.println(findLineSegmentsIntersection(0, 0, 0, 1, 1, 0, 1, 1, 0)); } public static boolean isPointInTriangle(double px, double py, double ax, double ay, double bx, double by, double cx, double cy) { double sABC = Math.abs(ax * by + bx * cy + cx * ay - ax * cy - bx * ay - cx * by) / 2.0; double sPAB = Math.abs(px * ay + ax * by + bx * py - ax * py - px * by - bx * ay) / 2.0; double sPAC = Math.abs(px * ay + ax * cy + cx * py - ax * py - px * cy - cx * ay) / 2.0; double sPBC = Math.abs(px * by + bx * cy + cx * py - bx * py - px * cy - cx * by) / 2.0; return Math.abs(sPAB + sPAC + sPBC - sABC) = 0 && t = 0 && s <= 1) { result[0] = x1 + t * (x2 - x1); result[1] = y1 + t * (y2 - y1); result[2] = z1 + t * (z2 - z1); return result; } else { return null; // 线段无交点 } } }