#include "geometry.h" #include static const float PI = 3.1415f; RectF::RectF(float x, float y, float width, float height) : m_x(x) , m_y(y) , m_width(width) , m_height(height) {} RectF::RectF(const RectF &other) : m_x(other.m_x) , m_y(other.m_y) , m_width(other.m_width) , m_height(other.m_height) {} RectF::RectF(const vec2 &p1, const vec2 &p2) : m_x(fmin(p1.x, p2.x)) , m_y(fmin(p1.y, p2.y)) , m_width(fabs(p1.x - p2.x)) , m_height(fabs(p1.y - p2.y)) {} bool RectF::contains(float x, float y) const { return (x > m_x && x < m_x + m_width) && (y > m_y && y < m_y + m_height); } bool RectF::contains(const vec2 &p) const { return contains(p.x, p.y); } bool RectF::intersects(const RectF &other) const { // TODO return false; } Geometry::Geometry() {} int Geometry::vertexCount() const { return 0; } vec2 *Geometry::vertexDataAsPoint2D() { return nullptr; } int calculateCircleVertexCount(float diameter) { // 10 * sqrt(r) \approx 2*pi / acos(1 - 1 / (4*r)) return int(10.0 * sqrt(diameter / 2)); } void updateCircleGeometry(Geometry *geometry, float diameter, float cx, float cy) { int vertexCount = geometry->vertexCount(); float theta = 2 * PI / float(vertexCount); float c = cosf(theta); float s = sinf(theta); float x = diameter / 2; float y = 0; vec2 *vertexData = geometry->vertexDataAsPoint2D(); for (int i = 0; i < vertexCount; i++) { vertexData[i].set(x + cx, y + cy); float t = x; x = c*x - s*y; y = s*t + c*y; } } void updateRectGeometry(Geometry *geometry, float x, float y, float w, float h) { vec2 *vertexData = geometry->vertexDataAsPoint2D(); vertexData[0].set(x, y); vertexData[1].set(x + w, y); vertexData[2].set(x + w, y + h); vertexData[3].set(x, y + h); } void updateCrossHairGeometry(Geometry *geometry, float x, float y, float thickness, float length) { vec2 *vertexData = geometry->vertexDataAsPoint2D(); if (geometry->vertexCount() != 12) { return; } vertexData[ 0].set(x + thickness, y - thickness); vertexData[ 1].set(x + length, y - thickness); vertexData[ 2].set(x + length, y + thickness); vertexData[ 3].set(x + thickness, y + thickness); vertexData[ 4].set(x + thickness, y + length); vertexData[ 5].set(x - thickness, y + length); vertexData[ 6].set(x - thickness, y + thickness); vertexData[ 7].set(x - length, y + thickness); vertexData[ 8].set(x - length, y - thickness); vertexData[ 9].set(x - thickness, y - thickness); vertexData[10].set(x - thickness, y - length); vertexData[11].set(x + thickness, y - length); }