-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathelevatorsim.py
More file actions
77 lines (59 loc) · 2.12 KB
/
elevatorsim.py
File metadata and controls
77 lines (59 loc) · 2.12 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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Define the floors as y-values:
floors = [0, 1, 2]
class Stage:
height: int
color: str
loc: int
def __init__(self, height, color):
self.loc = 0
self.color = color
self.height = height
class Elevator:
stages: list[Stage] = []
stage_height: int
rects: list[plt.Rectangle] = []
def __init__(self, stage_count, stage_height):
self.stage_height = stage_height
colors = ['red', 'green', 'blue', 'yellow', 'purple', 'orange']
for i in range(stage_count):
self.stages.append(Stage(stage_height, colors[i % len(colors)] ))
self.rects.append(plt.Rectangle((0, 0), 0.1, stage_height, fc=self.stages[i].color))
def move(self, direction):
for i in range(len(self.stages)):
# prev_stage = self.stages[i-1] if i > 0 else None
# stage = self.stages[i]
# stage.loc = (prev_stage.loc if prev_stage else 0) + self.stage_height + direction
self.stages[i].loc += direction * i
def draw(self):
for rect, stage in zip(self.rects, self.stages):
rect.set_y(stage.loc)
return self.rects
# Create a figure and axis:
fig, ax = plt.subplots(figsize=(4,6))
# # Plot horizontal lines for each floor:
# for f in floors:
# ax.hlines(y=f, xmin=-1, xmax=1, color='gray', linewidth=1, linestyles='dashed')
# ax.text(1.1, f, f'Floor {f}', va='center', fontsize=10)
# Set plot limits:
ax.set_xlim(-1, 2)
ax.set_ylim(-0.5, 2.5)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('3-Stage Elevator Visualization')
elevator = Elevator(3, 1)
ax.add_patch(elevator.rects[0])
ax.add_patch(elevator.rects[1])
ax.add_patch(elevator.rects[2])
# Animation update function:
# Here, we make the elevator move smoothly from y=0 to y=2 over 100 frames
def update(frame):
# frame will go from 0 to 99
t = frame / 99.0 # normalize 0 → 1
elevator.move(0.01)
return elevator.draw()
# Create the animation:
ani = animation.FuncAnimation(fig, update, frames=100, interval=50, blit=True)
plt.show()