CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Math/geom2d.h
Views: 1401
#pragma once12#include <cmath>34struct Point2D {5Point2D() : x(0.0f), y(0.0f) {}6Point2D(float x_, float y_) : x(x_), y(y_) {}78float x;9float y;1011float distanceTo(const Point2D &other) const {12float dx = other.x - x, dy = other.y - y;13return sqrtf(dx*dx + dy*dy);14}1516bool operator ==(const Point2D &other) const {17return x == other.x && y == other.y;18}1920/*21FocusDirection directionTo(const Point &other) const {22int angle = atan2f(other.y - y, other.x - x) / (2 * M_PI) - 0.125;2324}*/25};262728// Resolved bounds on screen after layout.29struct Bounds {30Bounds() : x(0.0f), y(0.0f), w(0.0f), h(0.0f) {}31Bounds(float x_, float y_, float w_, float h_) : x(x_), y(y_), w(w_), h(h_) {}3233bool Contains(float px, float py) const {34return (px >= x && py >= y && px < x + w && py < y + h);35}3637bool Intersects(const Bounds &other) const {38return !(x > other.x2() || x2() < other.x || y > other.y2() || y2() < other.y);39}4041void Clip(const Bounds &clipTo) {42if (x < clipTo.x) {43w -= clipTo.x - x;44x = clipTo.x;45}46if (y < clipTo.y) {47h -= clipTo.y - y;48y = clipTo.y;49}50if (x2() > clipTo.x2()) {51w = clipTo.x2() - x;52}53if (y2() > clipTo.y2()) {54h = clipTo.y2() - y;55}56}5758float x2() const { return x + w; }59float y2() const { return y + h; }60float centerX() const { return x + w * 0.5f; }61float centerY() const { return y + h * 0.5f; }62Point2D Center() const {63return Point2D(centerX(), centerY());64}65Bounds Expand(float amount) const {66return Bounds(x - amount, y - amount, w + amount * 2, h + amount * 2);67}68Bounds Expand(float xAmount, float yAmount) const {69return Bounds(x - xAmount, y - yAmount, w + xAmount * 2, h + yAmount * 2);70}71Bounds Expand(float left, float top, float right, float bottom) const {72return Bounds(x - left, y - top, w + left + right, h + top + bottom);73}74Bounds Offset(float xAmount, float yAmount) const {75return Bounds(x + xAmount, y + yAmount, w, h);76}77Bounds Inset(float left, float top, float right, float bottom) {78return Bounds(x + left, y + top, w - left - right, h - bottom - top);79}8081Bounds Inset(float xAmount, float yAmount) const {82return Bounds(x + xAmount, y + yAmount, w - xAmount * 2, h - yAmount * 2);83}84Bounds Inset(float left, float top, float right, float bottom) const {85return Bounds(x + left, y + top, w - left - right, h - top - bottom);86}8788float x;89float y;90float w;91float h;92};9394959697