vec2 & rect code cleanup

This commit is contained in:
Noel Berry
2021-02-21 16:30:21 -08:00
parent 118af7d3b4
commit f8741e27e2
7 changed files with 337 additions and 69 deletions

View File

@ -507,7 +507,7 @@ void Batch::bezier_line(const Vec2& from, const Vec2& b, const Vec2& to, int ste
for (int i = 1; i < steps; i++)
{
Vec2 at = Vec2::bezier_lerp(from, b, to, add * i);
Vec2 at = Vec2::lerp_bezier(from, b, to, add * i);
line(prev, at, t, color);
prev = at;
}
@ -522,7 +522,7 @@ void Batch::bezier_line(const Vec2& from, const Vec2& b, const Vec2& c, const Ve
for (int i = 1; i < steps; i++)
{
Vec2 at = Vec2::bezier_lerp(from, b, c, to, add * i);
Vec2 at = Vec2::lerp_bezier(from, b, c, to, add * i);
line(prev, at, t, color);
prev = at;
}
@ -668,8 +668,8 @@ void Batch::rect_rounded(const Rect& rect, float rtl, int rtl_steps, float rtr,
{
// get corners
Rect tl = Rect(rect.top_left(), Vec2(rtl, rtl));
Rect tr = Rect(rect.top_right() + Vec2(-rtr, 0), Vec2(rtr, rtr));
Rect bl = Rect(rect.bottom_left() + Vec2(0, -rbl), Vec2(rbl, rbl));
Rect tr = Rect(rect.top_right() + Vec2(-rtr, 0.0f), Vec2(rtr, rtr));
Rect bl = Rect(rect.bottom_left() + Vec2(0.0f, -rbl), Vec2(rbl, rbl));
Rect br = Rect(rect.bottom_right() + Vec2(-rbr, -rbr), Vec2(rbr, rbr));
// rounded corners

View File

@ -26,11 +26,68 @@ RectI::RectI(Point pos, Point size)
h = size.y;
}
Point RectI::center() const { return Point(centerX(), centerY()); }
Point RectI::top_left() const { return Point(left(), top()); }
Point RectI::top_right() const { return Point(right(), top()); }
Point RectI::bottom_left() const { return Point(left(), bottom()); }
Point RectI::bottom_right() const { return Point(right(), bottom()); }
int RectI::left() const
{
return x;
}
int RectI::right() const
{
return x + w;
}
int RectI::top() const
{
return y;
}
int RectI::bottom() const
{
return y + h;
}
int RectI::center_x() const
{
return x + w / 2;
}
int RectI::center_y() const
{
return y + h / 2;
}
Point RectI::center() const
{
return Point(x + w / 2, y + h / 2);
}
Point RectI::top_left() const
{
return Point(x, y);
}
Point RectI::top_right() const
{
return Point(x + w, y);
}
Point RectI::bottom_left() const
{
return Point(x, y + h);
}
Point RectI::bottom_right() const
{
return Point(x + w, y + h);
}
bool RectI::overlaps(const RectI& other) const
{
return x < other.x + other.w
&& other.x < x + w
&& y < other.y + other.h
&& other.y < y + h;
}
bool RectI::contains(const Point& point) const
{
@ -84,7 +141,42 @@ char RectI::get_sector(const Vec2& pt) const
return h | v;
}
RectI RectI::operator+(const Point& rhs) const { return RectI(x + rhs.x, y + rhs.y, w, h); }
RectI RectI::operator-(const Point& rhs) const { return RectI(x - rhs.x, y - rhs.y, w, h); }
RectI& RectI::operator+=(const Point& rhs) { x += rhs.x; y += rhs.y; return *this; }
RectI& RectI::operator-=(const Point& rhs) { x -= rhs.x; y -= rhs.y; return *this; }
bool RectI::operator==(const RectI& rhs) const
{
return x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h;
}
bool RectI::operator!=(const RectI& rhs) const
{
return !(*this == rhs);
}
RectI RectI::operator+(const Point& rhs) const
{
return RectI(x + rhs.x, y + rhs.y, w, h);
}
RectI RectI::operator-(const Point& rhs) const
{
return RectI(x - rhs.x, y - rhs.y, w, h);
}
RectI RectI::operator*(const int& rhs) const
{
return RectI(x * rhs, y * rhs, w * rhs, h * rhs);
}
RectI RectI::operator/(const int& rhs) const
{
return RectI(x / rhs, y / rhs, w / rhs, h / rhs);
}
RectI& RectI::operator+=(const Point& rhs)
{
x += rhs.x; y += rhs.y; return *this;
}
RectI& RectI::operator-=(const Point& rhs)
{
x -= rhs.x; y -= rhs.y; return *this;
}

View File

@ -5,28 +5,100 @@
using namespace Blah;
Vec2::Vec2() { x = y = 0; }
Vec2::Vec2(float vx, float vy) { x = vx; y = vy; }
Vec2::Vec2(int vx, float vy) { x = (float)vx; y = vy; }
Vec2::Vec2(float vx, int vy) { x = vx; y = (float)vy; }
Vec2::Vec2(int vx, int vy) { x = (float)vx; y = (float)vy; }
Vec2::Vec2(Point p) { x = (float)p.x; y = (float)p.y; }
Vec2::Vec2()
: x(0)
, y(0)
{}
Vec2 Vec2::operator +(const Vec2 rhs) const { return Vec2(x + rhs.x, y + rhs.y); }
Vec2 Vec2::operator -(const Vec2 rhs) const { return Vec2(x - rhs.x, y - rhs.y); }
Vec2 Vec2::operator /(const float rhs) const { return Vec2(x / rhs, y / rhs); }
Vec2 Vec2::operator *(const float rhs) const { return Vec2(x * rhs, y * rhs); }
Vec2 Vec2::operator-() const { return Vec2(-x, -y); }
Vec2::Vec2(float vx, float vy)
: x(vx)
, y(vy)
{}
Vec2& Vec2::operator +=(const Vec2& rhs) { x += rhs.x; y += rhs.y; return *this; }
Vec2& Vec2::operator -=(const Vec2& rhs) { x -= rhs.x; y -= rhs.y; return *this; }
Vec2& Vec2::operator /=(const Vec2& rhs) { x /= rhs.x; y /= rhs.y; return *this; }
Vec2& Vec2::operator *=(const Vec2& rhs) { x *= rhs.x; y *= rhs.y; return *this; }
Vec2& Vec2::operator/=(float rhs) { x /= rhs; y /= rhs; return *this; }
Vec2& Vec2::operator*=(float rhs) { x *= rhs; y *= rhs; return *this; }
Vec2::Vec2(int vx, float vy)
: x(static_cast<float>(vx))
, y(vy)
{}
bool Vec2::operator ==(const Vec2& rhs) const { return x == rhs.x && y == rhs.y; }
bool Vec2::operator !=(const Vec2& rhs) const { return x != rhs.x || y != rhs.y; }
Vec2::Vec2(float vx, int vy)
: x(vx)
, y(static_cast<float>(vy))
{}
Vec2::Vec2(int vx, int vy)
: x(static_cast<float>(vx))
, y(static_cast<float>(vy))
{}
Vec2::Vec2(Point p)
: x(static_cast<float>(p.x))
, y(static_cast<float>(p.y))
{}
Vec2 Vec2::operator +(const Vec2 rhs) const
{
return Vec2(x + rhs.x, y + rhs.y);
}
Vec2 Vec2::operator -(const Vec2 rhs) const
{
return Vec2(x - rhs.x, y - rhs.y);
}
Vec2 Vec2::operator /(const float rhs) const
{
return Vec2(x / rhs, y / rhs);
}
Vec2 Vec2::operator *(const float rhs) const
{
return Vec2(x * rhs, y * rhs);
}
Vec2 Vec2::operator-() const
{
return Vec2(-x, -y);
}
Vec2& Vec2::operator +=(const Vec2& rhs)
{
x += rhs.x; y += rhs.y; return *this;
}
Vec2& Vec2::operator -=(const Vec2& rhs)
{
x -= rhs.x; y -= rhs.y; return *this;
}
Vec2& Vec2::operator /=(const Vec2& rhs)
{
x /= rhs.x; y /= rhs.y; return *this;
}
Vec2& Vec2::operator *=(const Vec2& rhs)
{
x *= rhs.x; y *= rhs.y; return *this;
}
Vec2& Vec2::operator/=(float rhs)
{
x /= rhs; y /= rhs; return *this;
}
Vec2& Vec2::operator*=(float rhs)
{
x *= rhs; y *= rhs; return *this;
}
bool Vec2::operator ==(const Vec2& rhs) const
{
return x == rhs.x && y == rhs.y;
}
bool Vec2::operator !=(const Vec2& rhs) const
{
return x != rhs.x || y != rhs.y;
}
Vec2 Vec2::normal() const
{
@ -46,8 +118,15 @@ Vec2 Vec2::turn_left() const
return Vec2(-y, x);
}
float Vec2::length() const { return sqrtf(x * x + y * y); }
float Vec2::length_squared() const { return x * x + y * y; }
float Vec2::length() const
{
return sqrtf(x * x + y * y);
}
float Vec2::length_squared() const
{
return x * x + y * y;
}
Vec2 Vec2::perpendicular() const
{
@ -59,9 +138,20 @@ float Vec2::angle() const
return Calc::atan2(y, x);
}
float Vec2::dot(Vec2 a, Vec2 b) { return (a.x * b.x + a.y * b.y); }
float Vec2::dot(float x, float y, Vec2 b) { return (x * b.x + y * b.y); }
float Vec2::dot(float x1, float y1, float x2, float y2) { return (x1 * x2 + y1 * y2); }
float Vec2::dot(Vec2 a, Vec2 b)
{
return (a.x * b.x + a.y * b.y);
}
float Vec2::dot(float x, float y, Vec2 b)
{
return (x * b.x + y * b.y);
}
float Vec2::dot(float x1, float y1, float x2, float y2)
{
return (x1 * x2 + y1 * y2);
}
Vec2 Vec2::transform(const Vec2& vec, const Mat3x2& matrix)
{
@ -77,16 +167,25 @@ Vec2 Vec2::transform(float x, float y, const Mat3x2& matrix)
(x * matrix.m12) + (y * matrix.m22) + matrix.m32);
}
Vec2 transform_normal(const Vec2& vec, const Mat3x2& matrix)
{
return Vec2(
vec.x * matrix.m11 + vec.y * matrix.m21,
vec.x * matrix.m12 + vec.y * matrix.m22);
}
Vec2 transform_normal(float x, float y, const Mat3x2& matrix)
{
return Vec2(
x * matrix.m11 + y * matrix.m21,
x * matrix.m12 + y * matrix.m22);
}
Vec2 Vec2::from_angle(float radians, float length)
{
return Vec2((float)cos(radians) * length, (float)sin(radians) * length);
}
Vec2 Vec2::from_angle(float radians)
{
return from_angle(radians, 1);
}
Vec2 Vec2::lerp(Vec2 a, Vec2 b, float t)
{
if (t == 0)
@ -97,14 +196,14 @@ Vec2 Vec2::lerp(Vec2 a, Vec2 b, float t)
return a + (b - a) * t;
}
Vec2 Vec2::bezier_lerp(Vec2 start, Vec2 b, Vec2 end, float t)
Vec2 Vec2::lerp_bezier(Vec2 start, Vec2 b, Vec2 end, float t)
{
return lerp(lerp(start, b, t), lerp(b, end, t), t);
}
Vec2 Vec2::bezier_lerp(Vec2 start, Vec2 b, Vec2 c, Vec2 end, float t)
Vec2 Vec2::lerp_bezier(Vec2 start, Vec2 b, Vec2 c, Vec2 end, float t)
{
return bezier_lerp(lerp(start, b, t), lerp(b, c, t), lerp(c, end, t), t);
return lerp_bezier(lerp(start, b, t), lerp(b, c, t), lerp(c, end, t), t);
}
Vec2 Vec2::reflect(const Vec2& vector, const Vec2& normal)
@ -116,6 +215,20 @@ Vec2 Vec2::reflect(const Vec2& vector, const Vec2& normal)
vector.y - 2.0f * dot * normal.y);
}
Vec2 Vec2::min(const Vec2& a, const Vec2& b)
{
return Vec2(
a.x < b.x ? a.x : b.x,
a.y < b.y ? a.y : b.y);
}
Vec2 Vec2::max(const Vec2& a, const Vec2& b)
{
return Vec2(
a.x > b.x ? a.x : b.x,
a.y > b.y ? a.y : b.y);
}
const Vec2 Vec2::unit_x = Vec2(1, 0);
const Vec2 Vec2::unit_y = Vec2(0, 1);
const Vec2 Vec2::right = Vec2(1, 0);