-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e05b3d
commit 5413a99
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace Nine.Geometry | ||
{ | ||
using System.Numerics; | ||
|
||
public static class GeometryHelper | ||
{ | ||
/// <summary> | ||
/// Returns whether the points are in counter clockwise order. | ||
/// </summary> | ||
/// <param name="points"></param> | ||
/// <returns></returns> | ||
public static bool PointsAreCounterClockwiseOrder(Vector2[] points) | ||
{ | ||
float signedArea = 0; | ||
for (int i = 0; i < points.Length; i++) | ||
{ | ||
int nextIndex = (i + 1) % points.Length; | ||
signedArea += (points[nextIndex].X - points[i].X) | ||
* (points[nextIndex].Y + points[i].Y); | ||
} | ||
|
||
return signedArea < 0; | ||
} | ||
|
||
/// <summary> | ||
/// Returns whether the points are in counter clockwise order. | ||
/// </summary> | ||
/// <param name="points"></param> | ||
/// <returns></returns> | ||
public static bool PointsAreCounterClockwiseOrder(Vector3[] points) | ||
{ | ||
float signedArea = 0; | ||
for (int i = 0; i < points.Length; i++) | ||
{ | ||
int nextIndex = (i + 1) % points.Length; | ||
signedArea += (points[nextIndex].X - points[i].X) | ||
* (points[nextIndex].Y + points[i].Y) | ||
* (points[nextIndex].Z + points[i].Z); | ||
} | ||
|
||
return signedArea < 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
namespace Nine.Geometry.Test | ||
{ | ||
using System.Numerics; | ||
using Xunit; | ||
|
||
public class GeometryHelperTest | ||
{ | ||
[Fact] | ||
public void PointsAreCounterClockwiseOrder_Vector2() | ||
{ | ||
var points = new Vector2[] | ||
{ | ||
new Vector2(0, 0), | ||
new Vector2(1, 0), | ||
new Vector2(1, 1), | ||
}; | ||
|
||
var counterClockwise = GeometryHelper.PointsAreCounterClockwiseOrder(points); | ||
|
||
Assert.True(counterClockwise); | ||
} | ||
|
||
[Fact] | ||
public void PointsAreCounterClockwiseOrder_Vector2_Clockwise() | ||
{ | ||
var points = new Vector2[] | ||
{ | ||
new Vector2(1, 1), | ||
new Vector2(1, 0), | ||
new Vector2(0, 0), | ||
}; | ||
|
||
var counterClockwise = GeometryHelper.PointsAreCounterClockwiseOrder(points); | ||
|
||
Assert.False(counterClockwise); | ||
} | ||
|
||
[Fact] | ||
public void PointsAreCounterClockwiseOrder_Vector3() | ||
{ | ||
var points = new Vector3[] | ||
{ | ||
new Vector3(1, 1, 1), | ||
new Vector3(1, 0, 1), | ||
new Vector3(0, 0, 1), | ||
}; | ||
|
||
var counterClockwise = GeometryHelper.PointsAreCounterClockwiseOrder(points); | ||
|
||
Assert.False(counterClockwise); | ||
} | ||
|
||
[Fact] | ||
public void PointsAreCounterClockwiseOrder_Vector3_Clockwise() | ||
{ | ||
var points = new Vector3[] | ||
{ | ||
new Vector3(1, 1, 1), | ||
new Vector3(1, 0, 1), | ||
new Vector3(0, 0, 1), | ||
}; | ||
|
||
var counterClockwise = GeometryHelper.PointsAreCounterClockwiseOrder(points); | ||
|
||
Assert.False(counterClockwise); | ||
} | ||
} | ||
} |