-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotLine.py
More file actions
172 lines (151 loc) · 4.57 KB
/
plotLine.py
File metadata and controls
172 lines (151 loc) · 4.57 KB
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
import numpy as np
import matplotlib.pyplot as plt
import math
import time
# dataStyle example:
# start
# x1 y1
# end
# x1 y1
# obstacle
# 100 200 1 50 x x (circle x y kind r a b)
# obstacle
# 300 200 0 x 390 20 (rectangle x y kind r a b)
# line
# 255 (weight [0, 255])
# x1 y1
# x2 y2
# ...
# endline
# line
# 200
# x1 y1
# ...
# endline
#
# ...
#
# clear (if you want to clear previous lines)
# line
# 128 (weight [0, 255])
# x1 y1
# x2 y2
# ...
# endline
# exit
# Run code:
# ./{编译后文件} | python3 plotLine.py
# Red color (from light to dark)
# 'lightpink': '#FFB6C1'
# 'lightcoral': '#F08080'
# 'tomato': '#FF6347'
# 'red': '#FF0000'
# 'firebrick': '#B22222'
# 'brown': '#A52A2A'
# 'darkred': '#8B0000'
mapSize = 500
def getColor(weight): # weight small-> black, weight large-> red. weight range [0, 255]
# frac = 255/math.log(255, math.e)
# redColor = int(math.exp(weight/frac))
redColor = weight
if redColor <= 16:
# print("Set redColor from %d to 16" % redColor)
redColor = 16
if redColor > 255:
# print("Set redColor from %d to 255" % redColor)
redColor = 255
colorStr = hex(int(redColor)) + '0000'
if redColor>100:
width = (redColor-100) / 155 * 0.9 + 0.05
else:
width = 0.05
return '#' + colorStr[2:], width
def plot_data(data):
try:
colorStr = getColor(data[2])
except IndexError:
print("No color weight input! Set as default RED!")
colorStr = '#FF0000'
plt.plot(data[0], data[1], color=colorStr, marker=',')
plt.pause(0.0001)
# print('continue computation')
def plot_line(lineData, colorStr, width):
dataX = []
dataY = []
for data in lineData:
dataX.append(data[0])
dataY.append(data[1])
ln, = plt.plot(dataX, dataY, color=colorStr, linewidth=width)
lineSet.append(ln)
plt.pause(0.0001)
def plot_circle(x, y, r):
circle = plt.Circle((x, y), r, color='b')
ax.add_artist(circle)
def plot_rec(x, y, a, b):
rectangle = plt.Rectangle((x-b/2, y-a/2), a, b, color='b')
ax.add_artist(rectangle)
def plot_start(x, y):
circle = plt.Circle((x, y), 10, color='#98FB98')
ax.add_artist(circle)
def plot_end(x, y):
circle = plt.Circle((x, y), 10, color='#98FB98')
ax.add_artist(circle)
if __name__ == "__main__":
fig, ax = plt.subplots()
# ax.axis("equal")
fig.set_size_inches((5, 5))
plt.ion()
plt.xlim((0, mapSize))
plt.ylim((0, mapSize))
plt.xticks([])
plt.yticks([])
plt.show()
inputStr = input()
countLine = 0
lineSet = []
while True:
if inputStr == 'line':
countLine += 1
# print("Ploting line number %d" % countLine)
lineData = []
weight = int(input())
colorStr, width = getColor(weight)
inputStr = input()
while inputStr != 'endline':
tmp = inputStr.strip().split()
data = np.array(tmp, dtype=np.double)
lineData.append(data)
inputStr = input()
plot_line(lineData, colorStr, width)
elif inputStr == 'start':
x, y = np.array(input().strip().split(), dtype=np.double)
plot_start(x, y)
elif inputStr == 'end':
x, y = np.array(input().strip().split(), dtype=np.double)
plot_end(x, y)
elif inputStr == 'obstacle':
infoObs = input().strip().split()
x, y, kind, r, a, b = np.array(infoObs, dtype=np.double)
if kind == 0:
plot_rec(x, y, a, b)
if kind == 1:
plot_circle(x, y, r)
elif inputStr == 'clear':
print("Lines will be cleared in 1 second.")
plt.pause(1)
for line in lineSet:
line.remove()
lineSet.clear()
countLine = 0
try:
inputStr = input()
if inputStr == "exit":
print("End of input! Exiting")
break
except EOFError:
print("Input has terminated! Exiting")
break
print("The plot will be close in 10 seconds")
#plt.savefig("data-%.8d.png" % counter)
plt.savefig("data.png")
plt.pause(10)