-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathVector2D.cpp
173 lines (134 loc) · 3.49 KB
/
Vector2D.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
173
// Vector2D.cpp: implementation of the CVector2D class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Vector2D.h"
#include <math.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CVector2D::CVector2D()
{
}
CVector2D::~CVector2D()
{
}
void CVector2D::GetDiscretionalNextPos(POINT ptStart,
POINT ptEnd, int nTotal, int nNext, POINT& ptNext)
{
if (!nTotal) return;
ptNext.x = ptStart.x + (ptEnd.x - ptStart.x)*nNext/nTotal;
ptNext.y = ptStart.y + (ptEnd.y - ptStart.y)*nNext/nTotal;
}
BOOL CVector2D::GetHorizontalNextPos(POINT ptStart,
POINT ptOnLine, int nLenght, POINT &ptNext)
{
// 注意,坐标系与数学笛卡尔坐标系是相同的
// 判断参数有效性
if(ptOnLine.x - ptStart.x == 0)
{
// 设置默认返回值
ptNext.x = 0;
ptNext.y = 0;
return FALSE;
}
// 如果夹角太小,直接返回,提高效率
if(abs(ptOnLine.y - ptStart.y) <= 1)
{
ptNext.x = ptStart.x + nLenght;
ptNext.y = ptStart.y;
return TRUE;
}
// 夹角余弦,取正
float cosine =
((float)(ptOnLine.x - ptStart.x)/(float)sqrt((double)
(ptOnLine.x - ptStart.x)*(ptOnLine.x - ptStart.x) + (ptOnLine.y - ptStart.y)*(ptOnLine.y - ptStart.y)));
if(cosine < 0)
cosine = (-1)*cosine;
// 确定直线方程 斜率k,纵轴交点b
float k = (float)(ptOnLine.y - ptStart.y)/(float)(ptOnLine.x - ptStart.x);
float b = ptStart.y - k*(float)ptStart.x;
// 得到下一点的坐标,返回
ptNext.x = (int)(ptStart.x + cosine*nLenght);
ptNext.y = (int)(k*ptNext.x + b);
return TRUE;
}
BOOL CVector2D::GetVerticalNextPos(POINT ptStart,
POINT ptOnLine, int nLenght, POINT &ptNext)
{
// 注意,这里做了坐标旋转,X轴与Y轴互换
// 判断参数有效性
if(ptOnLine.y - ptStart.y == 0)
{
// 设置默认返回值
ptNext.x = 0;
ptNext.y = 0;
return FALSE;
}
// 如果夹角太小,直接返回,提高效率
if(abs(ptOnLine.x - ptStart.x) <= 1)
{
ptNext.x = ptStart.x;
ptNext.y = ptStart.y + nLenght;
return TRUE;
}
// 夹角余弦,取正
float cosine =
((float)(ptOnLine.y - ptStart.y)/(float)sqrt((double)
(ptOnLine.x - ptStart.x)*(ptOnLine.x - ptStart.x) + (ptOnLine.y - ptStart.y)*(ptOnLine.y - ptStart.y)));
if(cosine < 0)
cosine = (-1)*cosine;
// 确定直线方程 斜率k,纵轴交点b
float k = (float)(ptOnLine.x - ptStart.x)/(float)(ptOnLine.y - ptStart.y);
float b = ptStart.x - k*(float)ptStart.y;
// 得到下一点的坐标,返回
ptNext.y = (int)(ptStart.y + cosine*nLenght);
ptNext.x = (int)(k*ptNext.y + b);
return TRUE;
}
BOOL CVector2D::RemoveNeighborPointFromVector(POINT ptToRemove, int nRadius, vector<POINT>& pts, POINT& pt)
{
vector<vector<POINT>::iterator> index;
vector<POINT>::iterator pos;
if(pts.size() == 0)
return FALSE;
// 寻找要移除的点
for(pos = pts.begin(); pos != pts.end(); pos++)
{
if(abs((*pos).x - ptToRemove.x) <= nRadius && abs((*pos).y - ptToRemove.y) <= nRadius)
{
index.push_back(pos);
}
}
if(index.empty())
return FALSE;
// 判断是否相邻
BOOL bNeighbor = TRUE;
if(index.size() > 1)
{
for(int i=1; i<index.size(); i++)
{
if(abs((*index[i]).x - (*index[0]).x) > 1
&& abs((*index[i]).y - (*index[0]).y) > 1)
{
bNeighbor = FALSE; // because of no neighbor
}
}
}
// 设置返回结果
if(bNeighbor)
pt = *index[0];
// remove it and return
for(int j=0; j<index.size(); j++)
{
try
{
pts.erase(index[j]); // ?????
}
catch(...)
{
AfxMessageBox(" pts.erase(index[j]) error ");
}
}
return bNeighbor;
}