Skip to content

Commit 456d5cd

Browse files
author
Xiretza
committed
[graph] finish numpy rewrite
1 parent 1963b19 commit 456d5cd

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

graph.py

+16-29
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use_braille = True
77

88
columns = 160
9-
rows = 40
9+
rows = 50
1010

1111
omega = 0.1
1212
offset = 0
@@ -52,7 +52,7 @@ def get_polar(self):
5252
return (mag, phi)
5353

5454
class Matrix:
55-
def __init__(self, w, h, fill):
55+
def __init__(self, w, h):
5656
self.w = w
5757
self.h = h
5858
self._data = np.zeros((h, w))
@@ -75,19 +75,6 @@ def rows(self):
7575
def points(self):
7676
return self._data.flat
7777

78-
def extract(self, _start, _end):
79-
start = Coord(min(_start.x, _end.x), min(_start.y, _end.y))
80-
end = Coord(max(_start.x, _end.x), max(_start.y, _end.y))
81-
82-
#print("extract from %s to %s" % (start, end))
83-
84-
#diffx = end.x-start.x
85-
#diffy = end.y-start.y
86-
87-
#new_matrix = Matrix(diffx+1, diffy+1, 0)
88-
89-
return self._data[start.y:end.y+1, start.x:end.x+1]
90-
9178
def draw_line(self, start, end):
9279
diffx = end.x-start.x
9380
diffy = end.y-start.y
@@ -104,23 +91,23 @@ def render(self, ren):
10491
ren.draw(self)
10592

10693
def scale(self, new_w, new_h):
107-
new_matrix = Matrix(new_w, new_h, 0)
94+
new_matrix = Matrix(new_w, new_h)
10895

10996
factor_w = self.w/new_w
11097
factor_h = self.h/new_h
11198

11299
for y in range(new_h):
113100
for x in range(new_w):
114-
extracted = self.extract(
115-
Coord(
116-
math.floor(factor_w * x + 0.5),
117-
math.floor(factor_h * y + 0.5)
118-
),
119-
Coord(
120-
math.floor(factor_w * (x+1) - 0.5),
121-
math.floor(factor_h * (y+1) - 0.5)
122-
)
123-
)
101+
start_x = math.floor(factor_w * x + 0.5)
102+
end_x = math.floor(factor_w * (x+1) + 0.5)
103+
104+
start_y = math.floor(factor_h * y + 0.5)
105+
end_y = math.floor(factor_h * (y+1) + 0.5)
106+
107+
extracted = self._data[
108+
start_y:end_y,
109+
start_x:end_x
110+
]
124111

125112
avg = extracted.mean()
126113

@@ -146,8 +133,8 @@ def draw(self, matrix):
146133
self.matrix = matrix.scale(self.w, self.h)
147134
for row in reversed(self.matrix.rows()):
148135
print("".join([
149-
[" ", "-", "o", "x"][max(min(math.floor(p/40), 3), 0)]
150-
#"x" if p else " "
136+
#[" ", "-", "o", "x"][max(min(math.floor(p/40), 3), 0)]
137+
"x" if p else " "
151138
for p in row
152139
]))
153140

@@ -164,7 +151,7 @@ def draw(self, matrix):
164151
for n in vals
165152
]
166153

167-
matrix = Matrix(300, 100, 0)
154+
matrix = Matrix(300, 100)
168155

169156
for col, val in enumerate(vals):
170157
# get previous column's value

0 commit comments

Comments
 (0)