-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphstuff.py
More file actions
144 lines (120 loc) · 4.67 KB
/
Graphstuff.py
File metadata and controls
144 lines (120 loc) · 4.67 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
import math
from math import pi, cos, sin
from EllipseStuff import translateAtAngle
import csv
import sys
# let me use global variables mr python
graphstuff = sys.modules[__name__]
# should probably hand pick these colours, now a simple transition from R to B and B to G
colors = []
colortransition = 3
# R -> B
for i in range(0, colortransition + 1):
colors.append((int(255 - (255 / colortransition) * i), 0, int(0 + (255 / colortransition) * i), 100))
# B-> G
for i in range(0, colortransition + 1):
colors.append((0, int(0 + (255 / colortransition) * i), int(255 - (255 / colortransition) * i), 100))
alpha = 200
graphstuff.max_depth = 0
# load colors from plasma.csv
plasma = []
with open('plasma_mod.csv') as f:
csv_reader = csv.reader(f, delimiter=',')
for color in csv_reader:
plasma.append((int(color[0]), int(color[1]), int(color[2]), alpha))
# reverse
plasma.reverse()
class Node(object):
def __init__(self, id, weight=0, data=None, parent=None):
self.id = id
self.w = weight
self.data = data
self.children = []
self.parent = parent
self.name = ''
self.e_b = 1.0
self.e_b_cache = 1.0
self.strat_one = {}
self.strat_two = {}
def nodeChanged(self):
if self.e_b_cache != self.e_b:
self.e_b_cache = self.e_b
return True
return False
def addChild(self, child):
self.children.append(child)
def setName(self, name):
self.name = name
def __str__(self):
return "i%d w%d : %s" % (self.id, self.w, [n.id for n in self.children])
def __repr__(self):
return "%s" % self
class Rectangle(object):
def __init__(self, c, x, y, t, name, depth, node):
self.c = c
self.x = x
self.y = y
self.t = t
self.depth = depth
self.node = node
self.setbbox()
if depth > graphstuff.max_depth:
graphstuff.max_depth = depth
def update(self, c, x, y, t):
self.c = c
self.x = x
self.y = y
self.t = t
self.setbbox()
def setbbox(self):
self.corners = [translateAtAngle(*self.c, -self.t, self.y / 2, self.x / 2)]
self.corners += [translateAtAngle(*self.c, -self.t, -self.y / 2, self.x / 2)]
self.corners += [translateAtAngle(*self.c, -self.t, self.y / 2, -self.x / 2)]
self.corners += [translateAtAngle(*self.c, -self.t, -self.y / 2, -self.x / 2)]
xs, ys = zip(*self.corners)
self.bbox = (min(xs), min(ys), max(xs), max(ys))
def overlaps(self, rect):
return any([self.pointinside(*corner) for corner in rect.corners])
def pointinside(self, x, y):
# rotate locally per rect to check if it falls inside
nx = x - self.c[0]
ny = y - self.c[1]
rx = nx * cos(self.t) - ny * sin(self.t)
ry = nx * sin(self.t) + ny * cos(self.t)
rx += self.c[0]
ry += self.c[1]
return math.fabs(rx - self.c[0]) < self.y / 2 and math.fabs(ry - self.c[1]) < self.x / 2
def offsetpoint(self, px, py, dx, dy, dt, rc, zoom):
# print('Old values for rect (' + str(self.c[0]) + ', ' + str(self.c[1]) + ')')
# rotate around rc
cx = px - rc[0]
cy = py - rc[1]
newx = cx * math.cos(dt) - cy * math.sin(dt)
newy = cx * math.sin(dt) + cy * math.cos(dt)
# translate back and apply dx, dy
newx *= zoom
newy *= zoom
newx += dx + rc[0]
newy += dy + rc[1]
# print('New values for rect (' + str(newx) + ', ' + str(newy) + ')')
return newx, newy
def drawbbox(self, arcade, dx, dy, dt, rx, ry, zoom):
p1 = self.offsetpoint(self.bbox[0], self.bbox[1], dx, dy, dt, (rx, ry), zoom)
p2 = self.offsetpoint(self.bbox[0], self.bbox[3], dx, dy, dt, (rx, ry), zoom)
p3 = self.offsetpoint(self.bbox[2], self.bbox[3], dx, dy, dt, (rx, ry), zoom)
p4 = self.offsetpoint(self.bbox[2], self.bbox[1], dx, dy, dt, (rx, ry), zoom)
arcade.draw_polygon_filled((p1, p2, p3, p4), color=(0, 255, 0, 100))
def draw(self, arcade, dx, dy, dt, rx, ry, zoom):
# print(self.c)
color_index = max(0, min(int(self.depth / graphstuff.max_depth * len(plasma)), len(plasma) - 1))
color = plasma[color_index]
arcade.draw_rectangle_filled(
*self.offsetpoint(self.c[0], self.c[1], dx, dy, dt, (rx, ry), zoom),
self.x * zoom,
self.y * zoom,
color=color,
tilt_angle=(pi / 2 - self.t + dt) * 180 / 3.141592)
def __str__(self):
return "%d:\tc%d,%d x%d y%d t%f" % (self.node.id, *self.c, self.x, self.y, self.t)
def __repr__(self):
return "%s" % self