Skip to content
test_Vector.py 4.71 KiB
Newer Older
Fabrice Salvaire's avatar
Fabrice Salvaire committed
####################################################################################################
#
Fabrice Salvaire's avatar
Fabrice Salvaire committed
# Patro - A Python library to make patterns for fashion design
Fabrice Salvaire's avatar
Fabrice Salvaire committed
# Copyright (C) 2017 Salvaire Fabrice
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
####################################################################################################

####################################################################################################

import unittest

from math import sqrt

Fabrice Salvaire's avatar
Fabrice Salvaire committed
from Patro.GeometryEngine.Vector import *
Fabrice Salvaire's avatar
Fabrice Salvaire committed

####################################################################################################

class TestVector2D(unittest.TestCase):

    ##############################################

    def _check_vector(self, vector, x ,y):
        self.assertEqual(vector.x, x)
        self.assertEqual(vector.y, y)

    ##############################################

    def test_ctor(self):
Fabrice Salvaire's avatar
Fabrice Salvaire committed

Fabrice Salvaire's avatar
Fabrice Salvaire committed

        v1 = Vector2D(x, y)
        self._check_vector(v1, x, y)
        v1 = Vector2D((x, y))
        self._check_vector(v1, x, y)
        v1 = Vector2D([x, y])
        self._check_vector(v1, x, y)
Fabrice Salvaire's avatar
Fabrice Salvaire committed

        self.assertEqual(v1, v1.clone())
Fabrice Salvaire's avatar
Fabrice Salvaire committed

        for angle in (20, 60, 100, 180):
            v1 = Vector2D.from_angle(angle)
            self.assertAlmostEqual(v1.orientation, angle)
            v1 = Vector2D.from_angle(-angle)
            self.assertAlmostEqual(v1.orientation, -angle)
Fabrice Salvaire's avatar
Fabrice Salvaire committed

    ##############################################

    def test_properties(self):

Fabrice Salvaire's avatar
Fabrice Salvaire committed

        v1 = Vector2D(x, y)
        magnitude_square = x**2 + y**2
        self.assertEqual(v1.magnitude_square, magnitude_square)
        self.assertEqual(v1.magnitude, sqrt(magnitude_square))
Fabrice Salvaire's avatar
Fabrice Salvaire committed

        v1 = Vector2D(x, 0)
        self.assertEqual(v1.orientation, 0)
        v1 = Vector2D(-x, 0)
        self.assertEqual(v1.orientation, 180)
        v1 = Vector2D(0, x)
        self.assertEqual(v1.orientation, 90)
        v1 = Vector2D(0, -x)
        self.assertEqual(v1.orientation, -90)
Fabrice Salvaire's avatar
Fabrice Salvaire committed

        v1 = Vector2D(x, x)
        self.assertEqual(v1.orientation, 45)
        v1 = Vector2D(x, -x)
        self.assertEqual(v1.orientation, -45)
        v1 = Vector2D(-x, x)
        self.assertEqual(v1.orientation, 135)
        v1 = Vector2D(-x, -x)
        self.assertEqual(v1.orientation, -135)
Fabrice Salvaire's avatar
Fabrice Salvaire committed

    ##############################################

    def test_math_operations(self):

        a, b, c = 10, 20, 30

        v1 = Vector2D(a, b)
        v2 = Vector2D(b, c)
        self.assertEqual(v1 + v2, Vector2D(a+b, b+c))
Fabrice Salvaire's avatar
Fabrice Salvaire committed

        v1 = Vector2D(a, b)
        v2 = Vector2D(b, c)
        self.assertEqual(v1 - v2, Vector2D(-a, -a))
Fabrice Salvaire's avatar
Fabrice Salvaire committed

        v1 = Vector2D(a, b)
        v1 += Vector2D(b, c)
        self.assertEqual(v1, Vector2D(a+b, b+c))
Fabrice Salvaire's avatar
Fabrice Salvaire committed

        v1 = Vector2D(a, b)
        v1 -= Vector2D(b, c)
        self.assertEqual(v1, Vector2D(-a, -a))

        v1 = Vector2D(a, b)
        self.assertEqual(v1 * a, Vector2D(a*a, b*a))

        v1 = Vector2D(a, b)
        v1 *= a
        self.assertEqual(v1, Vector2D(a*a, b*a))

        #?# v1 = Vector2D(a, b)
        #?# v1 /= a
        #?# self.assertEqual(v1, Vector2D(1, 2))

    ##############################################

    def test_two_vector_operations(self):
Fabrice Salvaire's avatar
Fabrice Salvaire committed

        x = 10

        v1 = Vector2D(x, x)
        v2 = Vector2D(-x, x)
Fabrice Salvaire's avatar
Fabrice Salvaire committed
        self.assertTrue(v1.is_orthogonal(v2))
Fabrice Salvaire's avatar
Fabrice Salvaire committed
        self.assertTrue(v1.is_orthogonal(v1.rotate(90)))
Fabrice Salvaire's avatar
Fabrice Salvaire committed

        v1 = Vector2D(x, x)
        self.assertTrue(v1.is_parallel(v1 * -x))
Fabrice Salvaire's avatar
Fabrice Salvaire committed
        self.assertTrue(v1.is_parallel(v1.rotate(180)))
Fabrice Salvaire's avatar
Fabrice Salvaire committed

        angle1 = 10
        direction = Vector2D.from_angle(angle1)
Fabrice Salvaire's avatar
Fabrice Salvaire committed
        perpendicular_direction = direction.rotate(90)
Fabrice Salvaire's avatar
Fabrice Salvaire committed
        for angle2 in range(-160, 180, 10):
            v2 = Vector2D.from_angle(angle2)

            self.assertAlmostEqual(v2.orientation_with(direction), angle2-angle1)

            v2x = direction * v2.projection_on(direction)
            v2y = perpendicular_direction * v2.deviation_with(direction)
            v3 = v2x + v2y
            self.assertTrue(v2.almost_equal(v3))

####################################################################################################

if __name__ == '__main__':

    unittest.main()