Notifications
Clear all

[Closed] 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-LineDistance3-Dimensional.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)

67 Replies

nice stuff

takes me back to highschool math days…

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

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

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

see also : Eric W. Weisstein. From MathWorld–A Wolfram Web Resource.
http://mathworld.wolfram.com/Line-PlaneIntersection.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!

This is great, thanks.

This should be a sticky!

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:

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-PlaneIntersection.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)) )
1 Reply
(@arketip)
Joined: 11 months ago

Posts: 0

Here is an improvment of the function for calculating the intersection of 2 planes:

pA, nA : first plane, where p is a point and n is the normal of the plane
pB, nB : the second plane


fn PlanePlaneIntersection pA nA pB nB =
(
    dir= cross nA nB
    perp= cross nA dir
    p= pA + (dot (pB-pA) nB) * perp / (dot perp nB)
    ray p (normalize dir)
)

This is not checked in the code but if dir=[0,0,0] then the Planes are parallel…

Page 1 / 7