Skip to content

Commit 1b1904f

Browse files
committed
adding a geometry module
1 parent 5fb6496 commit 1b1904f

19 files changed

+371
-0
lines changed

Diff for: geometry/__init__.py

Whitespace-only changes.

Diff for: geometry/angles/__init__.py

Whitespace-only changes.

Diff for: geometry/angles/angle.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class Angle:
6+
"""
7+
representation of an Angle in degrees (unit of measurement)
8+
"""
9+
10+
degrees: float

Diff for: geometry/coordinate_plane/__init__.py

Whitespace-only changes.

Diff for: geometry/lines/__init__.py

Whitespace-only changes.

Diff for: geometry/shapes/__init__.py

Whitespace-only changes.

Diff for: geometry/shapes/ellipses/__init__.py

Whitespace-only changes.

Diff for: geometry/shapes/ellipses/circle.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from ..shape_types.closed_shapes import closedShape
2+
import math
3+
4+
5+
class Circle(closedShape):
6+
7+
"""
8+
a structure which represents a
9+
geometrical circle on a 2D surface
10+
11+
>>> circle_one = Circle(5)
12+
>>> circle_one.get_diameter()
13+
10
14+
>>> circle_one.perimeter()
15+
31.41592653589793
16+
>>> circle_one.is_similar(None)
17+
Traceback (most recent call last):
18+
NotImplementedError: Not Implemented
19+
>>> circle_one.split()
20+
Traceback (most recent call last):
21+
NotImplementedError: Not Implemented
22+
>>> circle_one.max_parts(54)
23+
1486.0
24+
>>> circle_one.max_parts(7)
25+
29.0
26+
>>> circle_one.max_parts(22.5)
27+
265.375
28+
>>> circle_one.max_parts(-222)
29+
-1
30+
>>> circle_one.max_parts("-222")
31+
Traceback (most recent call last):
32+
TypeError: num_cuts must be a numeric value.
33+
"""
34+
35+
def __init__(self, radius):
36+
self.radius = radius
37+
self.origin = 0
38+
39+
def get_diameter(self):
40+
return self.radius * 2
41+
42+
def perimeter(self):
43+
return 2 * math.pi * self.radius
44+
45+
def area(self):
46+
return math.pi * (self.radius**2)
47+
48+
def is_similar(self, compared_shape):
49+
raise NotImplementedError("Not Implemented")
50+
51+
def split(self):
52+
raise NotImplementedError("Not Implemented")
53+
54+
def max_parts(self, num_cuts: float) -> float:
55+
"""
56+
returns the maximum amount of
57+
parts a circle can be divided
58+
by if cut 'num_cuts' times
59+
"""
60+
61+
if not isinstance(num_cuts, (int, float)):
62+
raise TypeError("num_cuts must be a numeric value.")
63+
return ((num_cuts + 2 + num_cuts**2) * 0.5) if num_cuts >= 0 else -1

Diff for: geometry/shapes/ellipses/ellipse.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from ..shape_types.closed_shapes import closedShape
2+
import math
3+
4+
5+
class Ellipse(closedShape):
6+
7+
"""
8+
a structure which represents a
9+
geometrical ellipse on a 2D surface
10+
11+
>>> ellipse_one = Ellipse(5, 10)
12+
>>> ellipse_one.perimeter()
13+
47.12388980384689
14+
>>> ellipse_one.is_similar(None)
15+
Traceback (most recent call last):
16+
NotImplementedError: Not Implemented
17+
>>> ellipse_one.split()
18+
Traceback (most recent call last):
19+
NotImplementedError: Not Implemented
20+
"""
21+
22+
def __init__(self, major_radius, minor_radius):
23+
self.major_radius: float = major_radius
24+
self.minor_radius: float = minor_radius
25+
26+
def perimeter(self):
27+
return math.pi * (self.major_radius + self.minor_radius)
28+
29+
def area(self):
30+
return math.pi * self.major_radius * self.minor_radius
31+
32+
def is_similar(self, compared_shape):
33+
raise NotImplementedError("Not Implemented")
34+
35+
def split(self):
36+
raise NotImplementedError("Not Implemented")

