Commit 3977a49c09db1eab6a1973d4a44508e6b0bd1546
1 parent
b90e16c5
Exists in
master
and in
68 other branches
Add: a module that contains math functions
Showing
2 changed files
with
36 additions
and
0 deletions
Show diff stats
.gitattributes
| @@ -187,6 +187,7 @@ invesalius/gui/widgets/platebtn.py -text | @@ -187,6 +187,7 @@ invesalius/gui/widgets/platebtn.py -text | ||
| 187 | invesalius/gui/widgets/slice_menu.py -text | 187 | invesalius/gui/widgets/slice_menu.py -text |
| 188 | invesalius/i18n.py -text | 188 | invesalius/i18n.py -text |
| 189 | invesalius/invesalius.py -text | 189 | invesalius/invesalius.py -text |
| 190 | +invesalius/math_utils.py -text | ||
| 190 | invesalius/presets.py -text | 191 | invesalius/presets.py -text |
| 191 | invesalius/project.py -text | 192 | invesalius/project.py -text |
| 192 | invesalius/reader/__init__.py -text | 193 | invesalius/reader/__init__.py -text |
| @@ -0,0 +1,35 @@ | @@ -0,0 +1,35 @@ | ||
| 1 | +#!/usr/bin/env python | ||
| 2 | +# -*- coding: utf-8 -*- | ||
| 3 | + | ||
| 4 | +import math | ||
| 5 | +import numpy | ||
| 6 | + | ||
| 7 | +def calculate_distance(p1, p2): | ||
| 8 | + """ | ||
| 9 | + Calculates the euclidian distance between p1 and p2 points. | ||
| 10 | + | ||
| 11 | + >>> calculate_distance((0, 0), (1, 0)) | ||
| 12 | + 1.0 | ||
| 13 | + | ||
| 14 | + >>> calculate_distance((0, 0), (0, 1)) | ||
| 15 | + 1.0 | ||
| 16 | + """ | ||
| 17 | + return math.sqrt(sum([(j-i)**2 for i,j in zip(p1, p2)])) | ||
| 18 | + | ||
| 19 | +def calculate_angle(v1, v2): | ||
| 20 | + """ | ||
| 21 | + Calculates the angle formed between vector v1 and v2. | ||
| 22 | + | ||
| 23 | + >>> calculate_angle((0, 1), (1, 0)) | ||
| 24 | + 90.0 | ||
| 25 | + | ||
| 26 | + >>> calculate_angle((1, 0), (0, 1)) | ||
| 27 | + 90.0 | ||
| 28 | + """ | ||
| 29 | + cos_ = numpy.dot(v1, v2)/(numpy.linalg.norm(v1)*numpy.linalg.norm(v2)) | ||
| 30 | + angle = math.degrees(math.acos(cos_)) | ||
| 31 | + return angle | ||
| 32 | + | ||
| 33 | +if __name__ == '__main__': | ||
| 34 | + import doctest | ||
| 35 | + doctest.testmod() |