Skip to content
####################################################################################################
#
# Patro - A Python library to make patterns for fashion design
# Copyright (C) 2019 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 Patro.GeometryEngine.Triangle import *
from Patro.GeometryEngine.Vector import Vector2D
####################################################################################################
class TestTriangle(unittest.TestCase):
##############################################
def test(self):
pass
####################################################################################################
if __name__ == '__main__':
unittest.main()
......@@ -32,97 +32,111 @@ class TestVector2D(unittest.TestCase):
##############################################
def test(self):
v1 = Vector2D(10, 20)
v1 = Vector2D((10, 20))
v1 = Vector2D([10, 20])
v1 = Vector2D(10, 20)
self.assertEqual(v1.x, 10)
self.assertEqual(v1.y, 20)
self.assertEqual(v1, v1.copy())
v1 = Vector2D(10, 20)
v2 = Vector2D(20, 30)
self.assertEqual(v1 + v2, Vector2D(30, 50))
v1 = Vector2D(10, 20)
v2 = Vector2D(20, 30)
self.assertEqual(v1 - v2, Vector2D(-10, -10))
v1 = Vector2D(10, 20)
v1 += Vector2D(20, 30)
self.assertEqual(v1, Vector2D(30, 50))
v1 = Vector2D(10, 20)
v1 -= Vector2D(20, 30)
self.assertEqual(v1, Vector2D(-10, -10))
v1 = Vector2D(10, 20)
self.assertEqual(v1 * 10, Vector2D(100, 200))
v1 = Vector2D(10, 20)
v1 *= 10
self.assertEqual(v1, Vector2D(100, 200))
#?# v1 = Vector2D(10, 20)
#?# v1 /= 10
#?# self.assertEqual(v1, Vector2D(1, 2))
v1 = Vector2D(10, 20)
self.assertEqual(v1.magnitude_square(), 10**2 + 20**2)
v1 = Vector2D(10, 20)
self.assertEqual(v1.magnitude(), sqrt(10**2 + 20**2))
v1 = Vector2D(10, 0)
self.assertEqual(v1.orientation(), 0)
v1 = Vector2D(-10, 0)
v1.orientation()
self.assertEqual(v1.orientation(), 180)
v1 = Vector2D(0, 10)
self.assertEqual(v1.orientation(), 90)
v1 = Vector2D(0, -10)
self.assertEqual(v1.orientation(), -90)
v1 = Vector2D(10, 10)
self.assertEqual(v1.orientation(), 45)
v1 = Vector2D(10, -10)
self.assertEqual(v1.orientation(), -45)
v1 = Vector2D(-10, 10)
self.assertEqual(v1.orientation(), 135)
v1 = Vector2D(-10, -10)
self.assertEqual(v1.orientation(), -135)
v1 = Vector2D.from_angle(25)
self.assertAlmostEqual(v1.orientation(), 25)
v1 = Vector2D.from_angle(-25)
self.assertAlmostEqual(v1.orientation(), -25)
v1 = Vector2D.from_angle(60)
self.assertAlmostEqual(v1.orientation(), 60)
v1 = Vector2D.from_angle(-60)
self.assertAlmostEqual(v1.orientation(), -60)
v1 = Vector2D.from_angle(100)
self.assertAlmostEqual(v1.orientation(), 100)
v1 = Vector2D.from_angle(-100)
self.assertAlmostEqual(v1.orientation(), -100)
v1 = Vector2D.from_angle(160)
self.assertAlmostEqual(v1.orientation(), 160)
v1 = Vector2D.from_angle(-160)
self.assertAlmostEqual(v1.orientation(), -160)
v1 = Vector2D(10, 10)
v2 = Vector2D(-10, 10)
def _check_vector(self, vector, x ,y):
self.assertEqual(vector.x, x)
self.assertEqual(vector.y, y)
##############################################
def test_ctor(self):
x, y = 10, 20
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)
self.assertEqual(v1, v1.clone())
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)
##############################################
def test_properties(self):
x, y = 10, 20
v1 = Vector2D(x, y)
magnitude_square = x**2 + y**2
self.assertEqual(v1.magnitude_square, magnitude_square)
self.assertEqual(v1.magnitude, sqrt(magnitude_square))
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)
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)
##############################################
def test_math_operations(self):
a, b, c = 10, 20, 30
# unary -
v1 = Vector2D(a, b)
self.assertEqual(-v1, Vector2D(-a, -b))
# binary + +=
v1 = Vector2D(a, b)
v2 = Vector2D(b, c)
self.assertEqual(v1 + v2, Vector2D(a+b, b+c))
self.assertEqual(v1 - v2, Vector2D(a-b, b-c))
v1 = Vector2D(a, b)
v1 += Vector2D(b, c)
self.assertEqual(v1, Vector2D(a+b, b+c))
v1 = Vector2D(a, b)
v1 -= Vector2D(b, c)
self.assertEqual(v1, Vector2D(a-b, b-c))
# scale *
v1 = Vector2D(a, b)
v2 = Vector2D(a*a, b*a)
self.assertEqual(v1 * a, v2)
self.assertEqual(a * v1, v2)
v1 = Vector2D(a, b)
v1 *= a
self.assertEqual(v1, v2)
v1 = Vector2D(a, b)
v1 /= a
self.assertEqual(v1, Vector2D(1, b/a))
##############################################
def test_two_vector_operations(self):
x = 10
v1 = Vector2D(x, x)
v2 = Vector2D(-x, x)
self.assertTrue(v1.is_orthogonal(v2))
self.assertTrue(v1.is_orthogonal(v1.rotate(90)))
v1 = Vector2D(10, 10)
self.assertTrue(v1.is_parallel(v1 * -10))
v1 = Vector2D(x, x)
self.assertTrue(v1.is_parallel(v1 * -x))
self.assertTrue(v1.is_parallel(v1.rotate(180)))
angle1 = 10
......