Last active
April 7, 2023 11:00
-
-
Save TimSC/8c25ca941d614bf48ebba6b473747d72 to your computer and use it in GitHub Desktop.
Line-Plane collision in 3D, Python 2 or 3
This file contains 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
#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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That looks great! Thank you for sharing!