Kale
Loading...
Searching...
No Matches
Line.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 "Line.hpp"
18
22#include <Kale/Math/Ray/Ray.hpp>
24
25#include <stdexcept>
26
27using namespace Kale;
28
32Line::Line() : point1(0, 0), point2(0, 0) {
33 // Empty Body
34}
35
41Line::Line(const Vector2f &point1, const Vector2f &point2) : point1(point1), point2(point2) {
42 // Empty Body
43}
44
50 Vector2f tmp = point2 - point1;
51 return tmp / tmp.magnitude();
52}
53
59bool Line::isPerpendicular(Ray ray) const {
60 return ray.isPerpendicular(*this);
61}
62
68bool Line::isPerpendicular(Line line) const {
69 return isFloating0((point2 - point1).dot(line.point2 - line.point1));
70}
71
77bool Line::isParallel(Ray ray) const {
78 return ray.isParallel(*this);
79}
80
86bool Line::isParallel(Line line) const {
87 return isFloating0((point2 - point1).cross(line.point2 - line.point1));
88}
89
95 return {
96 {std::min(point1.x, point2.x), std::max(point1.y, point2.y)},
97 {std::max(point1.x, point2.x), std::min(point1.y, point2.y)}
98 };
99}
100
107 Vector2f p = point - point1;
108 Vector2f p2 = point2 - point1;
109 return isFloating0(p.cross(p2)) && p.dot(p2) > 0.0f && (point - point2).dot(point1 - point2) > 0.0f;
110}
111
118 return pointCollision(rect.point1) || pointCollision(rect.point2) || pointCollision(rect.point3) || pointCollision(rect.point4) ||
119 lineCollision({rect.point1, rect.point2}) || lineCollision({rect.point2, rect.point3}) ||
120 lineCollision({rect.point3, rect.point4}) || lineCollision({rect.point4, rect.point1});
121}
122
128bool Line::rectCollision(Rect rect) const {
130 lineCollision({rect.topLeft, rect.topRight()}) || lineCollision({rect.topRight(), rect.bottomRight}) ||
131 lineCollision({rect.bottomRight, rect.bottomLeft()}) || lineCollision({rect.bottomLeft(), rect.topLeft});
132}
133
139bool Line::circleCollision(Circle circle) const {
140 return circle.lineCollision(*this);
141}
142
148bool Line::rayCollision(Ray ray) const {
149 return ray.lineCollision(*this);
150}
151
157bool Line::lineCollision(Line line) const {
158 float l = (point1 - point2).cross(line.point1 - point2);
159 float r = (point1 - point2).cross(line.point2 - point2);
160 if ((l > 0 && r > 0) || (l < 0 && r < 0)) return false;
161
162 // Two lines are parallel & on the same ray
163 if (isFloating0(l) && isFloating0(r)) return point1.x >= line.point2.x && point2.x <= line.point1.x;
164
165 float l2 = (line.point1 - line.point2).cross(point1 - line.point2);
166 float r2 = (line.point1 - line.point2).cross(point2 - line.point2);
167 if ((l2 > 0 && r2 > 0) || (l2 < 0 && r2 < 0)) return false;
168
169 return true;
170}
T dot(Vector2< T > o) const
Definition Vector.hpp:121
std::enable_if< std::is_floating_point< A >::value, T >::type magnitude() const
Definition Vector.hpp:123
T cross(Vector2< T > o) const
Definition Vector.hpp:122
bool isFloating0(T num, T epsilon=std::numeric_limits< T >::epsilon())
Definition Utils.hpp:58
bool lineCollision(Line line) const override
Definition Circle.cpp:109
bool rectCollision(RotatedRect rect) const override
Definition Line.cpp:117
bool lineCollision(Line line) const override
Definition Line.cpp:157
bool pointCollision(Vector2f point) const override
Definition Line.cpp:106
Rect getBoundingBox() const override
Definition Line.cpp:94
bool isParallel(Ray ray) const
Definition Line.cpp:77
bool circleCollision(Circle circle) const override
Definition Line.cpp:139
Vector2f point2
Definition Line.hpp:36
Vector2f point1
Definition Line.hpp:31
bool rayCollision(Ray ray) const override
Definition Line.cpp:148
bool isPerpendicular(Ray ray) const
Definition Line.cpp:59
Vector2f getNormal() const
Definition Line.cpp:49
bool isPerpendicular(Ray ray) const
Definition Ray.cpp:80
bool isParallel(Ray ray) const
Definition Ray.cpp:98
bool lineCollision(Line line) const override
Definition Ray.cpp:193
Vector2f bottomRight
Definition Rect.hpp:36
Vector2f topRight() const
Definition Rect.cpp:56
Vector2f bottomLeft() const
Definition Rect.cpp:64
Vector2f topLeft
Definition Rect.hpp:31