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