-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreeBodyObjects.py
More file actions
86 lines (74 loc) · 2.99 KB
/
Copy pathFreeBodyObjects.py
File metadata and controls
86 lines (74 loc) · 2.99 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
# Recall in Coding Problem 4.4.4 (and before that, in Coding
# Problem 4.3.9) you built a program for finding the net
# force (magnitude and angle) on an object from several
# individual forces.
#
# In the next two exercises, we're going to convert that
# system into one that uses objects.
#
# To start, create a class called Force. The constructor for
# Force should have two required arguments: magnitude and
# angle. These should be saved to two attributes called
# 'magnitude' and 'angle'. You should assume angle is
# initially in degrees, from -180 to 180.
#
# Then, add three methods to Force:
#
# - get_horizontal should return the horizontal component
# of the force, according to the formula:
# horizontal = magnitude * cos(angle).
# - get_vertical should return the vertical component of
# the force, according to the formula:
# vertical = magnitude * sin(angle).
# - get_angle should return the angle of the force, but
# should have a keyword parameter called use_degrees.
# use_degrees should default to True. If use_degrees
# is true, it should return the angle in degrees; if it
# is false, it should return the angle in radians.
#
# HINT: Don't overcomplicate this. All we want here is
# a class called Force with four methods: __init__,
# get_horizontal, get_vertical, and get_angle. Note that
# these are not true "getters" even though they have "get"
# in their names: all three will have some reasoning
# beyond just returning a single value.
#
# HINT 2: angle will initially be passed into the
# constructor in degrees. You may store it in either
# degrees or radians. Each approach has different benefits,
# but make sure to keep track of when it's in angles and
# when it's in degrees.
# Add your code here!
from math import sin, cos, atan2, radians, degrees, sqrt
class Force:
def __init__(self, magnitude, angle):
self.magnitude = magnitude
self.angle = angle
def get_horizontal(self):
horizontal = self.magnitude * cos(radians(self.angle))
return horizontal
def get_vertical(self):
vertical = self.magnitude * sin(radians(self.angle) ) # in sin
return vertical
def get_angle(self, use_degrees=True):
if use_degrees==False:
return radians(self.angle)
else:
return self.angle
# Below are some lines of code that will test your object.
# You can change these lines to test your code in different
# ways.
#
# If your code works correctly, this will originally run
# error-free and print (with room for rounding errors):
# Magnitude: 500
# Horizontal: 250.0
# Vertical: 433.0127018922193
# Angle in Degrees: 60.0
# Angle in Radians: 1.0471975511965976
a_force = Force(500, 60)
print("Magnitude:", a_force.magnitude)
print("Horizontal:", a_force.get_horizontal())
print("Vertical:", a_force.get_vertical())
print("Angle in Degrees:", a_force.get_angle())
print("Angle in Radians:", a_force.get_angle(use_degrees=False))