Kale
Loading...
Searching...
No Matches
Rect.cpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#include "Rect.hpp"
18
20#include <Kale/Math/Ray/Ray.hpp>
23
24#include <stdexcept>
25
26using namespace Kale;
27
31Rect::Rect() : topLeft(0, 0), bottomRight(0, 0) {
32 // Empty Body
33}
34
40Rect::Rect(const Vector2f &topLeft, const Vector2f &bottomRight) : topLeft(topLeft), bottomRight(bottomRight) {
41 // Empty Body
42}
43
49 return (topLeft + bottomRight) / 2;
50}
51
57 return {bottomRight.x, topLeft.y};
58}
59
65 return {topLeft.x, bottomRight.y};
66}
67
73 return *this;
74}
75
81bool Rect::pointCollision(Vector2f point) const {
82 return point >= topLeft && point <= bottomRight;
83}
84
93
99bool Rect::rectCollision(Rect rect) const {
100 return topLeft <= rect.bottomRight && bottomRight >= rect.topLeft;
101}
102
108bool Rect::circleCollision(Circle circle) const {
109 Vector2f closest = circle.center - circle.center.clamp(topLeft, bottomRight);
110 return closest.dot(closest) <= pow(circle.radius, 2.0f);
111}
112
118bool Rect::rayCollision(Ray ray) const {
119 return ray.rectCollision(*this);
120}
121
127bool Rect::lineCollision(Line line) const {
128 return line.rectCollision(*this);
129}
Vector2< T > clamp(T minX, T maxX, T minY, T maxY) const
Definition Vector.hpp:127
T dot(Vector2< T > o) const
Definition Vector.hpp:121
Vector2f center
Definition Circle.hpp:31
float radius
Definition Circle.hpp:36
bool rectCollision(RotatedRect rect) const override
Definition Line.cpp:117
bool rectCollision(RotatedRect rect) const override
Definition Ray.cpp:138
Vector2f center() const
Definition Rect.cpp:48
bool pointCollision(Vector2f point) const override
Definition Rect.cpp:81
Vector2f bottomRight
Definition Rect.hpp:36
Vector2f topRight() const
Definition Rect.cpp:56
bool rayCollision(Ray ray) const override
Definition Rect.cpp:118
bool rectCollision(RotatedRect rect) const override
Definition Rect.cpp:90
bool lineCollision(Line line) const override
Definition Rect.cpp:127
Rect getBoundingBox() const override
Definition Rect.cpp:72
Vector2f bottomLeft() const
Definition Rect.cpp:64
bool circleCollision(Circle circle) const override
Definition Rect.cpp:108
Vector2f topLeft
Definition Rect.hpp:31
bool rectCollision(RotatedRect rect) const override