Update board.hpp, forces.hpp, forces.cpp, main.cpp, pedestrian.hpp: correct some bugs in implementation
This commit is contained in:
@@ -1,9 +1,41 @@
|
||||
#pragma once
|
||||
#include "vector2d.hpp"
|
||||
|
||||
struct Obstacle {
|
||||
Vector2d start;
|
||||
Vector2d end;
|
||||
|
||||
Obstacle() : start(0, 0), end(0, 0) {}
|
||||
Obstacle(Vector2d s, Vector2d e) : start(s), end(e) {}
|
||||
// find the point of the obstacle that is nearest to p
|
||||
Vector2d find_nearest_obstacle_point(Vector2d p) {
|
||||
Vector2d obstacle_v = end - start;
|
||||
double projection = (p - start).dot(obstacle_v) / obstacle_v.dot(obstacle_v);
|
||||
if (projection < 0.0)
|
||||
projection = 0.0;
|
||||
if (projection > 1.0)
|
||||
projection = 1.0;
|
||||
return start + obstacle_v * projection;
|
||||
}
|
||||
};
|
||||
|
||||
struct Board {
|
||||
double length, width;
|
||||
Vector2d *borders; // array containing the position of each obstacle
|
||||
int n_borders; // number of obstacles
|
||||
Board(double l, double w, int n, Vector2d *border) : length(l), width(w), n_borders(n), borders(border) {}
|
||||
Obstacle *obstacles; // array of obstacles (represented as lines) that are present on the board
|
||||
int n_obstacles; // number of obstacles
|
||||
|
||||
Board(double l, double w, int n, Obstacle *o) : length(l), width(w), n_obstacles(n + 4) {
|
||||
obstacles = new Obstacle[n + 4];
|
||||
for (int i = 0; i < n; i++) {
|
||||
obstacles[i] = o[i];
|
||||
}
|
||||
|
||||
// borders of the board: bottom, right, top, left
|
||||
obstacles[n] = Obstacle(Vector2d(0, 0), Vector2d(l, 0));
|
||||
obstacles[n + 1] = Obstacle(Vector2d(l, 0), Vector2d(l, w));
|
||||
obstacles[n + 2] = Obstacle(Vector2d(l, w), Vector2d(0, w));
|
||||
obstacles[n + 3] = Obstacle(Vector2d(0, w), Vector2d(0, 0));
|
||||
}
|
||||
|
||||
~Board() { delete[] obstacles; }
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user