forked from gradientspace/geometry3Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Line2d, ported 2D Line/Line and Segment/Segment intersection te…
…sts (required Intersector1 interval-overlap, so also added Interval1d struct) added Polygon2d Signed Area, IsCW, and IsInside(pt) tests all untested!
- Loading branch information
Showing
10 changed files
with
610 additions
and
1 deletion.
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
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
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,102 @@ | ||
using System; | ||
|
||
namespace g3 | ||
{ | ||
// interval [a,b] on Real line. | ||
// TODO: should check that a <= b !! | ||
public struct Interval1d | ||
{ | ||
public double a; | ||
public double b; | ||
|
||
public Interval1d(double f) { a = b = f; } | ||
public Interval1d(double x, double y) { this.a = x; this.b = y; } | ||
public Interval1d(double[] v2) { a = v2[0]; b = v2[1]; } | ||
public Interval1d(float f) { a = b = f; } | ||
public Interval1d(float x, float y) { this.a = x; this.b = y; } | ||
public Interval1d(float[] v2) { a = v2[0]; b = v2[1]; } | ||
public Interval1d(Interval1d copy) { a = copy.a; b = copy.b; } | ||
|
||
|
||
static public readonly Interval1d Zero = new Interval1d(0.0f, 0.0f); | ||
static public readonly Interval1d Empty = new Interval1d(double.MaxValue, -double.MaxValue); | ||
static public readonly Interval1d Infinite = new Interval1d(-double.MaxValue, double.MaxValue); | ||
|
||
|
||
public double this[int key] | ||
{ | ||
get { return (key == 0) ? a : b; } | ||
set { if (key == 0) a = value; else b = value; } | ||
} | ||
|
||
|
||
public double LengthSquared | ||
{ | ||
get { return (a-b)*(a-b); } | ||
} | ||
public double Length | ||
{ | ||
get { return b-a; } | ||
} | ||
|
||
|
||
public bool Contains(double d) { | ||
return d >= a && d <= b; | ||
} | ||
|
||
|
||
public bool Overlaps(Interval1d o) { | ||
return ! ( o.a > b || o.b < a ); | ||
} | ||
|
||
public double SquaredDist(Interval1d o) { | ||
if ( b < o.a ) | ||
return (o.a - b)*(o.a - b); | ||
else if ( a > o.b ) | ||
return (a - o.b)*(a - o.b); | ||
else | ||
return 0; | ||
} | ||
public double Dist(Interval1d o) { | ||
if ( b < o.a ) | ||
return o.a - b; | ||
else if ( a > o.b ) | ||
return a - o.b; | ||
else | ||
return 0; | ||
} | ||
|
||
|
||
public void Set(Interval1d o) { | ||
a = o.a; b = o.b; | ||
} | ||
public void Set(double fA, double fB) { | ||
a = fA; b = fB; | ||
} | ||
|
||
|
||
|
||
public static Interval1d operator -(Interval1d v) { | ||
return new Interval1d(-v.a, -v.b); | ||
} | ||
|
||
|
||
public static Interval1d operator +(Interval1d a, double f) { | ||
return new Interval1d(a.a + f, a.b + f); | ||
} | ||
public static Interval1d operator -(Interval1d a, double f) { | ||
return new Interval1d(a.a - f, a.b - f); | ||
} | ||
|
||
public static Interval1d operator *(Interval1d a, double f) { | ||
return new Interval1d(a.a * f, a.b * f); | ||
} | ||
|
||
|
||
public override string ToString() { | ||
return string.Format("[{0:F8},{1:F8}]", a, b); | ||
} | ||
|
||
|
||
} | ||
} |
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,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace g3 | ||
{ | ||
public struct Line2d | ||
{ | ||
public Vector2d Origin; | ||
public Vector2d Direction; | ||
|
||
public Line2d(Vector2d origin, Vector2d direction) | ||
{ | ||
this.Origin = origin; | ||
this.Direction = direction; | ||
} | ||
|
||
// parameter is distance along Line | ||
public Vector2d PointAt(double d) { | ||
return Origin + d * Direction; | ||
} | ||
|
||
public double Project(Vector2d p) | ||
{ | ||
return (p - Origin).Dot(Direction); | ||
} | ||
|
||
public double DistanceSquared(Vector2d p) | ||
{ | ||
double t = (p - Origin).Dot(Direction); | ||
Vector2d proj = Origin + t * Direction; | ||
return (proj - p).LengthSquared; | ||
} | ||
|
||
// conversion operators | ||
public static implicit operator Line2d(Line2f v) | ||
{ | ||
return new Line2d(v.Origin, v.Direction); | ||
} | ||
public static explicit operator Line2f(Line2d v) | ||
{ | ||
return new Line2f((Vector2f)v.Origin, (Vector2f)v.Direction); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
public struct Line2f | ||
{ | ||
public Vector2f Origin; | ||
public Vector2f Direction; | ||
|
||
public Line2f(Vector2f origin, Vector2f direction) | ||
{ | ||
this.Origin = origin; | ||
this.Direction = direction; | ||
} | ||
|
||
// parameter is distance along Line | ||
public Vector2f PointAt(float d) | ||
{ | ||
return Origin + d * Direction; | ||
} | ||
|
||
public float Project(Vector2f p) | ||
{ | ||
return (p - Origin).Dot(Direction); | ||
} | ||
|
||
public float DistanceSquared(Vector2f p) | ||
{ | ||
float t = (p - Origin).Dot(Direction); | ||
Vector2f proj = Origin + t * Direction; | ||
return (proj - p).LengthSquared; | ||
} | ||
} | ||
} |
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
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
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
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,72 @@ | ||
using System; | ||
|
||
namespace g3 | ||
{ | ||
// ported from WildMagic5 | ||
// | ||
// A class for intersection of intervals [u0,u1] and [v0,v1]. The end | ||
// points must be ordered: u0 <= u1 and v0 <= v1. Values of MAX_REAL | ||
// and -MAX_REAL are allowed, and degenerate intervals are allowed: | ||
// u0 = u1 or v0 = v1. | ||
// | ||
public class Intersector1 | ||
{ | ||
// intervals to intersect | ||
public Interval1d U; | ||
public Interval1d V; | ||
|
||
|
||
// Information about the intersection set. The number of intersections | ||
// is 0 (intervals do not overlap), 1 (intervals are just touching), or | ||
// 2 (intervals intersect in an inteval). | ||
public int NumIntersections = 0; | ||
|
||
// intersection point/interval, access via GetIntersection | ||
private Interval1d Intersections = Interval1d.Zero; | ||
|
||
public Intersector1(double u0, double u1, double v0, double v1) { | ||
// [TODO] validate 0 < 1 | ||
U = new Interval1d(u0,u1); | ||
V = new Interval1d(v0,v1); | ||
} | ||
public Intersector1(Interval1d u, Interval1d v) { | ||
U = u; | ||
V = v; | ||
} | ||
|
||
public bool Test { | ||
get { return U.a <= V.b && U.b >= V.a; } | ||
} | ||
|
||
|
||
public double GetIntersection(int i) { | ||
return Intersections[i]; | ||
} | ||
|
||
public bool Find() | ||
{ | ||
if (U.b < V.a || U.a > V.b) { | ||
NumIntersections = 0; | ||
} else if (U.b > V.a) { | ||
if (U.a < V.b) { | ||
NumIntersections = 2; | ||
Intersections.a = (U.a < V.a ? V.a : U.a); | ||
Intersections.b = (U.b > V.b ? V.b : U.b); | ||
if (Intersections.a == Intersections.b) { | ||
NumIntersections = 1; | ||
} | ||
} else { | ||
// U.a == V.b | ||
NumIntersections = 1; | ||
Intersections.a = U.a; | ||
} | ||
} else { | ||
// U.b == V.a | ||
NumIntersections = 1; | ||
Intersections.a = U.b; | ||
} | ||
|
||
return NumIntersections > 0; | ||
} | ||
} | ||
} |
Oops, something went wrong.