Created
July 26, 2011 20:21
-
-
Save hohyon-ryu/1107915 to your computer and use it in GitHub Desktop.
Get a 3D plane through 3 points and get z of a point (x,y) on that plane
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
require "matrix" | |
def cross( v, w ) | |
x = v[1]*w[2] - v[2]*w[1] | |
y = v[2]*w[0] - v[0]*w[2] | |
z = v[0]*w[1] - v[1]*w[0] | |
Vector[x,y,z] | |
end | |
def multi_sumV( v, w ) | |
v[0]*w[0] + v[1]*w[1] + v[2]*w[2] | |
end | |
def planeFrom3Points(p1, p2, p3) | |
v1= p3-p1 | |
v2= p2-p1 | |
crossed=cross(v1,v2) | |
d=multi_sumV(crossed, p1) | |
[crossed[0],crossed[1], crossed[2],d] | |
end | |
def getZfromPlane(x,y,plane) | |
(plane[3]-(plane[0]*x+plane[1]*y))/plane[2] | |
end | |
p1=Vector[1,2,3] | |
p2=Vector[4,6,9] | |
p3=Vector[12,11,9] | |
plane=planeFrom3Points(p1, p2, p3) | |
#puts plane | |
puts getZfromPlane(12,10,plane) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment