# Changeset 909

Ignore:
Timestamp:
02/04/20 19:36:58 (10 months ago)
Message:

Introduced XY::extensionContaining()

File:
1 edited

Unmodified
Removed
• ## cpp/common/2d.h

 r905 // This file is a part of Framsticks SDK.  http://www.framsticks.com/ // Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski. // Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski. // See LICENSE.txt for details. XY() {} XY(T _x, T _y) :x(_x), y(_y) {} template XY(const Q& other) : x(other.x), y(other.y) {} template const XY& operator=(const Q& other) { x = other.x; y = other.y; return *this; } template const XY operator()(const Q& other) { return XY(other.x, other.y); } XY operator+(const XY&p) const { return XY(x + p.x, y + p.y); } XY operator-(const XY&p) const { return XY(x - p.x, y - p.y); } XY operator+=(const XY&p) { x += p.x; y += p.y; return *this; } XY operator-=(const XY&p) { x -= p.x; y -= p.y; return *this; } template XY(const Q &other) : x(other.x), y(other.y) {} template const XY &operator=(const Q &other) { x = other.x; y = other.y; return *this; } template const XY operator()(const Q &other) { return XY(other.x, other.y); } XY operator+(const XY &p) const { return XY(x + p.x, y + p.y); } XY operator-(const XY &p) const { return XY(x - p.x, y - p.y); } XY operator+=(const XY &p) { x += p.x; y += p.y; return *this; } XY operator-=(const XY &p) { x -= p.x; y -= p.y; return *this; } XY operator-() const { return XY(-x, -y); } // allows float operations on ints template XY operator/=(Q q) { x /= q; y /= q; return *this; } template XY operator/(Q q) const { return XY(x / q, y / q); } template XY operator*(Q q) const { return XY(q*x, q*y); } XY operator*=(const XY& q) { x *= q.x; y *= q.y; return *this; } XY operator/=(const XY& q) { x /= q.x; y /= q.y; return *this; } XY operator*(const XY& q) const { return XY(x*q.x, y*q.y); } XY operator/(const XY& q) const { return XY(x/q.x, y/q.y); } template XY operator*(Q q) const { return XY(q * x, q * y); } XY operator*=(const XY &q) { x *= q.x; y *= q.y; return *this; } XY operator/=(const XY &q) { x /= q.x; y /= q.y; return *this; } XY operator*(const XY &q) const { return XY(x * q.x, y * q.y); } XY operator/(const XY &q) const { return XY(x / q.x, y / q.y); } void set(T _x, T _y) { x = _x; y = _y; } void add(T _x, T _y) { x += _x; y += _y; } void sub(T _x, T _y) { x -= _x; y -= _y; } bool operator==(const XY& p) const { return (fabs(double(x - p.x)) < 1e-20) && (fabs(double(y - p.y)) < 1e-20); } bool operator!=(const XY& p) const { return !operator==(p); } T distanceTo(const XY& p) const { return sqrt(double((p.x - x)*(p.x - x) + (p.y - y)*(p.y - y))); } T magnitude() const { return sqrt(x*x + y * y); } T length() const { return sqrt(x*x + y * y); } bool operator==(const XY &p) const { return (fabs(double(x - p.x)) < 1e-20) && (fabs(double(y - p.y)) < 1e-20); } bool operator!=(const XY &p) const { return !operator==(p); } T distanceTo(const XY &p) const { return sqrt(double((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y))); } T magnitude() const { return sqrt(x * x + y * y); } T length() const { return sqrt(x * x + y * y); } T lengthSq() const { return x * x + y * y; } T dotProduct(const XY& v) const { return x * v.x + y * v.y; } T crossProduct(const XY& v) const { return x * v.y - y * v.x; } T dotProduct(const XY &v) const { return x * v.x + y * v.y; } T crossProduct(const XY &v) const { return x * v.y - y * v.x; } void normalize() { operator/=(length()); } // length becomes 1 static XY average(const XY& v1, const XY& v2) { return XY((v1.x + v2.x)*0.5, (v1.y + v2.y)*0.5); } static XY average(const XY &v1, const XY &v2) { return XY((v1.x + v2.x) * 0.5, (v1.y + v2.y) * 0.5); } double getDirection() const { return atan2(y, x); } static XY interpolate(const XY& v1, const XY& v2, double t) { return universal_lerp(v1,v2,t); } static XY interpolate(const XY &v1, const XY &v2, double t) { return universal_lerp(v1, v2, t); } XY toInt() const { return XY(int(x), int(y)); } XY transpose() const { return XY(y, x); } static const XY& zero() { static XY t(0, 0); return t; } static const XY& one() { static XY t(1, 1); return t; } static const XY &zero() { static XY t(0, 0); return t; } static const XY &one() { static XY t(1, 1); return t; } }; //specialized: int equality not using fabs() template<> inline bool XY::operator==(const XY& p) const { return (x == p.x) && (y == p.y); } template XY xymin(const XY& a, const XY& b) { return XY(min(a.x, b.x), min(a.y, b.y)); } template XY xymax(const XY& a, const XY& b) { return XY(max(a.x, b.x), max(a.y, b.y)); } template<> inline bool XY::operator==(const XY &p) const { return (x == p.x) && (y == p.y); } template XY xymin(const XY &a, const XY &b) { return XY(min(a.x, b.x), min(a.y, b.y)); } template XY xymax(const XY &a, const XY &b) { return XY(max(a.x, b.x), max(a.y, b.y)); } template XY p, size; XYRect() {} XYRect(const XY& p1, const XY& s) :p(p1), size(s) {} template XYRect(const Q& other) : p(other.p), size(other.size) {} XYRect(const XY &p1, const XY &s) :p(p1), size(s) {} template XYRect(const Q &other) : p(other.p), size(other.size) {} XYRect(T _x, T _y, T _w, T _h) :p(_x, _y), size(_w, _h) {} static XYRect centeredAt(const XY& p, XY s) { return XYRect(p - s * 0.5, s); } static XYRect centeredAt(const XY &p, XY s) { return XYRect(p - s * 0.5, s); } bool isEmpty() const { return (size.x < 0) || (size.y < 0); } XYRect toInt() const { return XYRect(int(p.x), int(p.y), int(p.x + size.x) - int(p.x), int(p.y + size.y) - int(p.y)); } bool operator==(const XYRect& r) const { return (p == r.p) && (size == r.size); } template const XYRect& operator=(const Q& other) { p = other.p; size = other.size; return *this; } bool operator==(const XYRect &r) const { return (p == r.p) && (size == r.size); } template const XYRect &operator=(const Q &other) { p = other.p; size = other.size; return *this; } T right() const { return p.x + size.x; } T left() const { return p.x; } XY center() const { return p + size / 2; } const XY& topLeft() const { return p; } const XY &topLeft() const { return p; } XY bottomRight() const { return p + size; } XY topRight() const { return XY(p.x + size.x, p.y); } XY bottomLeft() const { return XY(p.x, p.y + size.y); } T area() const { return size.x*size.y; } bool intersects(const XYRect& r) const T area() const { return size.x * size.y; } bool intersects(const XYRect &r) const { if (r.p.x >= (p.x + size.x)) return false; } bool contains(const XY& n) const bool contains(const XY &n) const { if (n.x < p.x) return false; } bool contains(const XYRect& r) const bool contains(const XYRect &r) const { return contains(r.p) && contains(r.p + r.size); } void add(const XY& n) void add(const XY &n) { if (n.x < p.x) { size.x += p.x - n.x; p.x = n.x; } } XYRect extendBy(const XY& border_size) const XYRect extendBy(const XY &border_size) const { return XYRect(p - border_size, size + border_size * 2); } XYRect shrinkBy(const XY& border_size) const XYRect shrinkBy(const XY &border_size) const { return XYRect(p + border_size, size - border_size * 2); } XYRect extendBy(const XYMargin& m) const XYRect extendBy(const XYMargin &m) const { return XYRect(p.x - m.left, p.y - m.top, size.x + m.horizontal(), size.y + m.vertical()); } XYRect shrinkBy(const XYMargin& m) const XYRect shrinkBy(const XYMargin &m) const { return XYRect(p.x + m.left, p.y + m.top, size.x - m.horizontal(), size.y - m.vertical()); { XYRect r; r.size=size; if (size.x < size.y*aspect) r.size.y = r.size.x/aspect; r.size = size; if (size.x < size.y * aspect) r.size.y = r.size.x / aspect; else r.size.x = r.size.y*aspect; r.p=p+(size-r.size)*0.5; return r; } XYRect intersection(const XYRect& r) const r.size.x = r.size.y * aspect; r.p = p + (size - r.size) * 0.5; return r; } XYRect intersection(const XYRect &r) const { XYRect i; } XYRect extensionContaining(const XYRect& r) const XYRect extensionContaining(const XY &p) const { XY p1 = xymin(topLeft(), p); XY p2 = xymax(bottomRight(), p); return XYRect(p1, p2 - p1); } XYRect extensionContaining(const XYRect &r) const { XY p1 = xymin(topLeft(), r.topLeft()); } XYRect translation(const XY& t) const XYRect translation(const XY &t) const { return XYRect(p + t, size); } T distanceTo(const XY& n) const T distanceTo(const XY &n) const { XY tp = n; } T distanceTo(const XYRect& r) const T distanceTo(const XYRect &r) const { bool r_above = (r.bottom() <= top()); } static const XYRect& zero() { static XYRect t(0, 0, 0, 0); return t; } static const XYRect& one() { static XYRect t(0, 0, 1, 1); return t; } static const XYRect &zero() { static XYRect t(0, 0, 0, 0); return t; } static const XYRect &one() { static XYRect t(0, 0, 1, 1); return t; } };
Note: See TracChangeset for help on using the changeset viewer.