September 28, 2021, 17:08:10

### Author Topic: [bb] TriSteepness by ringwraith [ 1+ years ago ]  (Read 1312 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] TriSteepness by ringwraith [ 1+ years ago ]
« on: June 29, 2017, 00:28:43 »
Title : TriSteepness
Author : ringwraith
Posted : 1+ years ago

Description : 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.

Code :
Code: BlitzBasic
1. Function TriSteepness#(surface,index)
2.
3.         vert0 = TriangleVertex(surface,index,0)
4.         vert1 = TriangleVertex(surface,index,1)
5.         vert2 = TriangleVertex(surface,index,2)
6.         vert0to1# = VertSteepness#(surface,TriangleVertex(surface,index,0),surface,TriangleVertex(surface,index,1))
7.         vert0to2# = VertSteepness(surface,TriangleVertex(surface,index,0),surface,TriangleVertex(surface,index,2))
8.         vert1to2# = VertSteepness#(surface,TriangleVertex(surface,index,1),surface,TriangleVertex(surface,index,2))
9.         If vert0to1# => vert0to2# And vert0to1# => vert1to2# Then Return vert0to1#
10.         If vert0to2# => vert0to1# And vert0to2# => vert1to2# Then Return vert0to2#
11.         If vert1to2# => vert0to2# And vert1to2# => vert0to1# Then Return vert1to2#
12.
13. End Function
14.
15. Function VertDist#(surface,index,x#,Z#)
16.
17.         xdist# = VertexX(surface,index) - x
18.         Zdist# = VertexZ(surface,index) - Z
19.
20.         Dist# = Sqr(Xdist^2 + Zdist^2)
21.
22.         Return Dist#
23.
24. End Function
25.
26. Function VertSteepness#(surface,index,surface2,index2)
27.
28.         vertgroundist# = VertDist#(surface,index,VertexX(surface2,index2),VertexZ(surface2,index2))
29.         vertupdist# = Abs(VertexY(surface2,index2) - VertexY(surface,index))
30.         slope# = vertupdist#/vertgroundist#
31.
32.         Return slope#
33.
34. End Function

Stevie G(Posted 1+ years ago)

Better and quicker just to calculate the triangle Y normal component, rather than the steepest edge, like so ...Where  >0 = Flat up ,  <0 = flat down, 0 = steepest / vertical
Code: [Select]
`Function TriangleNY#( s , t ) v0 = TriangleVertex( s, t, 0 ) v1 = TriangleVertex( s, t, 1 ) v2 = TriangleVertex( s, t, 2 ) ax# = VertexX( s, v1 ) - VertexX( s, v0 ) ay# = VertexY( s, v1 ) - VertexY( s, v0 ) az# = VertexZ( s, v1 ) - VertexZ( s, v0 ) bx# = VertexX( s, v2 ) - VertexX( s, v1 ) by# = VertexY( s, v2 ) - VertexY( s, v1 ) bz# = VertexZ( s, v2 ) - VertexZ( s, v1 ) Nx# = ( ay * bz ) - ( az * by ) Ny# = ( az * bx ) - ( ax * bz ) Nz# = ( ax * by ) - ( ay * bx ) Ns# = Sqr( Nx * Nx + Ny * Ny + Nz * Nz )               if Ns > 0      Return Ny / Ns        else           return 0        endifEnd Function`Stevie

ringwraith(Posted 1+ years ago)

Oh cool, you learn something new everyday! [/i]