2D Geometry for Unity. Suited for everyday polygon hassle. Polygon clipping, polygon winding direction, polygon area, polygon centroid, centroid of multiple polygons, line intersection, point-line distance, segment intersection, polygon-point containment, polygon triangulation, polygon Voronoi diagram, polygon offset, polygon outline, polygon buffer, polygon union, polygon substraction, polygon boolean operations, and more. It is a polygon fest.
Geometry
part of Unity.Library.eppz
📐 2D geometry for Unity. Suited for everyday polygon hassle.
Polygon clipping, polygon winding direction, polygon area, polygon centroid, centroid of multiple polygons, line intersection, point-line distance, segment intersection, polygon-point containment, polygon triangulation, polygon Voronoi diagram, polygon offset, polygon outline, polygon buffer, polygon union, polygon substraction, polygon boolean operations, and more.
The library is being used in production. However, it comes with the disclaimed liability and warranty of MIT License.
If you prefer to read example code immediately, you can find example scenes in Scenes
folder.
Vertex.cs
Vector2
point, but is aware of the polygon context it resides (neighbours, segments, edges, polygon, bisector, normal).Segment.cs
Vector2
point. Carries out basic geometry features (point distance, point containment, segment intersection).Edge.cs
Vertex
in a polygon (a special Segment
subclass). Likewise vertices, this model is also aware of the polygon context it resides (neighbours, segments, edges, polygon, perpendicular, normal).Polygon.cs
Source.Polygon
components. Further polygons can be embedded into recursively. Vertices, edges, polygons can be enumerated (recursively).Geometry.cs
Most of the basic 2D geometry algorithm collection is implemented in this static base class. You can (mostly) use them with Unity Vector2
types directly, so (almost entirely) without the model classes introduced above.
ArePointsEqualWithAccuracy()
ArePointsCCW()
IsRectContainsRectSizeWithAccuracy()
rect2.size
fits into rect1
(compare sizes only).IsRectContainsRectWithAccuracy()
rect2
is contained by rect1
(even if permiters are touching) with a given accuracy.IntersectionPointOfLines()
PointDistanceFromLine()
PointIsLeftOfSegment()
AreSegmentsEqualWithAccuracy()
HaveSegmentsCommonPointsWithAccuracy()
AreSegmentsIntersecting()
EPPZ.Geometry.Polygon
)
IsPolygonContainsPoint()
CentroidOfPolygons()
For clipping, offsetting, triangulating the library use these brilliant third party C#
libraries below.
The library uses namespaces heavily. I like to name things as they are. An edge in this library called Edge
, a polygon is called Polygon
. If it is a polygon model, it resides the Model
namespace (EPPZ.Geometry.Model
actually). Whether it is a source component for polygon, it resides in the Source
namespace. It becomes nicely readable, as you declare polygons like Model.Polygon
, or reference polygon sources as Source.Polygon
.
In addition, every class is namespaced in the folder it resides. If you look at a folder name, you can tell that classes are namespaced to the same as the folder name.
Polygon
extensions for easy conversion between eppz! Geometry and Clipper. It has a method to convert from generic Vector2[]
array. Clipper works with integers. So conversion involves a scale up (and a scale down), thus you'll need to pass a scale value to Clipper. (for example eppz! Geometry internals use 10e+5f
by default).
Polygon PolygonFromClipperPaths(Paths paths, float scale)
Polygon PolygonFromClipperPath(Path path, float scale)
Paths ClipperPaths(this Polygon this_, float scale)
Path ClipperPath(this Polygon this_, float scale)
Vector2[] PointsFromClipperPath(Path path, float scale)
Model.Polygon
objects and Triangle.NET
models (meshes, voronoi diagrams).
TriangleNet.Geometry.Polygon TriangleNetPolygon(this Polygon this_)
Rect Bounds(this TriangleNet.Voronoi.Legacy.SimpleVoronoi this_)
Paths ClipperPathsFromVoronoiRegions(List<TriangleNet.Voronoi.Legacy.VoronoiRegion> voronoiRegions, float scale = 1.0f)
Vector2 VectorFromPoint(TriangleNet.Geometry.Point point)
Vector2[] PointsFromVertices(ICollection<TriangleNet.Geometry.Point> vertices)
Model.Polygon
(yet enormously useful) extension that triangulates the corresponding polygon, and hooks up the result into a UnityEngine.MeshFilter
component. This is the core functionality embedded into Source.Mesh
component (see example scene Polygon triangulation for more).
UnityEngine.Mesh Mesh(this EPPZ.Geometry.Model.Polygon this_, string name = "")
UnityEngine.Mesh Mesh(this EPPZ.Geometry.Model.Polygon this_, TriangulatorType triangulator, string name = "")
UnityEngine.Mesh Mesh(this EPPZ.Geometry.Model.Polygon this_, Color color, TriangulatorType triangulator, string name = "")
Licensed under the MIT License.