//Function to check if a point is inside a polygon (only works for convex polygons) public bool IsInsidePolygon(List <geoPoint> Polygon, geoPoint Q) { //float Theta = 0; float SumTheta = 0; float m1 = 0; float m2 = 0; float costheta = 0; float EPSILON = 0; geoPoint P1 = null; geoPoint P2 = null; //X_Boundary.Insert(0, X_Boundary.Last) //Y_Boundary.Insert(0, Y_Boundary.Last) EPSILON = 0.0001F; //Theta = 0; for (int j = 0; j <= Polygon.Count - 1; j++) { // Point #j - Q P1 = Polygon[j].subtract(Q); // Point #j+1 - Q P2 = Polygon[(j + 1) % Polygon.Count].subtract(Q); m1 = Mathf.Pow((float)(P1.DotProduct(P1)), 0.5F); m2 = Mathf.Pow((float)(P2.DotProduct(P2)), 0.5F); if ((m1 * m2 <= EPSILON)) { return(true); //We are on a node, consider this inside } else { costheta = (P1.DotProduct(P2)) / (m1 * m2); } SumTheta = SumTheta + Mathf.Acos((float)costheta); } if (Mathf.Abs((float)(SumTheta - 2 * Mathf.PI)) < EPSILON | Mathf.Abs((float)(SumTheta + 2 * Math.PI)) < EPSILON) { return(true); } else { return(false); } }
private float PintToPlaneClosestPoint(geoPoint Q, geoPoint V0, ref geoPoint V1, geoPoint V2, ref geoPoint projection) { // Input: P = a 3D point // PL = a plane with point V0 and normal n // Output: ClosestPt = base point on PL of perpendicular from P float sb = 0; float sn = 0; float sd = 0; geoPoint a = null; geoPoint b = null; geoPoint n = null; a = V0.subtract(V1); b = V0.subtract(V2); n = a.CrossProduct(b); sn = -n.DotProduct(Q.subtract(V0)); sd = n.DotProduct(n); sb = sn / sd; projection = Q.Addition(n.Multiply(sb)); return(projection.distance(ref Q)); }