-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
126 lines (100 loc) · 4.83 KB
/
example.py
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
import sys
from PyQt5.QtCore import QSize, Qt, QTimer, QPointF, QTime, QLineF
from PyQt5.QtGui import QColor, QPainter, QPalette, QPen
from PyQt5.QtWidgets import QApplication, QGridLayout, QSizePolicy, QWidget
from math import sin, cos
from obj_mov import Track, PolyCurvePoint, Point2, LineSegment, ArcSegment
class TrackWidget(QWidget):
def __init__(self, track, scale, parent=None):
super(TrackWidget, self).__init__(parent)
self.antialiased = False
self.frameNo = 0
self.setBackgroundRole(QPalette.Base)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.point_pen = QPen(QColor(0, 0, 0), 4)
self.segment_pen = QPen(QColor(0, 0, 0), 2)
self.base_poly_curve_pen = QPen(QColor(127, 127, 127), 1)
self.current_point_pen = QPen(QColor(200, 0, 0), 5)
self.tangent_line_pen = QPen(QColor(200, 0, 0), 1)
self.time = QTime()
self.time.start()
def set_track(self, track):
self.track = track
def setAntialiased(self, antialiased):
self.antialiased = antialiased
self.update()
def nextAnimationFrame(self):
self.frameNo += 1
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing, self.antialiased)
for i in range(len(self.track.points) - 1):
painter.setPen(self.base_poly_curve_pen)
painter.drawLine(QLineF(self.track.points[i].point.x * scale, self.track.points[i].point.y * scale,
self.track.points[i + 1].point.x * scale, self.track.points[i + 1].point.y * scale))
for segment in self.track.segments:
if type(segment) == LineSegment:
painter.setPen(self.point_pen)
painter.drawPoint(QPointF(segment.start.x * scale, segment.start.y * scale))
painter.drawPoint(QPointF(segment.finish.x * scale, segment.finish.y * scale))
painter.setPen(self.segment_pen)
painter.drawLine(segment.start.x * scale, segment.start.y * scale,
segment.finish.x * scale, segment.finish.y * scale)
elif type(segment) == ArcSegment:
painter.setPen(self.point_pen)
painter.drawPoint(QPointF(segment.start.x * scale, segment.start.y * scale))
painter.drawPoint(QPointF(segment.finish.x * scale, segment.finish.y * scale))
painter.setPen(self.segment_pen)
painter.drawEllipse(QPointF(segment.M.x * scale, segment.M.y * scale), segment.smooth_radius * scale, segment.smooth_radius * scale)
current_time = self.time.elapsed()
current_point = Track.get_track_point(track, current_time)
painter.setPen(self.current_point_pen)
painter.drawPoint(QPointF(current_point.x * scale, current_point.y * scale))
painter.setPen(self.tangent_line_pen)
painter.drawLine(QLineF(current_point.x * scale, current_point.y * scale,
current_point.x * scale + 20 * cos(current_point.angle),
current_point.y * scale + 20 * sin(current_point.angle)))
class Window(QWidget):
def __init__(self, track, scale = 1.0):
super(Window, self).__init__()
layout = QGridLayout()
timer = QTimer(self)
w = TrackWidget(track, scale)
w.setAntialiased(True)
w.set_track(track)
timer.timeout.connect(w.nextAnimationFrame)
layout.addWidget(w, 0, 0)
timer.start(25)
self.setLayout(layout)
self.setWindowTitle("Track")
if __name__ == '__main__':
points = []
points.append(PolyCurvePoint(Point2(1.0, 1.0), 0.2))
points.append(PolyCurvePoint(Point2(5.0, 1.0), 4.0))
points.append(PolyCurvePoint(Point2(10.0, 1.0), 1.0))
points.append(PolyCurvePoint(Point2(12.0, 9.0), 2.0))
points.append(PolyCurvePoint(Point2(1.0, 5.0), 3.0))
points.append(PolyCurvePoint(Point2(2.0, 15.0), 0.0))
points.append(PolyCurvePoint(Point2(8.0, 15.0), 0.0))
# Data preparation
if len(points) > 0:
points[0].velocity = abs(points[0].velocity)
points[-1].velocity = abs(points[-1].velocity)
for i in range(1, len(points) - 1):
if points[i].velocity == 0.0:
points[i].velocity = 1.0
else:
points[i].velocity = abs(points[i].velocity)
track = Track(points)
app = QApplication(sys.argv)
scale = 20.0
window = Window(track, scale)
x_coordinates = []
y_coordinates = []
for p in points:
x_coordinates.append(p.point.x)
y_coordinates.append(p.point.y)
window.resize(max(x_coordinates) * scale + 50.0, max(y_coordinates) * scale + 50.0)
window.show()
sys.exit(app.exec_())