Henry Triff / Mbed 2 deprecated ELEC2645_Project_el18ht

Dependencies:   mbed

Revision:
5:2d9f3c36bcb9
Child:
6:5f76dd718dc3
diff -r 9f41fc1c2ad1 -r 2d9f3c36bcb9 Mechanics/Mechanics.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Mechanics/Mechanics.cpp	Tue Feb 11 17:28:56 2020 +0000
@@ -0,0 +1,146 @@
+#include "Mechanics.h"
+
+bool Mechanics::Is_Offtrack(Point_2D position, Square_2D offtrack_square[], Triangle_2D offtrack_triangle[], Map_Data map_info)
+{
+    return (Is_Offtrack_Square(offtrack_square, map_info.number_of_off_track_triangles, position) || Is_Offtrack_Triangle(offtrack_triangle, map_info.number_of_off_track_triangles, position));
+}
+
+bool Mechanics::Is_Out_Of_Bounds(Point_2D position, Square_2D offtrack_square[], Triangle_2D offtrack_triangle[], Map_Data map_info)
+{
+    return (Is_Out_Of_Bounds_Square(offtrack_square, map_info.number_of_out_of_bounds_squares, position) || Is_Out_Of_Bounds_Triangle(offtrack_triangle, map_info.number_of_out_of_bounds_triangles, position));
+}
+
+//*************
+//  OFF TRACK
+//*************
+
+bool Mechanics::Is_Offtrack_Square(Square_2D offtrack[], int size, Point_2D position)
+{
+    for(int i = 0; i < size; i++) {
+        if(position.x > offtrack[i].TL.x && position.x < offtrack[i].BR.x && position.y < offtrack[i].TL.y && position.y > offtrack[i].BR.y ) {
+            return true;
+        }
+    }
+    return false;
+}
+
+bool Mechanics::Is_Offtrack_Triangle(Triangle_2D offtrack[], int size, Point_2D position)
+{
+    for(int i = 0; i < size; i++) {
+        if(position.x > offtrack[i].TL.x && position.x < offtrack[i].BR.x && position.y < offtrack[i].TL.y && position.y > offtrack[i].BR.y ) {
+            if(offtrack[i].Type == 1) {
+                Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].TL.y};
+                float position_limit = (offtrack[i].BR.y - offtrack[i].TL.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x;
+                if(Translated_position.y < position_limit) {
+                    return true;
+                }
+            } else if(offtrack[i].Type == 2) {
+                Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].BR.y};
+                float position_limit =  (offtrack[i].TL.y - offtrack[i].BR.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x;
+                if(Translated_position.y > position_limit) {
+                    return true;
+                }
+            } else if(offtrack[i].Type == 3) {
+                Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].TL.y};
+                float position_limit = (offtrack[i].BR.y - offtrack[i].TL.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x;
+                if(Translated_position.y > position_limit) {
+                    return true;
+                }
+            } else if(offtrack[i].Type == 4) {
+                Point_2D Translated_position = {position.x - offtrack[i].TL.x, position.y - offtrack[i].BR.y};
+                float position_limit =  (offtrack[i].TL.y - offtrack[i].BR.y) / (offtrack[i].BR.x - offtrack[i].TL.x) * Translated_position.x;
+                if(Translated_position.y < position_limit) {
+                    return true;
+                }
+            }
+        }
+    }
+    return false;
+}
+
+//*****************
+//  OUT OF BOUNDS
+//*****************
+
+bool Mechanics::Is_Out_Of_Bounds_Square(Square_2D out_of_bounds[], int size, Point_2D position)
+{
+    for(int i = 0; i < size; i++) {
+        if(position.x > out_of_bounds[i].TL.x && position.x < out_of_bounds[i].BR.x && position.y < out_of_bounds[i].TL.y && position.y > out_of_bounds[i].BR.y ) {
+            return true;
+        }
+    }
+    return false;
+}
+
+bool Mechanics::Is_Out_Of_Bounds_Triangle(Triangle_2D out_of_bounds[], int size, Point_2D position)
+{
+    for(int i = 0; i < size; i++) {
+        if(position.x > out_of_bounds[i].TL.x && position.x < out_of_bounds[i].BR.x && position.y < out_of_bounds[i].TL.y && position.y > out_of_bounds[i].BR.y ) {
+            if(out_of_bounds[i].Type == 1) {
+                Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].TL.y};
+                float position_limit = (out_of_bounds[i].BR.y - out_of_bounds[i].TL.y) / (out_of_bounds[i].BR.x - out_of_bounds[i].TL.x) * Translated_position.x;
+                if(Translated_position.y < position_limit) {
+                    return true;
+                }
+            } else if(out_of_bounds[i].Type == 2) {
+                Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].BR.y};
+                float position_limit =  (out_of_bounds[i].TL.y - out_of_bounds[i].BR.y) / (out_of_bounds[i].BR.x - out_of_bounds[i].TL.x) * Translated_position.x;
+                if(Translated_position.y > position_limit) {
+                    return true;
+                }
+            } else if(out_of_bounds[i].Type == 3) {
+                Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].TL.y};
+                float position_limit = (out_of_bounds[i].BR.y - out_of_bounds[i].TL.y) / (out_of_bounds[i].BR.x - out_of_bounds[i].TL.x) * Translated_position.x;
+                if(Translated_position.y > position_limit) {
+                    return true;
+                }
+            } else if(out_of_bounds[i].Type == 4) {
+                Point_2D Translated_position = {position.x - out_of_bounds[i].TL.x, position.y - out_of_bounds[i].BR.y};
+                float position_limit =  (out_of_bounds[i].TL.y - out_of_bounds[i].BR.y) / (out_of_bounds[i].BR.x - out_of_bounds[i].TL.x) * Translated_position.x;
+                if(Translated_position.y < position_limit) {
+                    return true;
+                }
+            }
+        }
+    }
+    return false;
+}
+
+//*********
+//  GATES
+//*********
+
+int Mechanics::Get_Gate(Square_2D gates[], int number_of_gates, Point_2D position, int current_gate)
+{
+    int next_gate;
+
+    if(current_gate + 1 <= (number_of_gates - 1)) {
+        next_gate = current_gate + 1;
+    } else {
+        next_gate = 0;
+    }
+    printf("%i  ",next_gate);
+
+    if(position.x >= gates[next_gate].TL.x && position.x <= gates[next_gate].BR.x && position.y <= gates[next_gate].TL.y && position.y >= gates[next_gate].BR.y) {
+        return next_gate;
+    }
+
+    return current_gate;
+}
+
+int Mechanics::Get_Laps(int laps, Square_2D gates[], int number_of_gates, Point_2D position, int current_gate)
+{
+    int next_gate;
+
+    if(current_gate + 1 <= (number_of_gates - 1)) {
+        next_gate = current_gate + 1;
+    } else {
+        next_gate = 0;
+    }
+    if(position.x >= gates[next_gate].TL.x && position.x <= gates[next_gate].BR.x && position.y <= gates[next_gate].TL.y && position.y >= gates[next_gate].BR.y) {
+        if(next_gate == 0) {
+            laps++;
+        }
+    }
+    return laps;
+}
\ No newline at end of file