52 lines
874 B
C++
52 lines
874 B
C++
#ifndef POSITION_H
|
|
#define POSITION_H
|
|
|
|
#include "BASICS.h"
|
|
|
|
class Vector {
|
|
|
|
public:
|
|
|
|
double x;
|
|
|
|
double y;
|
|
|
|
Vector(double x, double y) :
|
|
x(x), y(y) {
|
|
// nothing
|
|
}
|
|
|
|
Vector(long degrees, double length) {
|
|
double radians = (double) degrees * DEG_TO_RAD;
|
|
x = cos(radians) * length;
|
|
y = sin(radians) * length;
|
|
}
|
|
|
|
Vector *add(Vector that) {
|
|
this->x += that.x;
|
|
this->y += that.y;
|
|
return this;
|
|
}
|
|
|
|
Vector *bounceInBox(uint8_t width, uint8_t height) {
|
|
while (x < 0 || x >= width) {
|
|
if (x < 0) {
|
|
x = -x;
|
|
} else if (x >= width) {
|
|
x = 2 * width - x;
|
|
}
|
|
}
|
|
while (y < 0 || y >= height) {
|
|
if (y < 0) {
|
|
y = -y;
|
|
} else if (y >= height) {
|
|
y = 2 * height - y;
|
|
}
|
|
}
|
|
return this;
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|