Created
July 11, 2014 14:00
-
-
Save mattneary/46da06e8f3ba9550dd14 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class ClsSurfacePlaneIntersector : ClsIntersector { | |
public int Precision = 100; | |
public Vector3 FindPointIntersection(Func<float, Vector3> surfaceSlice, float elevation, float min, float max) { | |
var mid = (max + min) / 2; | |
var minE = surfaceSlice(min).Z; | |
var midE = surfaceSlice(mid).Z; | |
var maxE = surfaceSlice(max).Z; | |
if (FuzzyEqual(elevation, midE) || FuzzyEqual(mid, max)) { | |
return surfaceSlice(mid); | |
} | |
if (PointInRange(elevation, minE, midE)) { | |
return FindPointIntersection(surfaceSlice, elevation, min, mid); | |
} else if (PointInRange(elevation, midE, maxE)) { | |
return FindPointIntersection(surfaceSlice, elevation, mid, max); | |
} else { | |
return null; | |
} | |
} | |
public List<Vector3> Intersect() { | |
var intersections = new List<Vector3>(); | |
for (float theta = 0; theta <= 1; theta += 1 / Precision) { | |
Func<float, Vector3> slice = (float h) => Surface(h, theta).Add(Origin); | |
var intersection = FindPointIntersection(slice, Plane, 0, 1); | |
if (intersection != null) { | |
intersections.Add(intersection); | |
} | |
} | |
return intersections; | |
} | |
public Vector3 Origin; | |
public ClsSurfacePlaneIntersector(Func<float, float, Vector3> surface, Vector3 origin, float elevation) { | |
Surface = surface; | |
Origin = origin; | |
Plane = elevation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment