Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 887451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:13:58+00:00 2026-05-15T13:13:58+00:00

I’m looking for an algorithm that determines the near and far intersection points between

  • 0

I’m looking for an algorithm that determines the near and far intersection points between a line segment and an axis-aligned box.

Here is my method definition:

public static Point3D[] IntersectionOfLineSegmentWithAxisAlignedBox(
    Point3D rayBegin, Point3D rayEnd, Point3D boxCenter, Size3D boxSize)

If the line segment doesn’t intersect the box, the method should return an empty Point3D array.

From my research so far, I’ve come across some research papers with highly optimized algorithms, but they all seem to be written in C++ and would require multiple long class files to be converted to C#. For my purposes, something that is reasonably efficient, easy to understand by someone who gets dot products and cross products, and simple/short would be preferred.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:13:58+00:00Added an answer on May 15, 2026 at 1:13 pm

    Here’s what I ended up using:

    public static List<Point3D> 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<Point3D>();
        foreach (Axis axis in Enum.GetValues(typeof(Axis)))
        {
            if (beginToEnd.GetCoordinate(axis) == 0) // parallel
            {
                if (beginToMin.GetCoordinate(axis) > 0 || beginToMax.GetCoordinate(axis) < 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 > tNear) tNear = tMin;
                if (tMax < tFar) tFar = tMax;
                if (tNear > tFar || tFar < 0) return intersections;
    
            }
        }
        if (tNear >= 0 && tNear <= 1) intersections.Add(segmentBegin + beginToEnd * tNear);
        if (tFar >= 0 && tFar <= 1) intersections.Add(segmentBegin + beginToEnd * tFar);
        return intersections;
    }
    

    public enum Axis
    {
        X,
        Y,
        Z
    }
    

    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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 471k
  • Answers 471k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I setup a page using the Northwind DB and using… May 16, 2026 at 3:11 am
  • Editorial Team
    Editorial Team added an answer Have you checked output of syncdb and actually seen, that… May 16, 2026 at 3:11 am
  • Editorial Team
    Editorial Team added an answer You can write two CSS classes: .tb_with_border { border: 1px… May 16, 2026 at 3:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.