[go: up one dir, main page]

Menu

[e79954]: / scripts / geometry.py  Maximize  Restore  History

Download this file

154 lines (130 with data), 4.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Tread - a track editor for Vamos Automotive Simulator
# Copyright (C) 2010 Sam Varner
#
# 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 math
import unittest
full_circle = 2.0 * math.pi
def distance (p1, p2):
delta = p2 - p1
return math.sqrt (delta.x**2 + delta.y**2)
class Point:
def __init__ (self, x, y):
self.x = x
self.y = y
def __add__ (self, other):
return Point (self.x + other.x, self.y + other.y)
def __sub__ (self, other):
return Point (self.x - other.x, self.y - other.y)
def __mul__ (self, number):
return Point (self.x * number, self.y * number)
def __div__ (self, number):
return self * (1.0 / number)
def __iadd__ (self, other):
self = self + other;
return self
def __isub__ (self, other):
self = self - other;
return self
def __abs__ (self):
return distance (Point (0, 0), self)
def __eq__ (self, other):
return self.x == other.x and self.y == other.y
class Circle:
def __init__ (self, center = Point (0, 0), edge = Point (0, 0)):
self.center = center
self.edge = edge
def move_to (self, new_center):
delta = new_center - self.center
self.center += delta
self.edge += delta
def set_center (self, p):
self.center = p
def set_edge (self, p):
self.edge = p
def radius (self):
return distance (self.center, self.edge)
def distance (self, p):
return distance (p, self.center)
def edge_distance (self, p):
return distance (p, self.edge)
### Tests
class Test_Point (unittest.TestCase):
def setUp (self):
self.p1 = Point (1, 2)
self.p2 = Point (3, -3)
def test_access (self):
self.assertEqual (self.p1.x, 1)
self.assertEqual (self.p1.y, 2)
self.p1.x = 6
self.p1.y = 7
self.assertEqual (self.p1.x, 6)
self.assertEqual (self.p1.y, 7)
def test_add (self):
p = self.p1 + self.p2
self.assertEqual (p.x, 4)
self.assertEqual (p.y, -1)
p += self.p2
self.assertEqual (p.x, 7)
self.assertEqual (p.y, -4)
def test_subtract (self):
p = self.p1 - self.p2
self.assertEqual (p.x, -2)
self.assertEqual (p.y, 5)
p -= self.p2
self.assertEqual (p.x, -5)
self.assertEqual (p.y, 8)
def test_multiply (self):
p = self.p1 * 2
self.assertEqual (p.x, 2)
self.assertEqual (p.y, 4)
def test_divide (self):
p = self.p1 / 2
self.assertEqual (p.x, 0.5)
self.assertEqual (p.y, 1)
def test_abs (self):
self.assertEqual (abs (self.p1), math.sqrt (5))
def test_equality (self):
self.assertFalse (self.p1 == self.p2)
p3 = self.p1
self.assertTrue (self.p1 == p3)
class Test_Circle (unittest.TestCase):
def setUp (self):
self.c = Circle (Point (5, 4), Point (2, 0))
def test_access (self):
self.assertEqual (self.c.center, Point (5, 4))
self.assertEqual (self.c.edge, Point (2, 0))
def test_move (self):
p = Point (3, 2)
c = self.c.move_to (p)
self.assertEqual (self.c.center, p)
self.assertEqual (self.c.edge, Point (0, -2))
def test_set_center (self):
p = Point (3, 2)
c = self.c.set_center (p)
self.assertEqual (self.c.center, p)
self.assertEqual (self.c.edge, Point (2, 0))
def test_set_edge (self):
p = Point (3, 2)
c = self.c.set_edge (p)
self.assertEqual (self.c.center, Point (5, 4))
self.assertEqual (self.c.edge, p)
def test_radius (self):
self.assertEqual (self.c.radius (), 5)
def test_distance (self):
self.assertEqual (self.c.distance (Point (-3, -2)), 10)
self.assertEqual (self.c.edge_distance (Point (1, 1)), math.sqrt (2))
point_suite = unittest.TestLoader().loadTestsFromTestCase (Test_Point)
circle_suite = unittest.TestLoader().loadTestsFromTestCase (Test_Circle)
suite = unittest.TestSuite ([point_suite, circle_suite])