-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturn_direction.cpp
65 lines (52 loc) · 1.13 KB
/
turn_direction.cpp
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
// Author: Jakub Pawlina
// Algorithm: Turn Direction using Cross Product
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
struct Point
{
double x;
double y;
};
struct Vector
{
double x;
double y;
};
long double cross_product(Vector vector1, Vector vector2)
{
return vector1.x * vector2.y - vector1.y * vector2.x;
}
void turn_direction(std::vector <Point> coordinates)
{
for (size_t i = 2; i < coordinates.size(); ++i)
{
Vector vector1, vector2;
vector1.x = coordinates[i - 1].x - coordinates[i - 2].x;
vector1.y = coordinates[i - 1].y - coordinates[i - 2].y;
vector2.x = coordinates[i].x - coordinates[i - 1].x;
vector2.y = coordinates[i].y - coordinates[i - 1].y;
if (cross_product(vector1, vector2) < 0)
{
std::cout << "RIGHT\n";
}
else
{
std::cout << "LEFT\n";
}
}
}
int32_t main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::vector <Point> coordinates;
Point point;
while (std::cin >> point.x)
{
std::cin >> point.y;
coordinates.push_back(point);
}
turn_direction(coordinates);
return 0;
}