reduced flop count

This commit is contained in:
Nguyen Quy Anh
2026-05-03 05:57:34 +02:00
parent 89dfdf7785
commit e9533ab925
10 changed files with 236 additions and 166 deletions
+6 -8
View File
@@ -8,14 +8,14 @@ struct Obstacle {
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
// # 15 flops
// Find the point of the obstacle that is closest to p
// # 8 ADDs, 6 MULTs, 1 DIV
Vector2d find_nearest_obstacle_point(Vector2d p) {
// # 2 ADDs
Vector2d obstacle_v = end - start;
// # 9 flops = 2 ADDs, 2 MULTs, 1 ADD, 1 DIV, 2 MULTs, 1 ADD
// # 4 ADDs, 4 MULTs, 1 DIV
double projection = (p - start).dot(obstacle_v) / obstacle_v.dot(obstacle_v);
if (projection < 0.0)
@@ -23,17 +23,15 @@ struct Obstacle {
else if (projection > 1.0)
projection = 1.0;
// # 4 flops = 2 ADDs, 2 MULTs
// # 2 ADDs, 2 MULTs
return start + obstacle_v * projection;
}
};
struct Board {
double length, width;
Obstacle *obstacles; // array of obstacles (represented as lines) that are present on the board
int n_obstacles; // number of obstacles
Obstacle *obstacles; // Array of obstacles, represented as lines 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];