-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.qml
229 lines (211 loc) · 10.6 KB
/
main.qml
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import QtQuick 2.3
import QtQuick.Window 2.2
import com.llliuyx.drawline 1.0
Window {
visible: true
Text {
anchors{ bottom: circlesRect.top; bottomMargin: 100; horizontalCenter: circlesRect.horizontalCenter}
font.pixelSize: 50
text: circlesRect.isActiveString
}
Rectangle {
id: circlesRect
property bool isPainting
property real spacing: 300
property real radius: 30
property real canActiveCircleRadius: circlesRect.radius * 3
property var circles: [mapFromItem(rect0, circle0.centerPointX, circle0.centerPointY), mapFromItem(rect1, circle1.centerPointX, circle1.centerPointY), mapFromItem(rect2, circle2.centerPointX, circle2.centerPointY), mapFromItem(rect3, circle3.centerPointX, circle3.centerPointY), mapFromItem(rect4, circle4.centerPointX, circle4.centerPointY), mapFromItem(rect5, circle5.centerPointX, circle5.centerPointY), mapFromItem(rect6, circle6.centerPointX, circle6.centerPointY), mapFromItem(rect7, circle7.centerPointX, circle7.centerPointY), mapFromItem(rect8, circle8.centerPointX, circle8.centerPointY)]
property var circleIds: [circle0, circle1, circle2, circle3, circle4, circle5, circle6, circle7, circle8]
property string isActiveString: ""
width: 900; height: 900
border.color: "black"
anchors.centerIn: parent
Item {
id: rect0
x: 0; y: 0
width: parent.spacing; height: width
Circle {
id: circle0
radius: circlesRect.radius
anchors.centerIn: parent
}
}
Item {
id: rect1
x: parent.spacing; y: 0;
width: parent.spacing; height: width
Circle {
id: circle1
radius: circlesRect.radius
anchors.centerIn: parent
}
}
Item {
id: rect2
x: parent.spacing * 2; y: 0;
width: parent.spacing; height: width
Circle {
id: circle2
radius: circlesRect.radius
anchors.centerIn: parent
}
}
Item {
id: rect3
x: 0; y: parent.spacing;
width: parent.spacing; height: width
Circle {
id: circle3
radius: circlesRect.radius
anchors.centerIn: parent
}
}
Item {
id: rect4
x: parent.spacing; y: parent.spacing;
width: parent.spacing; height: width
Circle {
id: circle4
radius: circlesRect.radius
anchors.centerIn: parent
}
}
Item {
id: rect5
x: parent.spacing * 2; y: parent.spacing;
width: parent.spacing; height: width
Circle {
id: circle5
radius: circlesRect.radius
anchors.centerIn: parent
}
}
Item {
id: rect6
x: 0; y: parent.spacing * 2;
width: parent.spacing; height: width
Circle {
id: circle6
radius: circlesRect.radius
anchors.centerIn: parent
}
}
Item {
id: rect7
x: parent.spacing; y: parent.spacing * 2;
width: parent.spacing; height: width
Circle {
id: circle7
radius: circlesRect.radius
anchors.centerIn: parent
}
}
Item {
id: rect8
x: parent.spacing * 2; y: parent.spacing * 2;
width: parent.spacing; height: width
Circle {
id: circle8
radius: circlesRect.radius
anchors.centerIn: parent
}
}
DrawLine {
id: drawLine
anchors.fill: parent
}
MouseArea {
id: circlesRectMouse
anchors.fill: parent
onPressed: {
circlesRect.resetPaintLine()
for(var i = 0; i < circlesRect.circles.length; i++) {
if(!circlesRect.circleIds[i].isActive) {
if(Math.abs(mouse.x - circlesRect.circles[i].x) <= circlesRect.canActiveCircleRadius && Math.abs(mouse.y - circlesRect.circles[i].y) <= circlesRect.canActiveCircleRadius) {
circlesRect.circleIds[i].isActive = true
circlesRect.isActiveString += i.toString()
drawLine.setDrawLineP1(Qt.point(circlesRect.circles[i].x, circlesRect.circles[i].y))
return
}
}
}
}
onReleased: {
circlesRect.isPainting = false
drawLine.finishedDrawLine()
}
onPositionChanged: {
if(!drawLine.hasP1()) {
for(var i = 0; i < circlesRect.circles.length; i++) {
if(!circlesRect.circleIds[i].isActive) {
if(Math.abs(mouse.x - circlesRect.circles[i].x) <= circlesRect.canActiveCircleRadius && Math.abs(mouse.y - circlesRect.circles[i].y) <= circlesRect.canActiveCircleRadius) {
circlesRect.circleIds[i].isActive = true
circlesRect.isActiveString += i.toString()
drawLine.setDrawLineP1(Qt.point(circlesRect.circles[i].x, circlesRect.circles[i].y))
return
}
}
}
}
else {
drawLine.setDrawLineP2(Qt.point(mouse.x, mouse.y))
for(var i = 0; i < circlesRect.circles.length; i++) {
if(!circlesRect.circleIds[i].isActive) {
if(Math.abs(mouse.x - circlesRect.circles[i].x) <= circlesRect.canActiveCircleRadius && Math.abs(mouse.y - circlesRect.circles[i].y) <= circlesRect.canActiveCircleRadius) {//cross the circle
var p1 = drawLine.getP1()
var midPointX = (p1.x + circlesRect.circles[i].x) / 2
var midPointY = (p1.y + circlesRect.circles[i].y) / 2
for(var j = 0; j < circlesRect.circles.length; j++) {
if(Math.abs(midPointX - circlesRect.circles[j].x) <= circlesRect.canActiveCircleRadius && Math.abs(midPointY - circlesRect.circles[j].y) <= circlesRect.canActiveCircleRadius) {//cross the circle
circlesRect.circleIds[j].isActive = true
circlesRect.isActiveString += j.toString()
drawLine.addLine(Qt.point(circlesRect.circles[j].x, circlesRect.circles[j].y))
return
}
}
circlesRect.circleIds[i].isActive = true
circlesRect.isActiveString += i.toString()
drawLine.addLine(Qt.point(circlesRect.circles[i].x, circlesRect.circles[i].y))
return
}
// else {//以下算法使用点到直线的距离,计算线飘过点的情况,当点距离线一定距离后,连接该点
// var p1 = drawLine.getP1()
// if((Math.pow((p1.x - mouse.x), 2) + Math.pow((p1.y - mouse.y), 2)) > (Math.pow((p1.x - circlesRect.circles[i].x), 2) + Math.pow((p1.y - circlesRect.circles[i].y), 2))) {//排除线还未到达该点的情况
// if((p1.x < mouse.x && p1.x < circlesRect.circles[i].x) || (p1.x > mouse.x && p1.x > circlesRect.circles[i].x) || (p1.y < mouse.y && p1.y < circlesRect.circles[i].y) || (p1.y > mouse.y && p1.y > circlesRect.circles[i].y)) {//排除与某点反方向的另一点
// //点到直线的距离公式
// var numerator = Math.abs( (mouse.y - p1.y)*circlesRect.circles[i].x + (p1.x - mouse.x)*circlesRect.circles[i].y + (mouse.x * p1.y - p1.x * mouse.y) )//|Ax + By + c|
// var denominator = Math.sqrt(Math.pow((mouse.y - p1.y), 2) + Math.pow((p1.x - mouse.x), 2)) //sqrt(A*A + B*B)
// var d = numerator / denominator
// if(d <= circlesRect.canActiveCircleRadius) {
// var midPointX2 = (p1.x + circlesRect.circles[i].x) / 2
// var midPointY2 = (p1.y + circlesRect.circles[i].y) / 2
// for(var j = 0; j < circlesRect.circles.length; j++) {
// if(Math.abs(midPointX2 - circlesRect.circles[j].x) <= circlesRect.canActiveCircleRadius && Math.abs(midPointY2 - circlesRect.circles[j].y) <= circlesRect.canActiveCircleRadius) {//cross the circle
// circlesRect.circleIds[j].isActive = true
// circlesRect.isActiveString += j.toString()
// drawLine.addLine(Qt.point(circlesRect.circles[j].x, circlesRect.circles[j].y))
// return
// }
// }
// circlesRect.circleIds[i].isActive = true
// circlesRect.isActiveString += i.toString()
// drawLine.addLine(Qt.point(circlesRect.circles[i].x, circlesRect.circles[i].y))
// }
// }
// }
// }
}
}
}
}
}
function resetPaintLine() {
for(var i = 0; i < circlesRect.circleIds.length; i++) {
circlesRect.circleIds[i].isActive = false
circlesRect.circleIds[i].radius = circlesRect.radius
}
drawLine.clearLines()
circlesRect.isActiveString = ""
}
}
}