Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what I ended up using:</p> <pre><code>public static List&lt;Point3D&gt; IntersectionOfLineSegmentWithAxisAlignedBox( Point3D segmentBegin, Point3D segmentEnd, Point3D boxCenter, Size3D boxSize) { var beginToEnd = segmentEnd - segmentBegin; var minToMax = new Vector3D(boxSize.X, boxSize.Y, boxSize.Z); var min = boxCenter - minToMax / 2; var max = boxCenter + minToMax / 2; var beginToMin = min - segmentBegin; var beginToMax = max - segmentBegin; var tNear = double.MinValue; var tFar = double.MaxValue; var intersections = new List&lt;Point3D&gt;(); foreach (Axis axis in Enum.GetValues(typeof(Axis))) { if (beginToEnd.GetCoordinate(axis) == 0) // parallel { if (beginToMin.GetCoordinate(axis) &gt; 0 || beginToMax.GetCoordinate(axis) &lt; 0) return intersections; // segment is not between planes } else { var t1 = beginToMin.GetCoordinate(axis) / beginToEnd.GetCoordinate(axis); var t2 = beginToMax.GetCoordinate(axis) / beginToEnd.GetCoordinate(axis); var tMin = Math.Min(t1, t2); var tMax = Math.Max(t1, t2); if (tMin &gt; tNear) tNear = tMin; if (tMax &lt; tFar) tFar = tMax; if (tNear &gt; tFar || tFar &lt; 0) return intersections; } } if (tNear &gt;= 0 &amp;&amp; tNear &lt;= 1) intersections.Add(segmentBegin + beginToEnd * tNear); if (tFar &gt;= 0 &amp;&amp; tFar &lt;= 1) intersections.Add(segmentBegin + beginToEnd * tFar); return intersections; } </code></pre> <hr> <pre><code>public enum Axis { X, Y, Z } </code></pre> <hr> <pre><code>public static double GetCoordinate(this Point3D point, Axis axis) { switch (axis) { case Axis.X: return point.X; case Axis.Y: return point.Y; case Axis.Z: return point.Z; default: throw new ArgumentException(); } } public static double GetCoordinate(this Vector3D vector, Axis axis) { switch (axis) { case Axis.X: return vector.X; case Axis.Y: return vector.Y; case Axis.Z: return vector.Z; default: throw new ArgumentException(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload