-
Notifications
You must be signed in to change notification settings - Fork 1
/
course_by_wind.py
121 lines (80 loc) · 3.16 KB
/
course_by_wind.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
# !/usr/bin/env python
"""
Test case for "course by wind" function.
"""
import math
import sys
import random
def course_by_wind(windRel, angle):
"""Plan course by angle of wind relative to boat."""
rudder = 99
main = 99
jib = 99
side = 1
if(angle < 0):
side = -side
theta = math.fabs(angle) - math.fabs(windRel)
rudder = math.floor((35.0 / 180.0) * theta)
rudder = rudder * side
if(math.fabs(windRel) < 45):
main = 0
jib = 0
else:
sailPos = (math.fabs(windRel) - 45) * 0.66
main = math.floor(sailPos)
jib = math.floor(sailPos)
return (main, jib, rudder)
def postcondition(result):
"""The condition following."""
assert result[0] >= 0 and result[0] <= 90
assert result[1] >= 0 and result[1] <= 90
assert result[2] >= -35 and result[2] <= 35
if __name__ == '__main__':
sys.stdout.write("Testing positive wind & angle values\n\n")
for i in range(0, 10):
wind = random.randint(0, 180)
angle = random.randint(0, 180)
sys.stdout.write("Input (wind,angle): %i %i\n" % (wind, angle))
result = course_by_wind(wind, angle)
postcondition(result)
sys.stdout.write("Output (sail,rudder): %i %i %i\n\n" % result)
sys.stdout.write("Testing negative wind & angle values\n\n")
for i in range(0, 10):
wind = random.randint(-180, 0)
angle = random.randint(-180, 0)
sys.stdout.write("Input (wind,angle): %i %i\n" % (wind, angle))
result = course_by_wind(wind, angle)
postcondition(result)
sys.stdout.write("Output (sail,rudder): %i %i %i\n\n" % result)
sys.stdout.write("Testing negative wind & positive angle values\n\n")
for i in range(0, 10):
wind = random.randint(-180, 0)
angle = random.randint(0, 180)
sys.stdout.write("Input (wind,angle): %i %i\n" % (wind, angle))
result = course_by_wind(wind, angle)
postcondition(result)
sys.stdout.write("Output (sail,rudder): %i %i %i\n\n" % result)
sys.stdout.write("Testing positive wind & negative angle values\n\n")
for i in range(0, 10):
wind = random.randint(0, 180)
angle = random.randint(-180, 0)
sys.stdout.write("Input (wind,angle): %i %i\n" % (wind, angle))
result = course_by_wind(wind, angle)
postcondition(result)
sys.stdout.write("Output (sail,rudder): %i %i %i\n\n" % result)
sys.stdout.write("Testing positive wind == angle values\n\n")
for i in range(0, 10):
wind = random.randint(0, 180)
angle = wind
sys.stdout.write("Input (wind,angle): %i %i\n" % (wind, angle))
result = course_by_wind(wind, angle)
postcondition(result)
sys.stdout.write("Output (sail,rudder): %i %i %i\n\n" % result)
sys.stdout.write("Testing negative wind == angle values\n\n")
for i in range(0, 10):
wind = random.randint(-180, 0)
angle = wind
sys.stdout.write("Input (wind,angle): %i %i\n" % (wind, angle))
result = course_by_wind(wind, angle)
postcondition(result)
sys.stdout.write("Output (sail,rudder): %i %i %i\n\n" % result)