Skip to content

Commit be428b2

Browse files
committed
Adding Plane.Halfspace support
1 parent 7f5b34e commit be428b2

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

RaysAndStuff/Plane.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ namespace RaysAndStuff
1010
{
1111
internal class Plane
1212
{
13+
public enum Halfspace
14+
{
15+
Halfspace_BEHIND, //behind the plane
16+
Halfspace_ON_PLANE,
17+
Halfspace_FRONT, //in front of plane
18+
};
19+
1320
public Plane(Vector3 A, Vector3 B, Vector3 C)
1421
{
1522
Normal = Vector3.Cross((B - A), (C - A)); // NORMAL = AB X AC
@@ -49,6 +56,14 @@ public float DistanceTo(Vector3 P)
4956
return Vector3.Dot(Normal, P) + D;
5057
}
5158

59+
public Halfspace ClassifyPoint(Vector3 P)
60+
{
61+
float d = DistanceTo(P);
62+
if (d == 0) return Halfspace.Halfspace_ON_PLANE;
63+
if (d > 0) return Halfspace.Halfspace_FRONT;
64+
return Halfspace.Halfspace_BEHIND;
65+
}
66+
5267
public readonly Vector3 Normal; // Normalized vector perpendicular to the plane
5368
public readonly float D; // Distance from a point to the plane.
5469
}

RaysAndStuff/Ray.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
using System.Text;
55
using System.Threading.Tasks;
66
using System.Numerics;
7+
using System.Xml.Linq;
78

89
namespace RaysAndStuff
910
{
1011
internal struct Ray
1112
{
1213
public Vector3 Start;
1314
public Vector3 Dir;
15+
public override string ToString()
16+
{
17+
return "Start:" + Start.ToString() + " Dir:" + Dir.ToString();
18+
}
1419
}
1520
}

RaysAndStuff/ShootRays.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public ShootRays()
2121
for(int r = 0; r < rays.Count; r++)
2222
{
2323
Ray ray = rays[r];
24+
Console.WriteLine($"Ray {r}: {ray}");
2425
foreach (Plane plane in planes)
2526
{
2627
if (plane.RayIntersection(ray.Start, ray.Dir, ref t, ref hitPt))
@@ -29,10 +30,13 @@ public ShootRays()
2930
}
3031
else
3132
{
32-
Console.WriteLine("Ray missed plane " + count);
33+
Console.WriteLine($"Ray {r} missed plane {count}");
3334
}
3435
float dist1 = plane.DistanceTo(ray.Start);
3536
Console.WriteLine($"Plane: {count} distance: {dist1}");
37+
Plane.Halfspace halfspace = plane.ClassifyPoint(ray.Start);
38+
var text = halfspace.ToString();
39+
Console.WriteLine("Classification Ray.start: " + text);
3640
count++;
3741
}
3842
}

0 commit comments

Comments
 (0)