Diff for: geometry/shapes/polygon/__init__.py

Whitespace-only changes.

Diff for: geometry/shapes/polygon/polygon.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from ..shape_types.closed_shapes import closedShape
2+
from ..side import Side
3+
from ..shape_types.intersecting_self_shapes import intersectSelfShape
4+
from ...angles.angle import Angle
5+
6+
7+
class Polygon(closedShape, intersectSelfShape):
8+
9+
"""
10+
an abstract class which represents a
11+
set of polygons on a 2D surface
12+
13+
>>> polygon_one = Polygon()
14+
>>> side_length = 15
15+
>>> angle_degrees = 60
16+
>>> side_one = Side([], Angle(angle_degrees), side_length)
17+
>>> side_two = Side([], Angle(angle_degrees), side_length)
18+
>>> side_three = Side([], Angle(angle_degrees), side_length)
19+
>>> side_one.adjacent_sides.append(side_two)
20+
>>> side_one.adjacent_sides.append(side_three)
21+
>>> side_two.adjacent_sides.append(side_three)
22+
>>> side_two.adjacent_sides.append(side_one)
23+
>>> side_three.adjacent_sides.append(side_one)
24+
>>> side_three.adjacent_sides.append(side_two)
25+
>>> polygon_one.add_side(side_one)
26+
>>> polygon_one.add_side(side_two)
27+
>>> polygon_one.add_side(side_three)
28+
>>> polygon_one.set_side(0, side_one)
29+
>>> polygon_one.set_side(1, side_two)
30+
>>> polygon_one.set_side(2, side_three)
31+
>>> side_one_data = polygon_one.get_side(0)
32+
>>> print(side_one_data.length)
33+
15
34+
>>> print(side_one_data.angle.degrees)
35+
60
36+
>>> polygon_one.area()
37+
38+
>>> polygon_one.is_similar(None)
39+
40+
>>> polygon_one.split()
41+
42+
>>> polygon_one.perimeter()
43+
44+
"""
45+
46+
def __init__(self):
47+
self.sides: list[Side] = []
48+
49+
def get_side(self, index):
50+
return self.sides[index]
51+
52+
def set_side(self, index, side):
53+
self.sides[index] = side
54+
55+
def add_side(self, side):
56+
self.sides.append(side)
57+
58+
def area(self):
59+
pass
60+
61+
def is_similar(self, compared_shape):
62+
pass
63+
64+
def perimeter(self):
65+
pass
66+
67+
def split(self):
68+
pass

Diff for: geometry/shapes/polygon/rectangle.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from .polygon import Polygon
2+
from ..side import Side
3+
from ...angles.angle import Angle
4+
5+
6+
class Rectangle(Polygon):
7+
"""
8+
a structure which represents a
9+
geometrical rectangle on a 2D surface
10+
11+
>>> rectangle_one = Rectangle(5, 10)
12+
>>> rectangle_one.perimeter()
13+
30
14+
>>> rectangle_one.area()
15+
50
16+
>>> rectangle_one.is_similar(None)
17+
Traceback (most recent call last):
18+
NotImplementedError: Not Implemented
19+
>>> rectangle_one.split()
20+
Traceback (most recent call last):
21+
NotImplementedError: Not Implemented
22+
"""
23+
24+
def __init__(self, short_side_length, long_side_length):
25+
super().__init__()
26+
self.short_side = Side([], Angle(90), short_side_length)
27+
self.long_side = Side([], Angle(90), long_side_length)
28+
super().add_side(self.short_side)
29+
super().add_side(self.long_side)
30+
31+
def perimeter(self):
32+
return (self.short_side.length + self.long_side.length) * 2
33+
34+
def area(self):
35+
return self.short_side.length * self.long_side.length
36+
37+
def is_similar(self, compared_shape):
38+
raise NotImplementedError("Not Implemented")
39+
40+
def split(self):
41+
raise NotImplementedError("Not Implemented")

