Skip to content

Instantly share code, notes, and snippets.

@TimSC
Last active April 7, 2023 11:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TimSC/8c25ca941d614bf48ebba6b473747d72 to your computer and use it in GitHub Desktop.
Save TimSC/8c25ca941d614bf48ebba6b473747d72 to your computer and use it in GitHub Desktop.
Line-Plane collision in 3D, Python 2 or 3
#Line-plane intersection
#By Tim Sheerman-Chase
#This software may be reused under the CC0 license
#Based on http://geomalgorithms.com/a05-_intersect-1.html
from __future__ import print_function
import numpy as np
def LinePlaneCollision(planeNormal, planePoint, rayDirection, rayPoint, epsilon=1e-6):
ndotu = planeNormal.dot(rayDirection)
if abs(ndotu) < epsilon:
raise RuntimeError("no intersection or line is within plane")
w = rayPoint - planePoint
si = -planeNormal.dot(w) / ndotu
Psi = w + si * rayDirection + planePoint
return Psi
if __name__=="__main__":
#Define plane
planeNormal = np.array([0, 0, 1])
planePoint = np.array([0, 0, 5]) #Any point on the plane
#Define ray
rayDirection = np.array([0, -1, -1])
rayPoint = np.array([0, 0, 10]) #Any point along the ray
Psi = LinePlaneCollision(planeNormal, planePoint, rayDirection, rayPoint)
print ("intersection at", Psi)
@dmitrii299
Copy link

That looks great! Thank you for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment