# Python Function to Discover Triangle Slope

I need to write a Python function that computes the direction a triangle slopes. More specifically, I need to be able to compute which way a marble would roll if placed on a triangle defined by three points and at what point the marble would fall off the edge of that triangle.

Unfortunately, the math is beyond me. Any help would be much appreciated.

Here's a diagram that helps illustrate the situation:

Thanks.

Okay, here's a start:

import bpy
from mathutils import Vector

a = Vector((..., ..., ...))
b = Vector((..., ..., ...))
c = Vector((..., ..., ...))

ab = b - a
ac = c - a

normal = ab.cross(ac)
normal.normalize()

# assuming Z is up
slope_dir = normal.xy

# Heres 3 methods you could use to measure slope amount...

# measure xy length (0-1) where 1 is flat.
slope_amount = slope_dir.length
# so you can tell if the slope points up or down...
slope_amount = slope_dir.length * (1.0 if normal.z > 0.0 else -1.0)

# use normal z axis, 1.0 will be up, -1.0 down.
slope_amount = normal.z

# measure slope angle (0 - pi) where 0 is flat.
slope_amount = Vector((0.0, 0.0, 1.0)).angle(normal)

• Thanks for the kick-start! I'll see if I can get somewhere with that. I'd really rather not have the mathutils dependency, though, in case I want to use the function in a non-Blender project. Jan 9, 2014 at 17:07
• Shouldn't be much of a problem to recreate the Vector type with python, but better not do that until you really have to. Jan 9, 2014 at 17:41