From 1f346a490bdaa4af6f4f1359a6d2b84e69a0042a Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 7 Apr 2022 13:59:16 +0200 Subject: LibGfx: Templatize Gfx::Triangle Previously this was limited to integer triangles, but I want to use it with floats, so let's start by templatizing the class. --- Userland/Libraries/LibGfx/Forward.h | 2 ++ Userland/Libraries/LibGfx/Triangle.cpp | 10 +++++++- Userland/Libraries/LibGfx/Triangle.h | 44 ++++++++++++++++++---------------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/Userland/Libraries/LibGfx/Forward.h b/Userland/Libraries/LibGfx/Forward.h index c6b78ee724..72bea7f1cd 100644 --- a/Userland/Libraries/LibGfx/Forward.h +++ b/Userland/Libraries/LibGfx/Forward.h @@ -28,6 +28,8 @@ class Path; class ShareableBitmap; class StylePainter; struct SystemTheme; + +template class Triangle; template diff --git a/Userland/Libraries/LibGfx/Triangle.cpp b/Userland/Libraries/LibGfx/Triangle.cpp index cc5e1c6d6d..5ba03a2a9d 100644 --- a/Userland/Libraries/LibGfx/Triangle.cpp +++ b/Userland/Libraries/LibGfx/Triangle.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Shannon Booth + * Copyright (c) 2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -9,7 +10,14 @@ namespace Gfx { -String Triangle::to_string() const +template<> +String Triangle::to_string() const +{ + return String::formatted("({},{},{})", m_a, m_b, m_c); +} + +template<> +String Triangle::to_string() const { return String::formatted("({},{},{})", m_a, m_b, m_c); } diff --git a/Userland/Libraries/LibGfx/Triangle.h b/Userland/Libraries/LibGfx/Triangle.h index 605f70fd70..d9f0de5870 100644 --- a/Userland/Libraries/LibGfx/Triangle.h +++ b/Userland/Libraries/LibGfx/Triangle.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, Shannon Booth + * Copyright (c) 2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -11,38 +12,39 @@ namespace Gfx { +template class Triangle { public: - Triangle(IntPoint a, IntPoint b, IntPoint c) + Triangle(Point a, Point b, Point c) : m_a(a) , m_b(b) , m_c(c) { - m_det = (m_b.x() - m_a.x()) * (m_c.y() - m_a.y()) - (m_b.y() - m_a.y()) * (m_c.x() - m_a.x()); + m_determinant = (m_b.x() - m_a.x()) * (m_c.y() - m_a.y()) - (m_b.y() - m_a.y()) * (m_c.x() - m_a.x()); } - IntPoint a() const { return m_a; } - IntPoint b() const { return m_b; } - IntPoint c() const { return m_c; } + Point a() const { return m_a; } + Point b() const { return m_b; } + Point c() const { return m_c; } - bool contains(IntPoint p) const + bool contains(Point p) const { - int x = p.x(); - int y = p.y(); + auto x = p.x(); + auto y = p.y(); - int ax = m_a.x(); - int bx = m_b.x(); - int cx = m_c.x(); + auto ax = m_a.x(); + auto bx = m_b.x(); + auto cx = m_c.x(); - int ay = m_a.y(); - int by = m_b.y(); - int cy = m_c.y(); + auto ay = m_a.y(); + auto by = m_b.y(); + auto cy = m_c.y(); - if (m_det * ((bx - ax) * (y - ay) - (by - ay) * (x - ax)) <= 0) + if (m_determinant * ((bx - ax) * (y - ay) - (by - ay) * (x - ax)) <= 0) return false; - if (m_det * ((cx - bx) * (y - by) - (cy - by) * (x - bx)) <= 0) + if (m_determinant * ((cx - bx) * (y - by) - (cy - by) * (x - bx)) <= 0) return false; - if (m_det * ((ax - cx) * (y - cy) - (ay - cy) * (x - cx)) <= 0) + if (m_determinant * ((ax - cx) * (y - cy) - (ay - cy) * (x - cx)) <= 0) return false; return true; } @@ -50,10 +52,10 @@ public: String to_string() const; private: - int m_det; - IntPoint m_a; - IntPoint m_b; - IntPoint m_c; + T m_determinant { 0 }; + Point m_a; + Point m_b; + Point m_c; }; } -- cgit v1.2.3