This repository was archived by the owner on Jan 9, 2019. It is now read-only.
forked from baskus/graphbots
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen.py
More file actions
93 lines (68 loc) · 3.05 KB
/
screen.py
File metadata and controls
93 lines (68 loc) · 3.05 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
import pygame
class Screen(object):
def __init__(self, width, height):
self.__surface = pygame.display.set_mode((width, height))
self.__width = width
self.__height = height
self.__window_min_x = None
self.__window_max_x = None
self.__window_min_y = None
self.__window_max_y = None
def set_window(self, min_x, max_x, max_y, curve_data):
zoom=100
if max_x - min_x < zoom:
temp = (max_x + min_x)/2.0
max_x = temp + zoom/2.0
min_x = temp - zoom/2.0
min_x_y = curve_data.value_at_x(min_x)
max_x_y = curve_data.value_at_x(max_x)
min_y = min(min_x_y, max_x_y)
max_y = max(min_x_y, max_x_y, max_y)
max_rx_ry=max(max_x-min_x,max_y-min_y)
if max_x-min_x<max_y-min_y:
temp = (max_x+min_x)/2.0
max_x=temp+max_rx_ry/2.0
min_x=temp-max_rx_ry/2.0
else:
temp = (max_y+min_y)/2.0
max_y=temp+max_rx_ry/2.0
min_y=temp-max_rx_ry/2.0
self.__window_min_x = min_x
self.__window_max_x = max_x
self.__window_min_y = min_y
self.__window_max_y = max_y
print max_x - min_x
def draw_background(self, curve_data):
self.__surface.fill((255,255,255))
min_i = max(0, int(self.__window_min_x) - 1)
max_i = int(self.__window_max_x) + 2
curve = curve_data.range(min_i, max_i)
points = []
range_x = float(self.__window_max_x - self.__window_min_x)
range_y = float(self.__window_max_y - self.__window_min_y)
for i in range(len(curve)):
x = (float(min_i + i) - self.__window_min_x) / range_x
#x = float(min_i + i) / len(curve)
y = 1.0 - (curve[i] - self.__window_min_y) / range_y
points.append((int(x* self.__width), int(y * self.__height)))
# for x, y in zip(range(len(curve)), curve):
# range_x = self.__window_max_x - self.__window_min_x
# range_y = self.__window_max_y - self.__window_min_y
# print "ranges:", range_x, range_y
# print "(x,y):", (x,y)
# print "self.__window_min_x:", self.__window_min_x
# print "self.__window_max_x:", self.__window_max_x
# X = (x - self.__window_min_x) / range_x * self.__width
# Y = (y - self.__window_min_y) / range_y * self.__height
# points.append((X, Y))
#print points
# lines(Surface, color, closed, pointlist, width=1)
pygame.draw.lines(self.__surface, (255, 0, 0), False, points)
def draw_car(self, car):
range_x = float(self.__window_max_x - self.__window_min_x)
range_y = float(self.__window_max_y - self.__window_min_y)
x = (car.position.x - self.__window_min_x) / range_x * self.__width
y = (1.0 - (car.position.y - self.__window_min_y) / range_y) \
* self.__height
# circle(Surface, color, pos, radius, width=0) -> Rect
pygame.draw.circle(self.__surface, (0,0,255), (int(x), int(y)), 5)