Interpolate the normal of a point along a triangle

Miscellaneous Forums/General Discussion/Interpolate the normal of a point along a triangle

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I took a shot at coding the elegant way.
This is untested virgin code.
Here it is Josh, just for you.. 8)

Point=x,y,z

Triangle=
Ax,Ay,Az
Bx,By,Bz
Cx,Cy,Cz
Anx,Any,Anz
Bnx,Bny,Bnz
Cnx,Cny,Cnz

;---------------------------------------------------------
; Calculate Normal for point=x,y,z

; Calculate the total area, and that of each sub triangle
AreaT#=TriArea(Ax#,Ay#,Az#,Bx#,By#,Bz#,Cx#,Cy#,Cz#)
AreaB#=TriArea(Ax#,Ay#,Az#,x#,y#,z#,Cx#,Cy#,Cz#)
AreaC#=TriArea(Ax#,Ay#,Az#,x#,y#,z#,Bx#,By#,Bz#)
AreaA#=AreaT-AreaB-AreaC


; Calculate coefficients
c1#=AreaA#/AreaT#
c2#=AreaB#/AreaT#
c3#=AreaC#/AreaT#

; Calculate the new weighted normal
nx#=Anx#*c1+Bnx#*c2+Cnx#*c3
ny#=Any#*c1+Bny#*c2+Cny#*c3
nz#=Anz#*c1+Bnz#*c2+Cnz#*c3

;----------------------------------------------------
; Calculate area of triangle using Heron's Formula
Function TriArea(Ax#,Ay#,Az#,Bx#,By#,Bz#,Cx#,Cy#,Cz#)

; Calculate the side lengths
a#=sqr((Ax-Bx)^2+(Ay-By)^2+(Az-Bz)^2))
b#=sqr((Ax-Cx)^2+(Ay-Cy)^2+(Az-Cz)^2))
c#=sqr((Bx-Cx)^2+(By-Cy)^2+(Bz-Cz)^2))

; Calculate half the perimeter
s#=(1/2)*(a+b+c)

; Calculate total Area
Area#=sqr(s*(s-1)*(s-b)*s-c)

Return Area#
End function