utils.cpp (567B)
1 #include "utils.hpp" 2 #include <cstdlib> 3 4 float square(float x) { 5 return x * x; 6 } 7 8 // call srand before invocation as this is a pure function. 9 Vector2 randomPosition(float xMax, float yMax) { 10 float r1 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX) * xMax; 11 float r2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX) * yMax; 12 Vector2 v {r1,r2}; 13 return v; 14 } 15 16 float distanceSquared(Vector2 v1, Vector2 v2) { 17 float xSquare = square(v1.x - v2.x); 18 float ySquare = square(v1.y - v2.y); 19 return xSquare + ySquare; 20 }