2020-01-18 23:04:35 +00:00
|
|
|
/*
|
2023-07-15 10:33:22 +00:00
|
|
|
* Copyright (c) 2020, Shannon Booth <shannon@serenityos.org>
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 23:04:35 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 23:04:35 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-28 18:40:53 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-14 23:58:14 +00:00
|
|
|
#include <AK/Forward.h>
|
2020-02-06 11:04:00 +00:00
|
|
|
#include <LibGfx/Point.h>
|
2020-01-18 23:04:35 +00:00
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
namespace Gfx {
|
|
|
|
|
2022-04-07 11:59:16 +00:00
|
|
|
template<typename T>
|
2020-01-18 23:04:35 +00:00
|
|
|
class Triangle {
|
|
|
|
public:
|
2022-04-07 11:59:16 +00:00
|
|
|
Triangle(Point<T> a, Point<T> b, Point<T> c)
|
2020-01-18 23:04:35 +00:00
|
|
|
: m_a(a)
|
|
|
|
, m_b(b)
|
|
|
|
, m_c(c)
|
|
|
|
{
|
2022-04-07 11:59:16 +00:00
|
|
|
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());
|
2020-01-18 23:04:35 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 11:59:16 +00:00
|
|
|
Point<T> a() const { return m_a; }
|
|
|
|
Point<T> b() const { return m_b; }
|
|
|
|
Point<T> c() const { return m_c; }
|
2020-01-18 23:04:35 +00:00
|
|
|
|
2022-04-07 11:59:16 +00:00
|
|
|
bool contains(Point<T> p) const
|
2020-01-18 23:04:35 +00:00
|
|
|
{
|
2022-04-07 11:59:16 +00:00
|
|
|
auto x = p.x();
|
|
|
|
auto y = p.y();
|
2020-01-18 23:04:35 +00:00
|
|
|
|
2022-04-07 11:59:16 +00:00
|
|
|
auto ax = m_a.x();
|
|
|
|
auto bx = m_b.x();
|
|
|
|
auto cx = m_c.x();
|
2020-01-18 23:04:35 +00:00
|
|
|
|
2022-04-07 11:59:16 +00:00
|
|
|
auto ay = m_a.y();
|
|
|
|
auto by = m_b.y();
|
|
|
|
auto cy = m_c.y();
|
2020-01-18 23:04:35 +00:00
|
|
|
|
2022-04-07 11:59:16 +00:00
|
|
|
if (m_determinant * ((bx - ax) * (y - ay) - (by - ay) * (x - ax)) <= 0)
|
2020-01-18 23:04:35 +00:00
|
|
|
return false;
|
2022-04-07 11:59:16 +00:00
|
|
|
if (m_determinant * ((cx - bx) * (y - by) - (cy - by) * (x - bx)) <= 0)
|
2020-01-18 23:04:35 +00:00
|
|
|
return false;
|
2022-04-07 11:59:16 +00:00
|
|
|
if (m_determinant * ((ax - cx) * (y - cy) - (ay - cy) * (x - cx)) <= 0)
|
2020-01-18 23:04:35 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString to_byte_string() const;
|
2020-01-18 23:04:35 +00:00
|
|
|
|
|
|
|
private:
|
2022-04-07 11:59:16 +00:00
|
|
|
T m_determinant { 0 };
|
|
|
|
Point<T> m_a;
|
|
|
|
Point<T> m_b;
|
|
|
|
Point<T> m_c;
|
2020-01-18 23:04:35 +00:00
|
|
|
};
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
}
|