-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaddpolygon.cpp
174 lines (148 loc) · 5.77 KB
/
addpolygon.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
174
/******************************************************************************************/
/**** FILE: addpolygon.cpp ****/
/******************************************************************************************/
#include "wisim_gui.h"
#include "wisim.h"
#include "gconst.h"
#include "qapplication.h"
#include "qmessagebox.h"
//Added by qt3to4:
#include <Q3PointArray>
#include <QMouseEvent>
#define CGDEBUG 0
/******************************************************************************************/
/**** FUNCTION: FigureEditor::addPolygon ****/
/******************************************************************************************/
void FigureEditor::addPolygon(int mouseAction, QMouseEvent* e)
{
static int count = 0;
static Q3CanvasEllipse* ellipse[200];
static Q3CanvasLine* edge[200];
static Q3PointArray pa(0);
Q3CanvasPolygon* polygon;
double s_one = 0;
double s_two = 0;
double s_three = 0;
double s_four = 0;
int *crossline_value = new int [200];
int sign=1;
static int flag = 1;
//QCanvasLine* line = new QCanvasLine(canvas());
static Q3CanvasLine* line;
int canvas_x;
int canvas_y;
for (int i=0;i<=199;i++) {crossline_value[i]=1;}
switch(mouseAction) {
case GConst::mousePress:
#if CGDEBUG
printf("Mouse Pressed \n");
#endif
if (e->button() == Qt::LeftButton) {
ellipse[count] = new Q3CanvasEllipse(5,5,canvas());
ellipse[count]->setBrush( QColor(Qt::red) );
ellipse[count]->move(e->x(),e->y());
ellipse[count]->setZ(GConst::selectRegionZ);
ellipse[count]->show();
line = new Q3CanvasLine(canvas());
line->setZ(GConst::selectRegionZ);
pa.resize(count+1);
pa[count] = e->pos();
if (count <= 2) {
edge[count] = line;
}
if (count > 2) {
edge[count] = line;
for (int i=0;i<count-2;i++) {
s_one=(pa.point(count).y()-pa.point(i).y())*(pa.point(i+1).x()-pa.point(i).x())-(pa.point(count).x()-pa.point(i).x())*(pa.point(i+1).y()-pa.point(i).y());
s_two=(pa[count-1].y()-pa[i].y())*(pa[i+1].x()-pa[i].x())-(pa[count-1].x()-pa[i].x())*(pa[i+1].y()-pa[i].y());
s_three=(pa[i].y()-pa[count].y())*(pa[count-1].x()-pa[count].x())-(pa[i].x()-pa[count].x())*(pa[count-1].y()-pa[count].y());
s_four=(pa[i+1].y()-pa[count].y())*(pa[count-1].x()-pa[count].x())-(pa[i+1].x()-pa[count].x())*(pa[count-1].y()-pa[count].y());
#if CGDEBUG
printf("\n%f",s_one);
printf("\n%f",s_two);
printf("\n%f",s_three);
printf("\n%f",s_four);
#endif
if (s_one*s_two<=0 && s_three*s_four<=0) {
crossline_value[i] = 0;
} else {
crossline_value[i] = 1;
}
#if CGDEBUG
printf("\n linecross occur? line [%d] &line [%d] %d\n",i+1,count,crossline_value[i]);
#endif
sign=sign*crossline_value[i];
#if CGDEBUG
printf("\ntext sign:sign = %d\n",sign);
#endif
}
#if CGDEBUG
printf("\nsign = %d\n",sign);
#endif
flag = sign;
if (sign == 0) {
#if CGDEBUG
printf("lines crossing,please draw the line again!");
#endif
//edge[count-1]->setVisible(false);
line->setVisible(false);
ellipse[count]->setVisible(false);
edge[count]->setVisible(false);
count = count-1;
//edge[count] = line;
edge[count]->setVisible(false);
edge[count] = line;
}
}
#if CGDEBUG
printf("Setting pa[%d]\n", count);
#endif
canvas_to_xy(canvas_x,canvas_y,e->x(),e->y());
#if CGDEBUG
printf("x = %d\n",canvas_x);
printf("y = %d\n",canvas_y);
#endif
count++;
} else if (e->button() == Qt::RightButton) {
if (count<=2) {
polygon = (Q3CanvasPolygon *) NULL;
} else {
polygon = new Q3CanvasPolygon(canvas());
polygon->setPoints(pa);
polygon->setBrush( QColor(Qt::green) );
polygon->setZ(GConst::selectRegionZ);
polygon->show();
}
for (int i=0;i<count;i++) {
delete ellipse[i];
delete edge[i];
}
setMouseMode(GConst::selectMode);
count = 0;
emit done_drawing_polygon(polygon);
#if CGDEBUG
printf("Right Button\n");
#endif
}
break;
case GConst::mouseMove:
//printf("Mouse Moved \n");
if (count >= 1) {
line->setPoints(pa.point(count-1).x(),pa.point(count-1).y(),e->x(),e->y());
line->show();
}
break;
case GConst::mouseRelease:
#if CGDEBUG
printf("Mouse Released\n");
#endif
break;
case GConst::mouseDoubleClick:
#if CGDEBUG
printf("Mouse Double-Clicked \n");
#endif
break;
}
}
/******************************************************************************************/
#undef CGDEBUG