# Geometrical calculations : points, lines, planes : intersections, distances, angles

 11 November 2005 #1 prettyPixel Know-it-All cg artist   Join Date: Feb 2005 Posts: 302 Geometrical calculations : points, lines, planes : intersections, distances, angles I am trying to collect the most usual functions for the geometrical calculations : Intersection point, projection, distance, angle, ... Of course there are many Web sites about this, but I do not seek the mathematical formulas but functions 'ready-to-use' in maxscript. I start by giving the formulas which I know already. If you know other formulas do not hesitate to add them. Let's say that you have 4 points: pA=[ax,ay,az] pB=[bx,by,bz] pC=[cx,cy,cz] pD=[dx,dy,dz] With these points you can define a vector, a line, a plane. Vector: vAB=pB-pA vAC=pC-pA vCD=pD-pC Vector Normalization : length vector = 1.0 ``normalize vAB`` see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/NormalizedVector.html Cross Product : ``cross vAB vAC`` see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/CrossProduct.html Dot Product : ``````dot vAB vAC dot (normalize vAB) (normalize vAC)`````` see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/DotProduct.html line: A line can be defined : - by 2 points - or by 1 point and 1 vector Middle Point M of AB: ``fn middlePoint pA pB = ((pA+pB)/2.0)`` Point along AB : the point at 25% between A and B: ``fn alongPoint pA pB prop = (pA+((pB-pA)*prop))`` plane: A plane can be defined : - by 3 points - or by 1 point and 2 vectors - or by 1 point and 1 vector (the normal vector) Normal Vector : the vector perpendicular to the plane: ``normalVector=normalize (cross vAB vAC)`` see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/NormalVector.html line and point: Point-Line Projection : find the point on the line AB which is the projection of the point C: ``````fn pointLineProj pA pB pC = ( local vAB=pB-pA local vAC=pC-pA local d=dot (normalize vAB) (normalize vAC) (pA+(vAB*(d*(length vAC/length vAB)))) )`````` Point-Line Distance : the distance between the line AB and the point C: ``````fn pointLineDist2 pA pB pC = ( local vAB=pB-pA local vAC=pC-pA (length (cross vAB vAC))/(length vAB) )`````` or ``d=distance pC (pointLineProj pA pB pC)`` see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Point-...imensional.html Point-Line Inclusion : is this point on the line ? ``````fn isPointLine pA pB pC tol = ( local vAB=pB-pA local vAC=pC-pA local d=1.0-abs(dot (normalize vAB) (normalize vAC)) if d<=tol then true else false )`````` or Point-Line Distance <= tolerance distance (to be continued) share quote
 11 November 2005 #2 Gibbz Game Artist   portfolio Bronson Mathews Game Artist Australia   Join Date: Dec 2002 Posts: 1,090 nice stuff takes me back to highschool math days.... __________________ share quote
 03 March 2006 #3 prettyPixel Know-it-All cg artist   Join Date: Feb 2005 Posts: 302 line and line line and line: Line-Line Intersection : intersection of two lines AB and CD: ``````fn lineLineIntersect pA pB pC pD = ( local a=pB-pA local b=pD-pC local c=pC-pA local cross1 = cross a b local cross2 = cross c b pA + ( a*( (dot cross2 cross1)/((length cross1)^2) ) ) )`````` or by using vectors: ``````fn lineLineIntersect pA a pB b = ( local c=pB-pA local cross1 = cross a b local cross2 = cross c b pA + ( a*( (dot cross2 cross1)/((length cross1)^2) ) ) )`````` see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-LineIntersection.html Line-Line Distance : distance between two skew lines ``````fn lineLineDist pA a pB b = ( local c=pB-pA local crossAB=cross a b abs (dot c crossAB) / (length crossAB) )`````` see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-LineDistance.html Line-Line // Distance : distance between two parallel lines use "Point-Line Distance" : pA-pB is the first line and pC is a point of the second line Line-Line Angle : angle between two lines: ``````fn getVectorsAngle vAB vCD = ( acos (dot (normalize vAB) (normalize vCD)) )`````` or ``````fn lineLineAngle pA pB pC pD = ( local vAB=pB-pA local vCD=pD-pC local angle=acos (dot (normalize vAB) (normalize vCD)) if angle<90.0 then angle else (180.0-angle) )`````` see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-LineAngle.html share quote
 03 March 2006 #4 prettyPixel Know-it-All cg artist   Join Date: Feb 2005 Posts: 302 plane and point plane and point: Point-Plane Projection : find the point on the plane ABC which is the projection of the point D ``````fn pointPlaneProj pA pB pC pD = ( local nABC=normalize (cross (pB-pA) (pC-pA)) pD+((dot (pA-pD) nABC)*nABC) )`````` Point-Plane Distance : find the distance between a plane and a point ``````fn pointPlaneDist pA pB pC pD = ( local nABC=normalize (cross (pB-pA) (pC-pA)) length ((dot (pA-pD) nABC)*nABC) )`````` see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Point-PlaneDistance.html Last edited by prettyPixel : 03 March 2006 at 07:05 AM. share quote
 03 March 2006 #5 prettyPixel Know-it-All cg artist   Join Date: Feb 2005 Posts: 302 plane and line plane and line: Line-Plane Intersection : intersection between a line and a plane ``````fn planeLineIntersect planePoint planeNormal linePoint lineVector = ( local lineVector=normalize lineVector local d1=dot (planePoint-linePoint) planeNormal local d2=dot lineVector planeNormal if abs(d2)<.0000000754 then ( if abs(d1)>.0000000754 then 0 else -1 ) else ( linePoint + ( (d1/d2)*lineVector ) ) )`````` This script is based on an original script of Joshua Newman. return 0 for parrallel (no intersections) results and -1 for co-incident (infinate) results http://forums.cgsociety.org/showthread.php?t=290255 see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-P...tersection.html Line-Plane Distance : distance between a plane and a line // use "Point-Plane Distance" : where 'point' is a point of the line Vector perpendicular to a line into a Plane : the perpendicular of AB into the plane ABC ``````vectorPerp=cross vectorAB (cross vectorAB vectorAC) -- or vectorPerp=cross (cross vectorAB vectorAC) vectorAB`````` see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Perpendicular.html share quote
 03 March 2006 #6 j-man in 3D!   portfolio Joshua Newman Director MESHROOM London, United Kingdom   Join Date: Apr 2005 Posts: 1,345 Originally Posted by prettyPixel: plane and line: Line-Plane Intersection : intersection between a line and a plane ``````fn planeLineIntersect planePoint planeNormal linePoint lineVector = ( local lineVector=normalize lineVector local d1=dot (planePoint-linePoint) planeNormal local d2=dot lineVector planeNormal if abs(d2)<.0000000754 then ( if abs(d1)>.0000000754 then 0 else -1 ) else ( linePoint + ( (d1/d2)*lineVector ) ) )`````` This script is based on an original script of Joshua Newman. return 0 for parrallel (no intersections) results and -1 for co-incident (infinate) results http://forums.cgsociety.org/showthread.php?t=290255 see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-P...tersection.html Line-Plane Distance : distance between a plane and a line // use "Point-Plane Distance" : where 'point' is a point of the line Vector perpendicular to a line into a Plane : the perpendicular of AB into the plane ABC ``````vectorPerp=cross vectorAB (cross vectorAB vectorAC) -- or vectorPerp=cross (cross vectorAB vectorAC) vectorAB`````` see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Perpendicular.html Thanks for the aknowelgement PP. Still can't get around using the tolerence then eh? Great resource BTW, keep up the good work! J¬ __________________ www.joshuanewman.net share quote
 03 March 2006 #7 JHaywood Know-it-All   portfolio James Haywood Senior Technical Artist Bungie Studios Seattle, USA   Join Date: Jun 2005 Posts: 395 This is great, thanks. __________________ Website Blog share quote
 03 March 2006 #8 erilaz Stealthy Tea Monkey   portfolio Martin Brennand VFX Artist Melbourne, Australia   Join Date: Jun 2002 Posts: 13,756 This should be a sticky! __________________ "There Really is No Secret" Martin Brennand - mocha Product Manager - Imagineer Systems share quote
 03 March 2006 #9 prettyPixel Know-it-All cg artist   Join Date: Feb 2005 Posts: 302 Thanks guys jman: The tolerance seems to work for me, but I use rather the function in cases where the line is not parallel to the plan. Can you give an example where the tolerance is an issue? Well, the continuation: share quote
 03 March 2006 #10 prettyPixel Know-it-All cg artist   Join Date: Feb 2005 Posts: 302 plane and plane plane and plane: Plane-Plane Intersection : find the line which is the intersection of 2 planes p1 : a point of the plane 1 n1 : the normal of the plane 1 ``````fn planePlaneIntersect p1 n1 p2 n2 = ( -- n1, n2 are normalized local lineVector = cross n1 n2 local proj1=(dot n1 p1)*n1 local proj2=(dot n2 p2)*n2 local perp1=cross n1 (normalize lineVector) local perp2=cross n2 (normalize lineVector) local cr = cross (proj2-proj1) perp2 local intersectionPoint = proj1 + (perp1*( (dot cr lineVector) / ((length lineVector)^2)) ) ray intersectionPoint lineVector )`````` I think that it is not the easiest solution. If you know a better means... see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Plane-...tersection.html Plane perpendicular to a Plane and a Line : find the plane which is perpendicular to the plane n and to the line AB It's the plane ABC where C=A+n see also : Eric W. Weisstein. From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/ParallelPlanes.html Plane-Plane Angle : find the angle between two planes n1 and n2 (normals) ``fn getVectorsAngle n1 n2 = ( acos (dot (normalize n1) (normalize n2)) )`` share quote
 04 April 2006 #11 j-man in 3D!   portfolio Joshua Newman Director MESHROOM London, United Kingdom   Join Date: Apr 2005 Posts: 1,345 Originally Posted by prettyPixel: Can you give an example where the tolerance is an issue? Yes, if the results are parrallel or co-incident the rounding error tells the function from telling which! J¬ __________________ www.joshuanewman.net Last edited by j-man : 04 April 2006 at 12:41 PM. share quote
 04 April 2006 #12 JHaywood Know-it-All   portfolio James Haywood Senior Technical Artist Bungie Studios Seattle, USA   Join Date: Jun 2005 Posts: 395 Hey PP, just wondering if you're done adding to this thread. I want to put together a word doc containing all these functions, but wanted to wait until you were fiinished. __________________ Website Blog share quote
 04 April 2006 #13 JHaywood Know-it-All   portfolio James Haywood Senior Technical Artist Bungie Studios Seattle, USA   Join Date: Jun 2005 Posts: 395 I've got a math question, and this seems like a good place to ask. I tried digging through Mathworld, but it's way over my head. I'd like to be able to find a point on an arc defined by the end point of an object rotated around it's pivot, along a specific axis. For example, let's say there's a bone object with a end bone as it's child. If I rotate the parent bone around it's Y axis 10 degrees, the end bone is now at a new position. What's the formula for finding that position? This page at Mathworld describes what I'm talking about, except in 2D: http://mathworld.wolfram.com/Arc.html __________________ Website Blog share quote
 04 April 2006 #14 erilaz Stealthy Tea Monkey   portfolio Martin Brennand VFX Artist Melbourne, Australia   Join Date: Jun 2002 Posts: 13,756 Originally Posted by JHaywood: I've got a math question, and this seems like a good place to ask. I tried digging through Mathworld, but it's way over my head. I'd like to be able to find a point on an arc defined by the end point of an object rotated around it's pivot, along a specific axis. For example, let's say there's a bone object with a end bone as it's child. If I rotate the parent bone around it's Y axis 10 degrees, the end bone is now at a new position. What's the formula for finding that position? This page at Mathworld describes what I'm talking about, except in 2D: http://mathworld.wolfram.com/Arc.html That should be a straight trig calculation where (skeleton code): `````` r = \$bone01.length theta = (rotation of your bone) phi = 90 - theta y = sin(theta) * r x = cos(theta) * r z = sin(theta) * r * cos(phi) `````` I'm a little hazy on the Z phi stuff, so somebody correct me if i'm wrong! __________________ "There Really is No Secret" Martin Brennand - mocha Product Manager - Imagineer Systems Last edited by erilaz : 04 April 2006 at 03:58 AM. share quote
 04 April 2006 #15 prettyPixel Know-it-All cg artist   Join Date: Feb 2005 Posts: 302 Originally Posted by JHaywood: Hey PP, just wondering if you're done adding to this thread. I want to put together a word doc containing all these functions, but wanted to wait until you were fiinished. I think that the essential formulae are there but anyone can add some more. The possibilities are illimited. Here is an example: this script discovers if a line intersects a sphere. It returns true or false. ``````fn lineSphereIntersection sphereCenter sphereRadius linePoint lineVector = ( local nLineVector= normalize lineVector local projPoint=linePoint+((dot (sphereCenter-linePoint) nLineVector)*nLineVector) local dist=distance sphereCenter projPoint if dist>sphereRadius then false else true )`````` I use the projection of a point on a plane which passes by the sphere center. If the distance between this point and the center is lower or equal to the sphere radius then the line intersects the sphere. It's based on the function 'Point-Plane Projection' . Another way of going farther is to write the same functions but on objects like triangular faces or segments. For example find the position of a line that intersects a mesh face (not a plane). The case is more restricted. share quote

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off CGSociety Society of Digital Artists www.cgsociety.org Powered by vBulletinCopyright ©2000 - 2006, Jelsoft Enterprises Ltd.