2-dimensional vector of floats, representing a point on the plane for graphics. */
| 187 | /** 2-dimensional vector of floats, representing a point on the plane for graphics. |
| 188 | */ |
| 189 | struct Vec { |
| 190 | float x = 0.f; |
| 191 | float y = 0.f; |
| 192 | |
| 193 | Vec() {} |
| 194 | Vec(float xy) : x(xy), y(xy) {} |
| 195 | Vec(float x, float y) : x(x), y(y) {} |
| 196 | |
| 197 | float& operator[](int i) { |
| 198 | return (i == 0) ? x : y; |
| 199 | } |
| 200 | const float& operator[](int i) const { |
| 201 | return (i == 0) ? x : y; |
| 202 | } |
| 203 | /** Negates the vector. |
| 204 | Equivalent to a reflection across the `y = -x` line. |
| 205 | */ |
| 206 | Vec neg() const { |
| 207 | return Vec(-x, -y); |
| 208 | } |
| 209 | Vec plus(Vec b) const { |
| 210 | return Vec(x + b.x, y + b.y); |
| 211 | } |
| 212 | Vec minus(Vec b) const { |
| 213 | return Vec(x - b.x, y - b.y); |
| 214 | } |
| 215 | Vec mult(float s) const { |
| 216 | return Vec(x * s, y * s); |
| 217 | } |
| 218 | Vec mult(Vec b) const { |
| 219 | return Vec(x * b.x, y * b.y); |
| 220 | } |
| 221 | Vec div(float s) const { |
| 222 | return Vec(x / s, y / s); |
| 223 | } |
| 224 | Vec div(Vec b) const { |
| 225 | return Vec(x / b.x, y / b.y); |
| 226 | } |
| 227 | float dot(Vec b) const { |
| 228 | return x * b.x + y * b.y; |
| 229 | } |
| 230 | float arg() const { |
| 231 | return std::atan2(y, x); |
| 232 | } |
| 233 | float norm() const { |
| 234 | return std::hypot(x, y); |
| 235 | } |
| 236 | Vec normalize() const { |
| 237 | return div(norm()); |
| 238 | } |
| 239 | float square() const { |
| 240 | return x * x + y * y; |
| 241 | } |
| 242 | float area() const { |
| 243 | return x * y; |
| 244 | } |
| 245 | /** Rotates counterclockwise in radians. */ |
| 246 | Vec rotate(float angle) { |
no outgoing calls
no test coverage detected