Code archives/3D Graphics - Mesh/TriSteepness

This code has been declared by its author to be Public Domain code.

Download source code

TriSteepness by ringwraith
(Posted 19 years ago)
This is a method of calculating the steepness (gradient) of a triangle by finding the slope of the edges of the triangle and returning the steepest one. It is substantially faster than a more complex method which I was using for a while that was posted in these archives. This takes about 0 - 1 milliseconds compared to 30 - 35 milliseconds using the more complex function. Of course, for the speed, some accuracy is sacrificed, but it is usually very accurate and the most I have seen it vary from the results of the more complex version is by about 0.07.
Note: If the triangle is vertical this function will not work.
Function TriSteepness#(surface,index)

	vert0 = TriangleVertex(surface,index,0)
	vert1 = TriangleVertex(surface,index,1)
	vert2 = TriangleVertex(surface,index,2)
	vert0to1# = VertSteepness#(surface,TriangleVertex(surface,index,0),surface,TriangleVertex(surface,index,1))
	vert0to2# = VertSteepness(surface,TriangleVertex(surface,index,0),surface,TriangleVertex(surface,index,2))
	vert1to2# = VertSteepness#(surface,TriangleVertex(surface,index,1),surface,TriangleVertex(surface,index,2))
	If vert0to1# => vert0to2# And vert0to1# => vert1to2# Then Return vert0to1#
	If vert0to2# => vert0to1# And vert0to2# => vert1to2# Then Return vert0to2#
	If vert1to2# => vert0to2# And vert1to2# => vert0to1# Then Return vert1to2#
	
End Function 

Function VertDist#(surface,index,x#,Z#)

	xdist# = VertexX(surface,index) - x
	Zdist# = VertexZ(surface,index) - Z
	
	Dist# = Sqr(Xdist^2 + Zdist^2)
	
	Return Dist#
	
End Function

Function VertSteepness#(surface,index,surface2,index2)

	vertgroundist# = VertDist#(surface,index,VertexX(surface2,index2),VertexZ(surface2,index2))
	vertupdist# = Abs(VertexY(surface2,index2) - VertexY(surface,index))
	slope# = vertupdist#/vertgroundist#
 
	Return slope#
	
End Function