Skip to content

Commit 8aa7804

Browse files
committed
0.7.0
1 parent de96391 commit 8aa7804

14 files changed

Lines changed: 284 additions & 55 deletions

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# eppz! `Geometry`
22

3+
* 0.7.0
4+
5+
+ Test scenes
6+
`4. Polygon-Segment containment`
7+
`5. Polygon-Polygon containment`
8+
`6. Vertex facing`
9+
`7. Polygon area, Polygon winding`
10+
311
* 0.6.0
412

513
+ Test scenes

Lines/GeometryLineRenderer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ public class GeometryLineRenderer : DirectLineRenderer
2424
protected void DrawSegment(Segment segment, Color color)
2525
{ DrawLine(segment.a, segment.b, color); }
2626

27+
protected void DrawSegment(Segment segment, Color color, bool drawNormals)
28+
{
29+
DrawLine(segment.a, segment.b, color);
30+
if (drawNormals)
31+
{
32+
Vector2 halfie = segment.a + ((segment.b - segment.a) / 2.0f);
33+
DrawLine(halfie, halfie + segment.normal * 0.1f, color);
34+
}
35+
}
36+
2737
protected void DrawPolygon(Polygon polygon, Color color)
2838
{ polygon.EnumerateEdgesRecursive((Edge eachEdge) => DrawLine(eachEdge.a, eachEdge.b, color)); }
2939

Lines/SegmentLineRenderer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class SegmentLineRenderer : GeometryLineRenderer
2424

2525
public Color lineColor;
2626
public Color boundsColor;
27+
public bool normals = false;
2728
private Segment segment;
2829

2930

@@ -37,7 +38,7 @@ void Start()
3738
protected override void OnDraw()
3839
{
3940
DrawRect(segment.bounds, boundsColor);
40-
DrawSegment(segment, lineColor);
41+
DrawSegment(segment, lineColor, normals);
4142
}
4243
}
4344
}

Model/Edge.cs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,6 @@ public class Edge : Segment
2424
public Vertex vertexA;
2525
public Vertex vertexB;
2626

27-
// If `alwaysCalculate` is on, every `normal` and `perpendicular` property access invokes recalculation of values based on actual topology.
28-
public bool alwaysCalculate = true;
29-
30-
private Vector2 _normal;
31-
public Vector2 normal
32-
{
33-
get
34-
{
35-
if (_normal == Vector2.zero || alwaysCalculate) { CalculateNormal(); } // Lazy calculation or force calculate on every access
36-
return _normal;
37-
}
38-
39-
set
40-
{ _normal = value; }
41-
}
42-
43-
public Vector2 _perpendicular;
44-
public Vector2 perpendicular
45-
{
46-
get
47-
{
48-
if (_perpendicular == Vector2.zero || alwaysCalculate) { CalculatePerpendicular(); } // Lazy calculation or force calculate on every access
49-
return _perpendicular;
50-
}
51-
52-
set
53-
{ _perpendicular = value; }
54-
}
55-
56-
public void CalculateNormal()
57-
{
58-
_normal = this.perpendicular.normalized;
59-
}
60-
61-
public void CalculatePerpendicular()
62-
{
63-
Vector2 translated = (this.b - this.a); // Translate to origin
64-
_perpendicular = new Vector2( -translated.y, translated.x); // Rotate CCW
65-
}
66-
6727

6828
#region Factory
6929

Model/Polygon.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public void EnumeratePolygons(Action<Polygon> action)
307307

308308
#region Polygon calculations
309309

310-
void Calculate()
310+
public void Calculate()
311311
{
312312
_CalculateBounds();
313313
_CalculateArea();
@@ -357,7 +357,7 @@ private void _CalculateArea()
357357
firstProducts += eachPoint.x * eachNextPoint.y;
358358
secondProducts += eachPoint.y * eachNextPoint.x;
359359
}
360-
float _area = (firstProducts - secondProducts) / 2.0f;
360+
_area = (firstProducts - secondProducts) / 2.0f;
361361

362362
// Add / Subtract sub-polygon areas.
363363
foreach (Polygon eachPolygon in polygons)

Model/Segment.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,50 @@ public virtual Vector2 b
3939
set { _b = value; }
4040
}
4141

