Skip to content

Commit

Permalink
Modified IsConvex helper to handle lines of collinear points
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Evans <martindevans@gmail.com>
  • Loading branch information
martindevans committed Apr 8, 2014
1 parent 39c3bcb commit d2d3ac2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
17 changes: 12 additions & 5 deletions Myre/Myre/Extensions/Vector2Extensions.cs
Expand Up @@ -88,6 +88,7 @@ public static float Area(this Vector2[] v)
public static bool IsConvex(this Vector2[] v)
{
int sign = 0;
bool first = true;
for (int i = 0; i < v.Length; i++)
{
var p = v[i];
Expand All @@ -97,12 +98,18 @@ public static bool IsConvex(this Vector2[] v)
var d1 = p1 - p;
var d2 = p2 - p1;

var zcrossproduct = d1.X * d2.Y - d1.Y * d2.X;
var crossProductSign = Math.Sign(d1.X * d2.Y - d1.Y * d2.X);

if (i == 0)
sign = Math.Sign(zcrossproduct);
else if (Math.Sign(zcrossproduct) != sign)
return false;
if (crossProductSign != 0)
{
if (first)
{
sign = crossProductSign;
first = false;
}
else if (crossProductSign != sign)
return false;
}
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion Myre/Myre/Properties/AssemblyInfo.cs
Expand Up @@ -27,6 +27,6 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.2.0")]
[assembly: AssemblyVersion("1.2.1")]

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("MyreTests")]

0 comments on commit d2d3ac2

Please sign in to comment.