diff --git a/.gitattributes b/.gitattributes index c44cbcf..e1abb9c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -187,6 +187,7 @@ invesalius/gui/widgets/platebtn.py -text invesalius/gui/widgets/slice_menu.py -text invesalius/i18n.py -text invesalius/invesalius.py -text +invesalius/math_utils.py -text invesalius/presets.py -text invesalius/project.py -text invesalius/reader/__init__.py -text diff --git a/invesalius/math_utils.py b/invesalius/math_utils.py new file mode 100644 index 0000000..dfe9a86 --- /dev/null +++ b/invesalius/math_utils.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import math +import numpy + +def calculate_distance(p1, p2): + """ + Calculates the euclidian distance between p1 and p2 points. + + >>> calculate_distance((0, 0), (1, 0)) + 1.0 + + >>> calculate_distance((0, 0), (0, 1)) + 1.0 + """ + return math.sqrt(sum([(j-i)**2 for i,j in zip(p1, p2)])) + +def calculate_angle(v1, v2): + """ + Calculates the angle formed between vector v1 and v2. + + >>> calculate_angle((0, 1), (1, 0)) + 90.0 + + >>> calculate_angle((1, 0), (0, 1)) + 90.0 + """ + cos_ = numpy.dot(v1, v2)/(numpy.linalg.norm(v1)*numpy.linalg.norm(v2)) + angle = math.degrees(math.acos(cos_)) + return angle + +if __name__ == '__main__': + import doctest + doctest.testmod() -- libgit2 0.21.2