-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.qml
167 lines (140 loc) · 5.44 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
import QtQuick 2.13
import QtQuick.Controls 2.5
Item {
id: root
width: 300
height: 300
focus: true
MouseArea {
id: globalMouse
hoverEnabled: true
anchors.fill: parent
}
ListModel {
id: listModel
}
Canvas {
id: canvas
anchors.fill: parent
property var path: listModel
function getArea() {
// Shoelace algorithm
// Ref: https://en.wikipedia.org/wiki/Shoelace_formula
var sum = 0
for(var i = 0; i < path.count; i++) {
var point = path.get(i)
var nextPoint = i + 1 != path.count ? path.get(i + 1) : path.get(0)
sum += point.xPos*nextPoint.yPos - nextPoint.xPos*point.yPos
}
return Math.abs(sum) / 2
}
function getCentroid() {
// Calculate the centroid of a non-self-intersecting closed polygon defined by n vertices
// Ref: https://en.wikipedia.org/wiki/Centroid
var areaMultiplied = 6*getArea()
var centroid = {xPos: 0, yPos: 0}
for(var i = 0; i < path.count; i++) {
var point = path.get(i)
var nextPoint = i + 1 != path.count ? path.get(i + 1) : path.get(0)
var multiplier = point.xPos*nextPoint.yPos - nextPoint.xPos*point.yPos
centroid.xPos += (point.xPos + nextPoint.xPos)*multiplier
centroid.yPos += (point.yPos + nextPoint.yPos)*multiplier
}
centroid.xPos = Math.abs(centroid.xPos/areaMultiplied)
centroid.yPos = Math.abs(centroid.yPos/areaMultiplied)
return centroid
}
onPaint: {
var context = getContext("2d");
context.reset();
// Do not paint if there isn't enough points to create a line
if(path.count < 2){
return;
}
var firstPoint = path.get(0)
context.beginPath()
context.moveTo(firstPoint.xPos, firstPoint.yPos)
context.lineWidth = 2
context.strokeStyle = "red"
context.textAlign = "center"
for(var i = 1; i < path.count; i++) {
// Create line with 2 points
var previousPoint = path.get(i - 1)
var point = path.get(i)
// Calculate line middle point
var middlePoint = {xPos: (point.xPos + previousPoint.xPos)/2, yPos: (point.yPos + previousPoint.yPos)/2}
// Translated line to the origin
var translatedLine = {xPos: point.xPos - previousPoint.xPos, yPos: point.yPos - previousPoint.yPos}
// Calculate relative distance between the line points
var relativeDistance = Math.hypot(translatedLine.yPos, translatedLine.xPos)
// Calculate line angle
var angle = Math.atan(translatedLine.yPos/translatedLine.xPos)
// Translate and rotate context to draw distance text
context.save();
context.translate(middlePoint.xPos, middlePoint.yPos);
context.rotate(angle);
context.fillStyle = "blue"
context.fillText(relativeDistance.toFixed(2), 0, -5);
// Restore from translation and rotation
context.restore()
// Draw line to the next point
context.lineTo(point.xPos, point.yPos)
}
// If there is enough points to create a polygon, fill it and add area information
if(path.count > 2) {
var centerPoint = getCentroid()
context.fillStyle = "green"
context.fillText(getArea().toFixed(2), centerPoint.xPos, centerPoint.yPos)
context.fillStyle = "#22000033"
context.fill()
}
context.stroke()
}
}
Repeater {
id: repeater
model: canvas.path
delegate: Rectangle {
width: 10
height: width
color: "black"
radius: width
x: xPos - width/2
y: yPos - height/2
MouseArea {
anchors.fill: parent
drag.target: parent
onPositionChanged: {
canvas.path.get(index).xPos = parent.x + width/2
canvas.path.get(index).yPos = parent.y + height/2
canvas.requestPaint()
}
}
}
}
Keys.onPressed: {
switch(event.key) {
case Qt.Key_N:
var newPoint = {xPos: globalMouse.mouseX, yPos: globalMouse.mouseY}
print("New point:", JSON.stringify(newPoint))
event.accepted = true
canvas.path.append({xPos: globalMouse.mouseX, yPos: globalMouse.mouseY})
canvas.requestPaint()
break;
case Qt.Key_D:
var lastIndex = canvas.path.count - 1
print("Delete last point:", JSON.stringify(canvas.path.get(lastIndex)))
event.accepted = true
canvas.path.remove(lastIndex, 1)
canvas.requestPaint()
break;
default:
print("We don't handle it.")
event.accepted = false
}
}
Label {
anchors.bottom: parent.bottom
text: "New Point: 'N', Delete Point: 'D'"
}
}