-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeom_formulae.py
212 lines (161 loc) · 5.63 KB
/
geom_formulae.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
__author__ = 'joseph'
from math import *
import math
def triangular_area(side1, side2):
"""
Calculate area of a triangle from the base and the height.
:@param side1:the base length
:@param side2:the height of the triangle
:@return:the area (units^2 from side)
>>>triangular_area(6, 9)
27.0
"""
return 1/2*side1*side2
def triangular_area_2(side1, side2, angle):
"""
calculate the area of a triangle given two sides and an angle.
:@param side1:a side length of the triangle
:@param side2:another side length of the triangle
:@param angle:included angle of the triangle
:@return:triangular area (units^2 from side)
>>>triangular_area_2(3, 7, 40)
7.823688185033163
"""
return 1/2*side1*side2*math.sin(angle)
def trapezium_area(side_a, side_b, side_c):
"""
calculate the area of a trapezium using two sides and the height.
:@param side_a:one of the sides of the trapezium
:@param side_b:one of the sides of the trapezium
:@param side_c:the height of the trapezium
:@return:the area (units^2 from side)
>>>trapezium_area(3, 4, 2)
7.0
"""
return 1/2*(side_a+side_b)*side_c
def trapezium_perimeter(side_a, side_b, side_c, side_d):
"""
calculate the perimeter of a four sided trapezium
:@param side_a:a side of the trapezium
:@param side_b:a side length
:@param side_c:a side length
:@param side_d:a side length
:@return:the perimeter (same units as side length)
>>>trapezium_perimeter(3, 4, 5, 6)
18
"""
return side_a+side_b+side_c+side_d
def cube_volume(side1, side2, side3):
"""
calculate the volume of a cube (three sides)
:@param side1:the length
:@param side2:the breadth
:@param side3:the height
:@return:volume of the cube (units^3 from side)
>>>cube_volume(2, 2, 2)
8
"""
return side1*side2*side3
def sphere_area(radius):
"""
calculate the area of sphere given its radius
:@param radius: radius
:@return:area of sphere (units^2 from radius)
>>>sphere_area(5)
314.1592653589793
"""
return 4*pi*radius**2
def rectangular_prism_area(length, width, height):
"""
calculate the area of a regular rectangular_prism
:@param length: the length of the rectangular_prism
:@param width: the width of the rectangular_prism
:@param height:the height of the rectangular_prism
:@return:area of rectangular_prism (units^2 from any of the sides)
>>>rectangular_prism_area(4, 2, 7)
100
"""
return 2*(width*length+height*length+height*width)
def rectangular_perimeter(length, width):
"""
calculate the perimeter of a rectangular block
:@param length: the length of the block
:@param width: the width of the block
:@return:perimeter (same unit as either length or width)
>>>rectangular_perimeter(5, 3)
16
"""
return 2*(length+width)
def cylindrical_volume(radius, height):
"""
calculating the volume of a cylinder given the radius and height
:@param radius: radius
:@param height: height
:@return:volume of cylinder (units^3 from any of the radius or height)
>>>cylindrical_volume(2, 7)
87.96459430051421
"""
return pi*radius**2*height
def right_circular_cone_volume(radius, height):
"""
calculate the volume of a right circular cone
:@param radius:radius of the cone
:@param height:height of the cone
:@return:volume of a right circular cone (units^3 from any of the given parameters)
>>>right_circular_cone_volume(2, 5)
20.943951023931955
"""
return pi*radius**2*height/3
def regular_hexagonal_area(side):
"""
calculate the area of a regular hexagon
:@param side: the side length of a regular hexagon
:@return:area of a regular hexagon (units^2 of a side)
>>>regular_hexagonal_area(4)
41.569219381653056
"""
return ((3*math.sqrt(3))/2)*side**2
def regular_tetrahedron_area(side):
"""
to calculate the area of a regular tetrahedron
:@param side:the side length of the given tetrahedron
:@return:area of a regular tetrahedron (units^2 of s side)
>>>regular_tetrahedron_area(5)
43.30127018922193
"""
return math.sqrt(3)*side**2
def regular_decagon_area(side):
"""
calculate the area of a regular decagon
:@param side:the side length of the decagon
:@return:area of a regular decagon (unit^2 of a side of the decagon)
>>>regular_decagon_area(4)
123.10734148701015
"""
return 5/2*side**2*(math.sqrt(5+2*(math.sqrt(5))))
if __name__ == "__main__":
print("triangular area:", triangular_area(6, 9))
if __name__ == "__main__":
print("triangular area_2:", triangular_area_2(3, 7, 40))
if __name__ == "__main__":
print("trapezium area:", trapezium_area(3, 4, 2))
if __name__ == "__main__":
print("trapezium perimeter:", trapezium_perimeter(3, 4, 5, 6))
if __name__ == "__main__":
print("cube volume:", cube_volume(2, 2, 2))
if __name__ == "__main__":
print("sphere area:", sphere_area(5))
if __name__ == "__main__":
print("rectangular prism area:", rectangular_prism_area(4, 2, 7))
if __name__ == "__main__":
print("rectangular perimeter:", rectangular_perimeter(5, 3))
if __name__ == "__main__":
print("cylindrical volume:", cylindrical_volume(2, 7))
if __name__ == "__main__":
print("right circular cone volume:", right_circular_cone_volume(2, 5))
if __name__ == "__main__":
print("regular hexagonal area:", regular_hexagonal_area(4))
if __name__ == "__main__":
print("regular tetrahedron area:", regular_tetrahedron_area(5))
if __name__ == "__main__":
print("regular decagon area:", regular_decagon_area(4))