-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.qml
70 lines (59 loc) · 1.83 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
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQml 2.12
Window {
id: window
width: 500
height: 500
visible: true
title: qsTr("Qt Random Walker")
color: "black"
Timer {
id: textTimer
interval: 500
repeat: true
running: true
triggeredOnStart: true
onTriggered: {
canvas.requestPaint()
}
}
Canvas {
id: canvas
anchors.fill: parent
property int xCoord: window.width / 2
property int yCoord: window.height / 2
readonly property int radius: 6
property vector2d current: Qt.vector2d(window.width / 2, window.height / 2)
property vector2d prev: current
onPaint: {
var ctx = canvas.getContext("2d")
var step;
var probability = Math.random() * 100
var directionX = ( Math.random() * 2 ) - 1
var directionY = ( Math.random() * 2 ) - 1
console.log(directionX, directionY)
if ( probability < 5 ) {
// Take levy's flight and go to some other direction
step = Qt.vector2d(Math.floor( Math.random() * 25 ) + 75,
Math.floor( Math.random() * 25 ) + 75 )
} else {
step = Qt.vector2d(5,5)
}
step = step.times(Qt.vector2d(directionX, directionY))
current = current.plus(step)
ctx.save();
ctx.beginPath();
ctx.lineWidth = 2;
ctx.fillStyle = "red";
ctx.strokeStyle = "blue";
ctx.moveTo(prev.x, prev.y)
ctx.lineTo(current.x, current.y);
ctx.ellipse(current.x, current.y, radius, radius);
ctx.closePath();
ctx.fill();
ctx.stroke();
prev = current;
}
}
}