-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathPoint.cpp
More file actions
204 lines (180 loc) · 4.31 KB
/
Point.cpp
File metadata and controls
204 lines (180 loc) · 4.31 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "Point.h"
#include <cmath>
#include <GL/glu.h>
#include <iostream>
using namespace std;
Point::Point()
{
}
Point::Point(int x, int y)
{
this->x = x;
this->y = y;
}
Point::~Point()
{
}
int Point::getX() const
{
return x;
}
int Point::getY() const
{
return y;
}
double Point::distanceTo(const Point &p) const
{
//return abs(this->x - p.x) + abs(this->y - p.y);
return sqrt((this->x - p.x)*(this->x - p.x) + (this->y - p.y)*(this->y - p.y));
}
void Point::draw()
{
glVertex2i(x, y);
}
void Point::markDraw()
{
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
int n = 100; //绘制100个点
GLfloat R = 5.0f; //圆的半径
GLfloat pi = 3.1415926536f;
for(int i=0;i<n;i++)
glVertex2f(x+R*cos(2*pi/n*i), y+R*sin(2*pi/n*i));
glEnd();
glFlush();
}
void Point::centerMarkDraw()
{
glColor3f(0.0, 0.0, 1.0);
//绘制十字
glBegin(GL_LINES);
glVertex2i(x-10, y);
glVertex2i(x+10, y);
glVertex2i(x, y-10);
glVertex2i(x, y+10);
glEnd();
//绘制圆点
glBegin(GL_POLYGON);
int n = 100; //绘制100个点
GLfloat R = 5.0f; //圆的半径
GLfloat pi = 3.1415926536f;
for(int i=0;i<n;i++)
glVertex2f(x+R*cos(2*pi/n*i), y+R*sin(2*pi/n*i));
glEnd();
glFlush();
}
void Point::handleDraw(const Point &p)
{
glColor3f(0.0, 0.0, 1.0); //蓝色
//绘制连线
glBegin(GL_LINES);
glVertex2i(x, y);
glVertex2i(p.x, p.y);
glEnd();
//绘制圆点
glBegin(GL_POLYGON);
int n = 100; //绘制100个点
GLfloat R = 5.0f; //圆的半径
GLfloat pi = 3.1415926536f;
for(int i=0;i<n;i++)
glVertex2f(x+R*cos(2*pi/n*i), y+R*sin(2*pi/n*i));
glEnd();
glFlush();
}
//void Point::clear()
//{
//}
void Point::setPoint(int x, int y)
{
this->x = x;
this->y = y;
}
void Point::setHandlePoint(const Point &begin, const Point &end, int h)
{
Point center((begin.getX()+end.getX())/2, (begin.getY()+end.getY())/2);
if(begin.getX()==end.getX())
setPoint(center.getX(), center.getY()+h);
else
{
double k = (double)(end.getY()-begin.getY())/(double)(end.getX()-begin.getX());
setPoint(int(center.getX()-h*k/sqrt(k*k+1)+0.5), int(center.getY()+h/sqrt(k*k+1)+0.5));
}
}
void Point::translate(const Point &offset)
{
this->x += offset.x;
this->y += offset.y;
}
void Point::rotate(const Point &ctr, double angle)
{
angle = angle * 3.14159265 / 180;
int nx = int(ctr.x + (x-ctr.x)*cos(angle) - (y-ctr.y)*sin(angle) + 0.5);
int ny = int(ctr.y + (x-ctr.x)*sin(angle) + (y-ctr.y)*cos(angle) + 0.5);
x = nx, y = ny;
}
void Point::scale(const Point &base, double sx, double sy)
{
x = x*sx + base.x*(1-sx);
y = y*sy + base.y*(1-sy);
}
void Point::rotateToParallel(const Point &base, const Point &ref, double dist)
{
if(base==ref)
return;
double c = ref.getX() - base.getX(), d = ref.getY() - base.getY(); //现向量base->ref
double rRef = base.distanceTo(ref); //center->ref向量的长度
x = int(base.x+dist*c/rRef+0.5);
y = int(base.y+dist*d/rRef+0.5);
}
void Point::rotateToPerpendicularUp(const Point &base, const Point &ref, double dist)
{
if(base==ref) //base=ref则不变
return;
if(ref.getY()==base.getY())
this->setPoint(base.getX(), base.getY()+dist);
else
{
double c = ref.getX() - base.getX(), d = ref.getY() - base.getY(); //现向量center->ref
double rRef = base.distanceTo(ref);//center->ref向量的长度
double tmp = -abs(dist*d/rRef); //此处rRef!=0
x = base.x + int(tmp+0.5);
y = base.y + int(-c/d*tmp+0.5); //此处d!=0
}
}
void Point::rotateToPerpendicularDown(const Point &base, const Point &ref, double dist)
{
if(base==ref)
return;
if(ref.getY()==base.getY())
this->setPoint(base.getX(), base.getY()-dist);
else
{
double c = ref.getX() - base.getX(), d = ref.getY() - base.getY(); //现向量center->ref
double rRef = base.distanceTo(ref);//center->ref向量的长度
double tmp = abs(dist*d/rRef); //此处rRef!=0
x = base.x + int(tmp+0.5);
y = base.y + int(-c/d*tmp+0.5); //此处d!=0
}
}
// 从上到下,从左到右增大
bool Point::operator<(const Point & p) const
{
return this->y>p.y || (this->y==p.y && this->x<p.x);
}
bool Point::operator==(const Point & p) const
{
return this->x==p.x && this->y==p.y;
}
Point Point::operator-(const Point &p) const
{
return Point(x-p.x, y-p.y);
}
Point Point::operator+(const Point &p) const
{
return Point(x+p.x, y+p.y);
}
ostream & operator<<(ostream & out, const Point & p)
{
out << '(' << p.getX() << ',' << p.getY() << ')';
return out;
}