42+
#region Calculations
43+
44+
// If `alwaysCalculate` is on, every `normal` and `perpendicular` property access invokes recalculation of values based on actual topology.
45+
public bool alwaysCalculate = true;
46+
47+
private Vector2 _normal;
48+
public Vector2 normal
49+
{
50+
get
51+
{
52+
if (_normal == Vector2.zero || alwaysCalculate) { CalculateNormal(); } // Lazy calculation or force calculate on every access
53+
return _normal;
54+
}
55+
56+
set
57+
{ _normal = value; }
58+
}
59+
60+
public Vector2 _perpendicular;
61+
public Vector2 perpendicular
62+
{
63+
get
64+
{
65+
if (_perpendicular == Vector2.zero || alwaysCalculate) { CalculatePerpendicular(); } // Lazy calculation or force calculate on every access
66+
return _perpendicular;
67+
}
68+
69+
set
70+
{ _perpendicular = value; }
71+
}
72+
73+
public void CalculateNormal()
74+
{
75+
_normal = this.perpendicular.normalized;
76+
}
77+
78+
public void CalculatePerpendicular()
79+
{
80+
Vector2 translated = (this.b - this.a); // Translate to origin
81+
_perpendicular = new Vector2( -translated.y, translated.x); // Rotate CCW
82+
}
83+
84+
#endregion
85+
4286

4387
#region Factories
4488

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ The library is **being used in production**. However, it comes with the disclaim
1111

1212
If you prefer to read example code immediately, you can find example scenes in [`Scenes`](Scenes) folder.
1313

14+
+ [Polygon-Point containment](Scenes/README.md/#0-polygon-point-containment)
15+
+ [Polygon-Segment intersection test](Scenes/README.md/#1-polygon-segment-intersection)
16+
+ [Polygon permiter-Point containment (Precise)](Scenes/README.md/#2-polygon-permiter-point-containment-precise)
17+
+ [Polygon permiter-Point containment (Default)](Scenes/README.md/#3-polygon-permiter-point-containment-default)
18+
+ [Polygon-Segment containment](Scenes/README.md/#4-polygon-segment-containment)
19+
+ [Polygon-Polygon containment](Scenes/README.md/#5-polygon-polygon-containment)
20+
+ [Vertex facing](Scenes/README.md/#6-vertex-facing)
21+
+ [Polygon area, Polygon winding](Scenes/README.md/#7-polygon-area-polygon-facing)
22+
1423
## Model classes
1524

1625
* [`Vertex.cs`](Model/Vertex.cs)

Scenes/6. Vertex facing.unity

37.2 KB
Binary file not shown.
83.4 KB
Binary file not shown.

Scenes/Controllers/Controller_5.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//
2+
// Copyright (c) 2017 Geri Borbás http://www.twitter.com/_eppz
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7+
//
8+
using UnityEngine;
9+
using System.Collections;
10+
11+
12+
namespace EPPZ.Geometry.Scenes
13+
{
14+
15+
16+
using Components;
17+
using Lines;
18+
19+
20+
/// <summary>
21+
/// 5. Polygon-Polygon containment
22+
/// </summary>
23+
public class Controller_5 : MonoBehaviour
24+
{
25+
26+
27+
public Color defaultColor;
28+
public Color passingColor;
29+
30+
public PolygonSource starSource;
31+
public PolygonSource squareSource;
32+
public PolygonLineRenderer starRenderer;
33+
public PolygonLineRenderer squareRenderer;
34+
35+
private Polygon star { get { return starSource.polygon; } }
36+
private Polygon square { get { return squareSource.polygon; } }
37+
38+
39+
void Update()
40+
{ RenderTestResult(IsPolygonInsideTest()); }
41+
42+
bool IsPolygonInsideTest()
43+
{
44+
// Point containment.
45+
bool pointContainment = true;
46+
square.EnumeratePointsRecursive((Vector2 eachPoint) =>
47+
{
48+
pointContainment &= star.ContainsPoint(eachPoint);
49+
});
50+
51+
// Segment-Polygon intersecion, Segment endpoint-permiter contaimnent.
52+
bool segmentIntersecting = false;
53+
bool permiterContainsSegment = false;
54+
foreach (Edge eachEdge in square.edges)
55+
{
56+
permiterContainsSegment |= star.PermiterContainsPoint(eachEdge.a) || star.PermiterContainsPoint(eachEdge.b);
57+
segmentIntersecting |= star.IsIntersectingWithSegment(eachEdge);
58+
}
59+
60+
// A polygon contains another polygon, when
61+
// - other polygon vertices are contained by polygon,
62+
// - other polygon segments are not intersecting with polygon,
63+
// - other polygon vertices are not contained by polygon permiter.
64+
bool polygonInside = (
65+
pointContainment &&
66+
segmentIntersecting == false &&
67+
permiterContainsSegment == false
68+
);
69+
70+
return polygonInside;
71+
}
72+
73+
void RenderTestResult(bool testResult)
74+
{
75+
Color color = (testResult) ? passingColor : defaultColor;
76+
77+
// Layout colors.
78+
starRenderer.lineColor = color;
79+
squareRenderer.lineColor = color;
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)