void Update() { if (polygon == null) { return; // Only having polygon } // Layout winding direction object if any. bool hasWindingDirectionObject = (windingDirectionObject != null); bool windingChanged = (polygon.windingDirection != _previousWindingDirection); if (hasWindingDirectionObject && windingChanged) { windingDirectionObject.transform.localScale = (polygon.isCW) ? Vector3.one : new Vector3(1.0f, -1.0f, 1.0f); windingDirectionObject.transform.rotation = (polygon.isCW) ? Quaternion.identity : Quaternion.Euler(new Vector3(0.0f, 0.0f, 90.0f)); } // Layout area text mesh if any. bool hasAreaTextMesh = (areaTextMesh != null); bool areaChanged = (polygon.area != _previousArea); if (hasAreaTextMesh && areaChanged) { areaTextMesh.text = polygon.area.ToString(); } // Track. _previousWindingDirection = polygon.windingDirection; _previousArea = polygon.area; }
public static Vector2 CentroidOfPolygons(Polygon[] polygons, Polygon.WindingDirection windingDirection = Polygon.WindingDirection.Unknown) { if (windingDirection == Polygon.WindingDirection.Unknown) { windingDirection = polygons[0].windingDirection; } // From https://en.wikipedia.org/wiki/Centroid#By_geometric_decomposition float ΣxA = 0.0f; float ΣyA = 0.0f; float ΣA = 0.0f; foreach (Polygon eachPolygon in polygons) { // Add or subtract area. float sign = (eachPolygon.windingDirection == windingDirection) ? 1.0f : -1.0f; float eachSignedArea = eachPolygon.area * sign; // Get centroid. Vector2 eachCentroid = eachPolygon.centroid; // Sum weighted. ΣxA += eachCentroid.x * eachSignedArea; ΣyA += eachCentroid.y * eachSignedArea; ΣA += eachSignedArea; } // "Remove" area. float x = ΣxA / ΣA; float y = ΣyA / ΣA; return(new Vector2(x, y)); }