# Calculating shape slopes

Discussion created by MBuehler-esristaff on Feb 27, 2012
hi !

with the following code, you can visualize the slope (relative to 'world-up'). the calculation is done via some vector math functions which check the orientation of the scope and compare it agains a vector which is defined as upVector.

to make sure this works on single shapes (polygon faces), I've put in a comp(f) {all : .. }

note that the display of the colors created by the Colorize rule at the bottom of the code is not correct in the viewport since it is still shaded with the default scene light. ;)

##################################################################################################################
# vector math functions
##################################################################################################################

upVector(dim) = case dim == "x" : 0 case dim == "y" : 1 else : 0

scopeYNormal(dim) =
case dim == "x" :  convert(x, scope, world, pos, 0, 1, 0) - convert(x, scope, world, pos, 0, 0, 0)
case dim == "y" :  convert(y, scope, world, pos, 0, 1, 0) - convert(y, scope, world, pos, 0, 0, 0)
else :     convert(z, scope, world, pos, 0, 1, 0) - convert(z, scope, world, pos, 0, 0, 0)

dotProduct(x1,y1,z1,x2,y2,z2) = x1 * x2 + y1 * y2 + z1 * z2

angleBetweenVectors =
case acos ( dotProduct (scopeYNormal("x"),scopeYNormal("y"),scopeYNormal("z"),upVector("x"),upVector("y"),upVector("z")) )
< 0.001 : 0
case acos ( dotProduct (scopeYNormal("x"),scopeYNormal("y"),scopeYNormal("z"),upVector("x"),upVector("y"),upVector("z")) )
> 89.999 : 90
else :
acos ( dotProduct (scopeYNormal("x"),scopeYNormal("y"),scopeYNormal("z"),upVector("x"),upVector("y"),upVector("z")) )

##################################################################################################################
# color function
##################################################################################################################

angleToColor(val) = 1 - (val / 90 )

##################################################################################################################
# start
##################################################################################################################

@StartRule
VisualizeSlope -->
comp(f)
{ all:
alignScopeToGeometry(yUp, world.lowest, world.lowest)
Colorize(angleBetweenVectors)
}

Colorize(val) -->
# print val
color(angleToColor(val),angleToColor(val),angleToColor(val))