-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrectangle.cpp
171 lines (151 loc) · 3.65 KB
/
rectangle.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* PGR 2013 project
* Rectangle representation in editor
*
* @author xskota05 Klara Vankova
* xsimet00 Vojtech Simetka
* @date 2013/11/26
* @version 1
* @file rectangle.cpp
*/
#include "rectangle.h"
#include <QDebug>
#include <QRect>
/**
* @brief Rectangle constructor
* @param x Upper left corner's coordinate x
* @param y Upper left corner's coordinate y
* @param w Rectangle's width
* @param h Rectangle's height
*/
Rectangle::Rectangle(float x1, float y1, float x2, float y2)
: p1(x1,y1)
, p2(x2,y1)
, p3(x2,y2)
, p4(x1,y2)
{
}
Rectangle::~Rectangle()
{
}
/**
* @brief Paints rectangle to OpenGL context
*/
void Rectangle::paintMe() const
{
// Sets color
glLineWidth(1.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_SMOOTH);
glColor4f(0.5, 0.75, 0.95, 0.7);
// Draws rectangle
glBegin(GL_LINES);
// Top
glVertex2f(this->p1.getX(), this->p1.getY());
glVertex2f(this->p2.getX(), this->p2.getY());
// Right
glVertex2f(this->p2.getX(), this->p2.getY());
glVertex2f(this->p3.getX(), this->p3.getY());
// Bottom
glVertex2f(this->p3.getX(), this->p3.getY());
glVertex2f(this->p4.getX(), this->p4.getY());
// Left
glVertex2f(this->p4.getX(), this->p4.getY());
glVertex2f(this->p1.getX(), this->p1.getY());
glEnd();
glDisable(GL_LINE_SMOOTH);
glDisable(GL_BLEND);
}
/**
* @brief Paints points of the rectangle
*/
void Rectangle::paintPoints(float x, float y) const
{
Point::paintPoint(this->p1, x,y);
Point::paintPoint(this->p2, x,y);
Point::paintPoint(this->p3, x,y);
Point::paintPoint(this->p4, x,y);
}
/**
* @brief Resize rectangle
* @param x1 First point's coordinate x
* @param y1 First point's coordinate y
* @param x2 Second point's coordinate x
* @param y2 Second point's coordinate y
*/
void Rectangle::resize(float x1, float y1, float x2, float y2)
{
this->p1.setX(x1);
this->p1.setY(y1);
this->p2.setX(x2);
this->p2.setY(y1);
this->p3.setX(x2);
this->p3.setY(y2);
this->p4.setX(x1);
this->p4.setY(y2);
}
/**
* @brief Gets first point of the rectangle
* @return First rectangle point
*/
Point Rectangle::getP1() const
{
return p1;
}
/**
* @brief Sets rectangle's first point to new value
* @param value New first point's coordinates
*/
void Rectangle::setP1(const Point &value)
{
p1 = value;
}
/**
* @brief Gets second point of the rectangle
* @return Second rectangle point
*/
Point Rectangle::getP2() const
{
return p2;
}
/**
* @brief Sets rectangle's second point to new value
* @param value New second point's coordinates
*/
void Rectangle::setP2(const Point &value)
{
p2 = value;
}
bool Rectangle::getCounterPoint(float x, float y, float *ox, float *oy, Qt::Corner *orientation) const
{
if (Point::isNearby(x, y, this->p1.getX(), this->p1.getY()))
{
*ox = this->p3.getX();
*oy = this->p3.getY();
*orientation = Qt::TopLeftCorner;
return true;
}
else if (Point::isNearby(x, y, this->p2.getX(), this->p2.getY()))
{
*ox = this->p4.getX();
*oy = this->p4.getY();
*orientation = Qt::TopRightCorner;
return true;
}
else if (Point::isNearby(x, y, this->p3.getX(), this->p3.getY()))
{
*ox = this->p1.getX();
*oy = this->p1.getY();
*orientation = Qt::BottomRightCorner;
return true;
}
else if (Point::isNearby(x, y, this->p4.getX(), this->p4.getY()))
{
*ox = this->p2.getX();
*oy = this->p2.getY();
*orientation = Qt::BottomLeftCorner;
return true;
}
return false;
}