-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwall.cpp
More file actions
69 lines (54 loc) · 1.7 KB
/
Copy pathwall.cpp
File metadata and controls
69 lines (54 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "wall.h"
Wall::Wall() : m_startPoint(0, 0), m_endPoint(0, 0) {}
Wall::Wall(const QPoint &start, const QPoint &end): m_startPoint(start), m_endPoint(end) {}
QPoint Wall::startPoint() const
{
return m_startPoint;
}
QPoint Wall::endPoint() const
{
return m_endPoint;
}
QLine Wall::line() const
{
return QLine(m_startPoint, m_endPoint);
}
void Wall::setStartPoint(const QPoint &point)
{
m_startPoint = point;
}
void Wall::setEndPoint(const QPoint &point)
{
m_endPoint = point;
}
bool Wall::isHorizontal() const
{
// Difference in y value is less than a threshold.
return qAbs(m_startPoint.y() - m_endPoint.y()) < 5;
}
bool Wall::isVertical() const
{
// Difference is x value is less than a threshold.
return qAbs(m_startPoint.x() - m_endPoint.x()) < 5;
}
void Wall::draw(QPainter &painter) const
{
painter.drawLine(m_startPoint, m_endPoint);
}
bool Wall::intersects(const QRectF &rect) const
{
QLineF top(rect.topLeft(), rect.topRight());
QLineF right(rect.topRight(), rect.bottomRight());
QLineF bottom(rect.bottomRight(), rect.bottomLeft());
QLineF left(rect.bottomLeft(), rect.topLeft());
QLineF wallLine(m_startPoint, m_endPoint);
QPointF intersection;
if (wallLine.intersects(top, &intersection) == QLineF::BoundedIntersection ||
wallLine.intersects(right, &intersection) == QLineF::BoundedIntersection ||
wallLine.intersects(bottom, &intersection) == QLineF::BoundedIntersection ||
wallLine.intersects(left, &intersection) == QLineF::BoundedIntersection) {
return true;
}
// Check whether the wall is completely inside the rectangle
return rect.contains(m_startPoint) && rect.contains(m_endPoint);
}