mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 06:07:34 +00:00
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.
This commit is contained in:
parent
f602bbf135
commit
1f346a490b
3 changed files with 34 additions and 22 deletions
|
@ -28,6 +28,8 @@ class Path;
|
||||||
class ShareableBitmap;
|
class ShareableBitmap;
|
||||||
class StylePainter;
|
class StylePainter;
|
||||||
struct SystemTheme;
|
struct SystemTheme;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
class Triangle;
|
class Triangle;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
|
* Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
|
||||||
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -9,7 +10,14 @@
|
||||||
|
|
||||||
namespace Gfx {
|
namespace Gfx {
|
||||||
|
|
||||||
String Triangle::to_string() const
|
template<>
|
||||||
|
String Triangle<int>::to_string() const
|
||||||
|
{
|
||||||
|
return String::formatted("({},{},{})", m_a, m_b, m_c);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
String Triangle<float>::to_string() const
|
||||||
{
|
{
|
||||||
return String::formatted("({},{},{})", m_a, m_b, m_c);
|
return String::formatted("({},{},{})", m_a, m_b, m_c);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
|
* Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
|
||||||
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -11,38 +12,39 @@
|
||||||
|
|
||||||
namespace Gfx {
|
namespace Gfx {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
class Triangle {
|
class Triangle {
|
||||||
public:
|
public:
|
||||||
Triangle(IntPoint a, IntPoint b, IntPoint c)
|
Triangle(Point<T> a, Point<T> b, Point<T> c)
|
||||||
: m_a(a)
|
: m_a(a)
|
||||||
, m_b(b)
|
, m_b(b)
|
||||||
, m_c(c)
|
, 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; }
|
Point<T> a() const { return m_a; }
|
||||||
IntPoint b() const { return m_b; }
|
Point<T> b() const { return m_b; }
|
||||||
IntPoint c() const { return m_c; }
|
Point<T> c() const { return m_c; }
|
||||||
|
|
||||||
bool contains(IntPoint p) const
|
bool contains(Point<T> p) const
|
||||||
{
|
{
|
||||||
int x = p.x();
|
auto x = p.x();
|
||||||
int y = p.y();
|
auto y = p.y();
|
||||||
|
|
||||||
int ax = m_a.x();
|
auto ax = m_a.x();
|
||||||
int bx = m_b.x();
|
auto bx = m_b.x();
|
||||||
int cx = m_c.x();
|
auto cx = m_c.x();
|
||||||
|
|
||||||
int ay = m_a.y();
|
auto ay = m_a.y();
|
||||||
int by = m_b.y();
|
auto by = m_b.y();
|
||||||
int cy = m_c.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;
|
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;
|
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 false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -50,10 +52,10 @@ public:
|
||||||
String to_string() const;
|
String to_string() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_det;
|
T m_determinant { 0 };
|
||||||
IntPoint m_a;
|
Point<T> m_a;
|
||||||
IntPoint m_b;
|
Point<T> m_b;
|
||||||
IntPoint m_c;
|
Point<T> m_c;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue