-
Notifications
You must be signed in to change notification settings - Fork 1
/
point.cpp
172 lines (148 loc) · 3.08 KB
/
point.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
172
/**
* PGR 2013 project
* Implements 2D point in float
*
* @author xskota05 Klara Vankova
* xsimet00 Vojtech Simetka
* @date 2013/11/26
* @version 1
* @file point.cpp
*/
#include "point.h"
#include "openglwidget.h"
/**
* @brief Point constructor
* @param x Coordinate x
* @param y Coordinate y
*/
Point::Point(float x, float y)
{
this->x = x;
this->y = y;
}
/**
* @brief Returns coordinate x
* @return Coordinate x
*/
float Point::getX() const
{
return x;
}
/**
* @brief Sets coordinate x to new value
* @param x New x coordinate
*/
void Point::setX(float x)
{
this->x = x;
}
/**
* @brief Returns coodinate y
* @return Coordinate y
*/
float Point::getY() const
{
return y;
}
/**
* @brief Sets coodinate y to new value
* @param y New y coordinate
*/
void Point::setY(float y)
{
this->y = y;
}
/**
* @brief Sets point's new location
* @param x New x coordinate
* @param y New y coordinate
*/
void Point::setLocation(float x, float y)
{
this->x = x;
this->y = y;
}
/**
* @brief Paints point p
* @param p Point to be painted
*/
void Point::paintPoint(Point p)
{
Point::paintPoint(p.getX(), p.getY());
}
/**
* @brief Paints point with coordinates x and y
* @param x Coordinate x of painted point
* @param y Coordinate y of painted point
*/
void Point::paintPoint(float x, float y)
{
// Enables and sets blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Sets size
glPointSize(MINDISTANCE);
// Sets color
glColor4f(0.5, 0.75, 0.95, 0.8);
// Draws primitive
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
// Disables blending
glDisable(GL_BLEND);
}
/**
* @brief Paints point with coordinates x and y
* @param x Coordinate x of painted point
* @param y Coordinate y of painted point
*/
void Point::paintPoint(Point p, float x, float y)
{
Point::paintPoint(p.getX(), p.getY(), x, y);
}
void Point::paintPoint(float px, float py, float x, float y)
{
// Enables and sets blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if (isNearby(px, py, x, y))
{
// Sets size
glPointSize(2*MINDISTANCE);
// Sets color
glColor4f(0.5, 0.75, 0.95, 1);
}
else
{
// Sets size
glPointSize(MINDISTANCE);
// Sets color
glColor4f(0.5, 0.75, 0.95, 0.8);
}
// Draws primitive
glBegin(GL_POINTS);
glVertex2f(px, py);
glEnd();
// Disables blending
glDisable(GL_BLEND);
}
float Point::distance(float x, float y) const
{
float dx = x - this->x;
float dy = y - this->y;
return sqrt((dx*dx) + (dy*dy));
}
float Point::distance(float x1, float y1, float x2, float y2)
{
float dx = x1 - x2;
float dy = y1 - y2;
return sqrt((dx*dx) + (dy*dy));
}
bool Point::isNearby(float x, float y)
{
return Point::distance(this->x, this->y, x, y) < OpenGLWidget::treshold_value;
}
bool Point::isNearby(float x1, float y1, float x2, float y2)
{
return Point::distance(x1, y1, x2, y2) < OpenGLWidget::treshold_value;
}