Diff for: geometry/shapes/polygon/square.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from .rectangle import Rectangle
2+
3+
4+
class Square(Rectangle):
5+
"""
6+
a structure which represents a
7+
geometrical square on a 2D surface
8+
9+
>>> square_one = Square(5)
10+
>>> square_one.perimeter()
11+
20
12+
>>> square_one.area()
13+
25
14+
>>> square_one.is_similar(None)
15+
Traceback (most recent call last):
16+
NotImplementedError: Not Implemented
17+
>>> square_one.split()
18+
Traceback (most recent call last):
19+
NotImplementedError: Not Implemented
20+
"""
21+
22+
def __init__(self, side_length):
23+
super().__init__(side_length, side_length)
24+
25+
def perimeter(self):
26+
return super().perimeter()
27+
28+
def area(self):
29+
return super().area()
30+
31+
def is_similar(self, compared_shape):
32+
raise NotImplementedError("Not Implemented")
33+
34+
def split(self):
35+
raise NotImplementedError("Not Implemented")

Diff for: geometry/shapes/shape.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class shape(ABC):
5+
6+
"""
7+
interface which represents a geometrical shape
8+
at a global level
9+
"""
10+
11+
@abstractmethod
12+
def is_similar(self, compared_shape):
13+
"""
14+
a method for comparing with another shape,
15+
which also implements this interface
16+
"""
17+
pass
18+
19+
@abstractmethod
20+
def split(self):
21+
"""
22+
a method for splitting a shape
23+
into a certain quantity of pieces,
24+
following a specific algorithm
25+
"""
26+
pass
27+
28+
"""
29+
to do: create a factory method for creating a specific shape
30+
@abstractmethod
31+
def create_shape(self):
32+
pass"""

Diff for: geometry/shapes/shape_types/__init__.py

Whitespace-only changes.

Diff for: geometry/shapes/shape_types/closed_shapes.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from abc import ABC, abstractmethod
2+
from ..shape import shape
3+
4+
5+
class closedShape(shape, ABC):
6+
"""
7+
an interface which represents shapes
8+
that start and end at the same point [closed shape]
9+
more info: https://en.wikipedia.org/wiki/Polygon
10+
"""
11+
12+
@abstractmethod
13+
def perimeter(self):
14+
pass
15+
16+
@abstractmethod
17+
def area(self):
18+
pass
19+
20+
@abstractmethod
21+
def is_similar(self, compared_shape):
22+
pass
23+
24+
@abstractmethod
25+
def split(self):
26+
pass
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from abc import ABC, abstractmethod
2+
from ..shape import shape
3+
4+
5+
class intersectSelfShape(shape, ABC):
6+
"""
7+
an interface which represents polygons
8+
some of whose edges cross each other [self intersecting shapes]
9+
more info: https://en.wikipedia.org/wiki/Polygon
10+
"""
11+
12+
@abstractmethod
13+
def perimeter(self):
14+
pass
15+
16+
@abstractmethod
17+
def area(self):
18+
pass
19+
20+
@abstractmethod
21+
def is_similar(self, compared_shape):
22+
pass
23+
24+
@abstractmethod
25+
def split(self):
26+
pass

Diff for: geometry/shapes/shape_types/open_shapes.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from abc import abstractmethod
2+
import shape
3+
4+
5+
class OpenShape(shape, ABC):
6+
"""
7+
an interface which represents shapes or figures
8+
with different starting and ending points [open shapes]
9+
more info: https://en.wikipedia.org/wiki/Polygon
10+
"""
11+
12+
@abstractmethod
13+
def is_similar(self, compared_shape):
14+
pass
15+
16+
@abstractmethod
17+
def split(self):
18+
pass

Diff for: geometry/shapes/side.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from ..angles.angle import Angle
2+
from dataclasses import dataclass
3+
4+
5+
@dataclass
6+
class Side:
7+
8+
"""
9+
represents a side of a shape [such as polygon, e.t.c.]
10+
adjacent_sides: a list of sides which are adjacent to the current side
11+
angle: the angle between adjacent sides
12+
"""
13+
14+
adjacent_sides: list[float]
15+
angle: Angle
16+
length: float

0 commit comments

Comments
 (0)