Skip to content

Commit ca58e87

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

19 files changed

+376
-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

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

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

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

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

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
18+
@abstractmethod
19+
def split(self):
20+
"""
21+
a method for splitting a shape
22+
into a certain quantity of pieces,
23+
following a specific algorithm
24+
"""
25+
26+
"""
27+
to do: create a factory method for creating a specific shape
28+
@abstractmethod
29+
def create_shape(self):
30+
pass"""

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

Whitespace-only changes.

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

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

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

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

Diff for: geometry/shapes/side.py

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

0 commit comments

Comments
 (0)