Skip to content

Instantly share code, notes, and snippets.

@bicycle1885
Last active July 4, 2023 04:20
Show Gist options
  • Save bicycle1885/c37ed1fd18bae6a61af6a05b293f3b05 to your computer and use it in GitHub Desktop.
Save bicycle1885/c37ed1fd18bae6a61af6a05b293f3b05 to your computer and use it in GitHub Desktop.
Dihedral angle
# Public Domain
using LinearAlgebra: cross, dot, norm
"""
dihedral(r1, r2, r3, r4)
Compute the dihedral angle of two planes defined by (`r1`, `r2`, `r3`) and
(`r2`, `r3`, `r4`).
Reference: https://en.wikipedia.org/wiki/Dihedral_angle
"""
function dihedral(r1, r2, r3, r4)
u1 = r2 - r1
u2 = r3 - r2
u3 = r4 - r3
x = cross(u1, u2)
y = cross(u2, u3)
return atan(norm(u2) * dot(u1, y), dot(x, y))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment