-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSailbotChallenge.cpp
More file actions
93 lines (77 loc) · 2.72 KB
/
Copy pathSailbotChallenge.cpp
File metadata and controls
93 lines (77 loc) · 2.72 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
87
88
89
90
91
92
93
#include "pch.h"
/**
* This C++ file is my submition for the Sailbot Coding Challenge. It contains two methods, BoundTo180() which forces any angle to lie between -180 to 180,
* IsAngleBetween() which determines if an angle lies between the acute angle between two others as well as unit tests using Google Test.
*
* Thanks for considering my application!
*
* @author Joshua Sam, 3rd year ELEC student.
* @date September 13, 2020
*
*/
//Function Declarations
float BoundTo180(float angle);
bool IsAngleBetween(float first_angle, float middle_angle, float second_angle);
/**
* UNIT TESTS for BoundTo180() , IsAngleBetween()
*/
TEST(Sailbot, BoundTo180Test)
{
EXPECT_EQ(BoundTo180(360), 0);
EXPECT_EQ(BoundTo180(270), -90);
EXPECT_EQ(BoundTo180(-450), -90);
}
TEST(Sailbot, IsAngleBetweenTest)
{
ASSERT_TRUE(IsAngleBetween(-90,-180,110));
ASSERT_FALSE(IsAngleBetween(-90, -180, 80));
}
/**
* Bounds the provided angle between [-180, 180) degrees.
* Ex. 360 becomes 0, 270 becomes -90, -450 becomes -90.
* @param angle Input angle in degrees.
* @return The bounded angle in degrees.
*/
float BoundTo180(float angle)
{
float bounded_angle = angle;
while (bounded_angle > 180) bounded_angle -= 360;
while (bounded_angle < -180) bounded_angle += 360;
return bounded_angle;
}
/**
* Determines whether |middle_angle| is in the acute angle between the other two
bounding angles.
* Note: Input angles are bounded to 180 for safety.
* Ex. -180 is between -90 and 110 but not between -90 and 80.
* @param first_angle First angle in degrees.
* @param middle_angle Middle angle in degrees.
* @param second_angle Second angle in degrees.
* @return Whether |middle_angle| is between |first_angle| and |second_angle|
(exclusive).
*/
bool IsAngleBetween(float first_angle, float middle_angle, float second_angle)
{
float larger_angle;
float smaller_angle;
//Convert all values to 0 degrees -> 360 degrees to make it less error prone
if (first_angle < 0) first_angle += 360;
if (middle_angle < 0) middle_angle += 360;
if (second_angle < 0) second_angle += 360;
//determine the larger and smaller angle
if (first_angle > second_angle)
{
larger_angle = first_angle;
smaller_angle = second_angle;
}
else
{
larger_angle = second_angle;
smaller_angle = first_angle;
}
//Check for the two conditions where the middle angle lies between the other two bounding angles and return true
if ((larger_angle - smaller_angle) < 180 && smaller_angle <= middle_angle <= larger_angle) return true;
if ((larger_angle - smaller_angle) > 180 && smaller_angle >= middle_angle >= larger_angle) return true;
//otherwise return false
return false;
}