From ef0bc6033b117f84005e236c77ac612371ab830c Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Wed, 2 Apr 2025 09:40:42 -0400 Subject: [PATCH] Ovals --- BeefLibs/Beefy2D/src/geom/Oval.bf | 40 ++++++++++++++++++++++++++++ BeefLibs/Beefy2D/src/gfx/Graphics.bf | 21 +++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 BeefLibs/Beefy2D/src/geom/Oval.bf diff --git a/BeefLibs/Beefy2D/src/geom/Oval.bf b/BeefLibs/Beefy2D/src/geom/Oval.bf new file mode 100644 index 00000000..b6112a0b --- /dev/null +++ b/BeefLibs/Beefy2D/src/geom/Oval.bf @@ -0,0 +1,40 @@ +namespace Beefy.geom; + +struct Oval +{ + public float mX; + public float mY; + public float mRadiusX; + public float mRadiusY; + + public this(float x, float y, float radiusX, float radiusY) + { + mX = x; + mY = y; + mRadiusX = radiusX; + mRadiusY = radiusY; + } + + public this(Rect rect) + { + mX = rect.CenterX; + mY = rect.CenterY; + mRadiusX = rect.mWidth / 2; + mRadiusY = rect.mHeight / 2; + } + + public bool Contains(float x, float y) + { + float dx = (x - mX) / mRadiusX; + float dy = (y - mY) / mRadiusY; + return dx*dx + dy*dy <= 1.0f; + } + + public void Inflate(float x, float y) mut + { + mRadiusX += x; + mRadiusY += y; + } + + public bool Contains(Vector2 vec) => Contains(vec.mX, vec.mY); +} \ No newline at end of file diff --git a/BeefLibs/Beefy2D/src/gfx/Graphics.bf b/BeefLibs/Beefy2D/src/gfx/Graphics.bf index 00d33739..0185948b 100644 --- a/BeefLibs/Beefy2D/src/gfx/Graphics.bf +++ b/BeefLibs/Beefy2D/src/gfx/Graphics.bf @@ -1035,6 +1035,27 @@ namespace Beefy.gfx { Gfx_CopyDrawVertex((.)idx, (.)srcIdx); } + + public void OutlineOval(float x, float y, float radiusX, float radiusY) + { + int numSections = 40; + for (int section < numSections) + { + float ang0 = (section * Math.PI_f * 2) / numSections; + float ang1 = ((section + 1) * Math.PI_f * 2) / numSections; + + float x0 = x + Math.Cos(ang0) * radiusX; + float y0 = y + Math.Sin(ang0) * radiusY; + float x1 = x + Math.Cos(ang1) * radiusX; + float y1 = y + Math.Sin(ang1) * radiusY; + DrawLine(x0, y0, x1, y1); + } + } + + public void OutlineCircle(float x, float y, float radius) + { + OutlineOval(x, y, radius, radius); + } } #else public class Graphics : GraphicsBase