Kale
Loading...
Searching...
No Matches
Circle.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 "Circle.hpp"
18
21#include <Kale/Math/Ray/Ray.hpp>
23
24#include <stdexcept>
25
26using namespace Kale;
27
31Circle::Circle() : center(0, 0), radius(1) {
32 // Empty Body
33}
34
40Circle::Circle(const Vector2f& center, float radius) : center(center), radius(radius) {
41 // Empty Body
42}
43
51
58 Vector2f tmp = point - center;
59 return tmp.dot(tmp) <= radius * radius;
60}
61
68 if (rect.pointCollision(center)) return true;
69 return lineCollision({rect.point1, rect.point2}) || lineCollision({rect.point2, rect.point3}) ||
70 lineCollision({rect.point3, rect.point4}) || lineCollision({rect.point4, rect.point1});
71}
72
78bool Circle::rectCollision(Rect rect) const {
79 return rect.circleCollision(*this);
80}
81
87bool Circle::circleCollision(Circle circle) const {
88 Vector2f tmp = circle.center - center;
89 return tmp.dot(tmp) <= pow(radius + circle.radius, 2.0f);
90}
91
97bool Circle::rayCollision(Ray ray) const {
98 Vector2f proj = (center - ray.origin).project(ray.direction);
99 if (sign(proj.x) != sign(ray.direction.x)) proj = ray.origin;
100 else proj += ray.origin;
101 return pointCollision(proj);
102}
103
109bool Circle::lineCollision(Line line) const {
110 Vector2f proj = line.point1 + (center - line.point1).project(line.point2 - line.point1);
111 if (proj.x > line.point2.x) proj = line.point2;
112 else if (proj.x < line.point1.x) proj = line.point1;
113 return pointCollision(proj);
114}
T dot(Vector2< T > o) const
Definition Vector.hpp:121
int sign(T val)
Definition Utils.hpp:79
bool rectCollision(RotatedRect rect) const override
Definition Circle.cpp:67
Rect getBoundingBox() const override
Definition Circle.cpp:48
Vector2f center
Definition Circle.hpp:31
bool lineCollision(Line line) const override
Definition Circle.cpp:109
bool circleCollision(Circle circle) const override
Definition Circle.cpp:87
bool rayCollision(Ray ray) const override
Definition Circle.cpp:97
bool pointCollision(Vector2f point) const override
Definition Circle.cpp:57
float radius
Definition Circle.hpp:36
Vector2f point2
Definition Line.hpp:36
Vector2f point1
Definition Line.hpp:31
Vector2f direction
Definition Ray.hpp:38
Vector2f origin
Definition Ray.hpp:33
bool circleCollision(Circle circle) const override
Definition Rect.cpp:108
bool pointCollision(Vector2f point) const override