/// <summary> /// The geometric center of a 3-dimensional, flat polygon. /// WARNING: UNTESTED! /// </summary> public static V3d ComputeCentroid(this Polygon3d polygon) { var pc = polygon.PointCount; if (pc < 3) { return(V3d.Zero); } V3d p0 = polygon[0], p1 = polygon[1]; V3d e0 = p1 - p0; var p2 = polygon[2]; var e1 = p2 - p0; var normal = e0.Cross(e1); var area2 = normal.Length; var centroid = area2 * (p0 + p1 + p2); p1 = p2; e0 = e1; for (int pi = 3; pi < pc; pi++) { p2 = polygon[pi]; e1 = p2 - p0; var n = e0.Cross(e1); var a2 = Fun.Sign(normal.Dot(n)) * n.Length; area2 += a2; centroid += a2 * (p0 + p1 + p2); p1 = p2; e0 = e1; } if (area2 > Constant <double> .PositiveTinyValue) { return(centroid * (Constant.OneThird / area2)); } else if (area2 < Constant <double> .NegativeTinyValue) { return(centroid * (-Constant.OneThird / area2)); } else { return(V3d.Zero); } }
public static Trafo3d FromToRH(Axis from, Axis to) { var s = Fun.Sign(((int)to) - ((int)from)); // +/- sin(90°) return(FromTo(from, to, s)); }