NXPCup_Cachan / Mbed 2 deprecated NXPCup

Dependencies:   mbed

Committer:
EISR
Date:
Thu Mar 12 15:22:02 2020 +0000
Revision:
3:07109a6bb472
Parent:
2:1103f5d61035
commentaires de la partie bluetooth;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wael_H 0:8743b606abc3 1 #include "Vector.h"
Wael_H 0:8743b606abc3 2 #include "math.h"
Wael_H 0:8743b606abc3 3
Wael_H 2:1103f5d61035 4 Vector Vector::v1_ideal = Vector(0, 41, 17, 0);
Wael_H 2:1103f5d61035 5 Vector Vector::v2_ideal = Vector(0, 40, 62, 0);
Wael_H 2:1103f5d61035 6
Wael_H 0:8743b606abc3 7 Vector::Vector(int x0, int y0, int x1, int y1)
Wael_H 0:8743b606abc3 8 {
Wael_H 0:8743b606abc3 9 this->p0.x = x0;
Wael_H 0:8743b606abc3 10 this->p0.y = y0;
Wael_H 0:8743b606abc3 11 this->p1.x = x1;
Wael_H 0:8743b606abc3 12 this->p1.y = y1;
Wael_H 0:8743b606abc3 13 }
Wael_H 0:8743b606abc3 14
Wael_H 0:8743b606abc3 15 Vector::Vector(Point p0, Point p1)
Wael_H 0:8743b606abc3 16 {
Wael_H 0:8743b606abc3 17 this->p0 = p0;
Wael_H 0:8743b606abc3 18 this->p1 = p1;
Wael_H 0:8743b606abc3 19 }
Wael_H 0:8743b606abc3 20
Wael_H 0:8743b606abc3 21 Vector::Vector()
Wael_H 0:8743b606abc3 22 {
Wael_H 2:1103f5d61035 23 this->p0.x = 0;
Wael_H 2:1103f5d61035 24 this->p0.y = 0;
Wael_H 2:1103f5d61035 25 this->p1.x = 0;
Wael_H 2:1103f5d61035 26 this->p1.y = 0;
Wael_H 0:8743b606abc3 27 }
Wael_H 0:8743b606abc3 28
Wael_H 0:8743b606abc3 29 void Vector::orienteVersLeHaut()
Wael_H 0:8743b606abc3 30 {
Wael_H 0:8743b606abc3 31 if(this->p1.y > this->p0.y)
Wael_H 0:8743b606abc3 32 {
Wael_H 0:8743b606abc3 33 Point savep0 = this->p0;
Wael_H 0:8743b606abc3 34 this->p0 = this->p1;
Wael_H 0:8743b606abc3 35 this->p1 = savep0;
Wael_H 0:8743b606abc3 36 }
Wael_H 0:8743b606abc3 37 }
Wael_H 0:8743b606abc3 38
Wael_H 0:8743b606abc3 39 float Vector::getCoeffDir() const
Wael_H 0:8743b606abc3 40 {
Wael_H 0:8743b606abc3 41 return (float)(this->p0.y - this->p1.y) / (float)(this->p1.x - this->p0.x); // y inversés pcq console tsais
Wael_H 0:8743b606abc3 42 }
Wael_H 0:8743b606abc3 43
Wael_H 0:8743b606abc3 44 int Vector::getNorme() const
Wael_H 0:8743b606abc3 45 {
Wael_H 0:8743b606abc3 46 return sqrt(pow((float)(this->p1.x - this->p0.x), 2) + pow((float)(this->p1.y - this->p0.y), 2));
Wael_H 0:8743b606abc3 47 }
Wael_H 0:8743b606abc3 48
Wael_H 0:8743b606abc3 49 bool Vector::estADroiteDe(Vector& v) const
Wael_H 0:8743b606abc3 50 {
Wael_H 0:8743b606abc3 51 if(this->p1.x > v.p1.x)
Wael_H 0:8743b606abc3 52 return true;
Wael_H 0:8743b606abc3 53 return false;
Wael_H 0:8743b606abc3 54 }
Wael_H 0:8743b606abc3 55
Wael_H 2:1103f5d61035 56 Vector Vector::testJoinedTo(Vector& v) const
Wael_H 2:1103f5d61035 57 {
Wael_H 2:1103f5d61035 58 if(this->p0.x == v.p0.x && this->p0.y == v.p0.y)
Wael_H 2:1103f5d61035 59 return Vector(this->p1.x, this->p1.y, v.p1.x, v.p1.y);
Wael_H 2:1103f5d61035 60 if(this->p1.x == v.p1.x && this->p1.y == v.p1.y)
Wael_H 2:1103f5d61035 61 return Vector(this->p0.x, this->p0.y, v.p0.x, v.p0.y);
Wael_H 2:1103f5d61035 62 if(this->p1.x == v.p0.x && this->p1.y == v.p0.y)
Wael_H 2:1103f5d61035 63 return Vector(this->p0.x, this->p0.y, v.p1.x, v.p1.y);
Wael_H 2:1103f5d61035 64 if(this->p0.x == v.p1.x && this->p0.y == v.p1.y)
Wael_H 2:1103f5d61035 65 return Vector(v.p0.x, v.p0.y, this->p1.x, this->p1.y);
Wael_H 2:1103f5d61035 66
Wael_H 2:1103f5d61035 67 return Vector(0, 0, 0, 0);
Wael_H 2:1103f5d61035 68 }
Wael_H 2:1103f5d61035 69
Wael_H 2:1103f5d61035 70 int Vector::calcDifference(Vector& v1, Vector& v2) const
Wael_H 2:1103f5d61035 71 {
Wael_H 2:1103f5d61035 72 int diff1, diff2;
Wael_H 2:1103f5d61035 73
Wael_H 2:1103f5d61035 74 diff1 = abs(v1.p0.x - this->p0.x) + abs(v1.p0.y - this->p0.y) + abs(v1.p1.x - this->p1.x) + abs(v1.p1.y - this->p1.y);
Wael_H 2:1103f5d61035 75 diff2 = abs(v2.p0.x - this->p0.x) + abs(v2.p0.y - this->p0.y) + abs(v2.p1.x - this->p1.x) + abs(v2.p1.y - this->p1.y);
Wael_H 2:1103f5d61035 76
Wael_H 2:1103f5d61035 77 if(diff1 < diff2)
Wael_H 2:1103f5d61035 78 return 1000 + diff1;
Wael_H 2:1103f5d61035 79
Wael_H 2:1103f5d61035 80 return 2000 + diff2;
Wael_H 2:1103f5d61035 81 }
Wael_H 2:1103f5d61035 82
Wael_H 2:1103f5d61035 83 Point Vector::projection() const
Wael_H 2:1103f5d61035 84 {
Wael_H 2:1103f5d61035 85 float coeffDir = this->getCoeffDir();
Wael_H 2:1103f5d61035 86
Wael_H 2:1103f5d61035 87 if(coeffDir > 0)
Wael_H 2:1103f5d61035 88 {
Wael_H 2:1103f5d61035 89 int y = this->p1.y - (78 - this->p1.x) * coeffDir; // 78 = valx max
Wael_H 2:1103f5d61035 90
Wael_H 2:1103f5d61035 91 if(y >= 0)
Wael_H 2:1103f5d61035 92 return Point(78, y);
Wael_H 2:1103f5d61035 93
Wael_H 2:1103f5d61035 94 int x = this->p1.x + this->p1.y / coeffDir;
Wael_H 2:1103f5d61035 95
Wael_H 2:1103f5d61035 96 return Point(x, 0);
Wael_H 2:1103f5d61035 97 }
Wael_H 2:1103f5d61035 98 else
Wael_H 2:1103f5d61035 99 {
Wael_H 2:1103f5d61035 100 int y = this->p1.y + this->p1.x * coeffDir; // 78 = valx max
Wael_H 2:1103f5d61035 101
Wael_H 2:1103f5d61035 102 if(y >= 0)
Wael_H 2:1103f5d61035 103 return Point(0, y);
Wael_H 2:1103f5d61035 104
Wael_H 2:1103f5d61035 105 int x = this->p1.x + this->p1.y / coeffDir;
Wael_H 2:1103f5d61035 106
Wael_H 2:1103f5d61035 107 return Point(x, 0);
Wael_H 2:1103f5d61035 108 }
Wael_H 2:1103f5d61035 109 }
Wael_H 2:1103f5d61035 110
Wael_H 0:8743b606abc3 111 Vector Vector::getVectorAuMilieuDe(Vector& v0, Vector& v1)
Wael_H 0:8743b606abc3 112 {
Wael_H 0:8743b606abc3 113 static Vector vM;
Wael_H 0:8743b606abc3 114
Wael_H 0:8743b606abc3 115 vM.p0.x = (v0.p0.x + v1.p0.x) / 2;
Wael_H 0:8743b606abc3 116 vM.p0.y = (v0.p0.y + v1.p0.y) / 2;
Wael_H 0:8743b606abc3 117 vM.p1.x = (v0.p1.x + v1.p1.x) / 2;
Wael_H 0:8743b606abc3 118 vM.p1.y = (v0.p1.y + v1.p1.y) / 2;
Wael_H 0:8743b606abc3 119
Wael_H 2:1103f5d61035 120 vM.orienteVersLeHaut();
Wael_H 2:1103f5d61035 121
Wael_H 0:8743b606abc3 122 return vM;
Wael_H 2:1103f5d61035 123 }
Wael_H 2:1103f5d61035 124
Wael_H 2:1103f5d61035 125 bool Vector::operator==(const Vector& v)
Wael_H 2:1103f5d61035 126 {
Wael_H 2:1103f5d61035 127 return (this->p0.x == v.p0.x && this->p0.y == v.p0.y && this->p1.x == v.p1.x && this->p1.y == v.p1.y);
Wael_H 2:1103f5d61035 128 }
Wael_H 2:1103f5d61035 129
Wael_H 2:1103f5d61035 130 bool Vector::operator!=(const Vector& v)
Wael_H 2:1103f5d61035 131 {
Wael_H 2:1103f5d61035 132 return !(*this == v);
Wael_H 2:1103f5d61035 133 }
Wael_H 2:1103f5d61035 134
Wael_H 2:1103f5d61035 135 void Vector::affVector(Serial& serial) const
Wael_H 2:1103f5d61035 136 {
Wael_H 2:1103f5d61035 137 serial.printf("x0 : %d, y0 : %d, x1 : %d, y1 : %d\n\r", this->p0.x, this->p0.y, this->p1.x, this->p1.y);
Wael_H 0:8743b606abc3 138 }