Compare commits
6 Commits
Author | SHA1 | Date |
---|---|---|
|
0eae309b34 | |
|
1467e6e70f | |
|
d01f07eea0 | |
|
e198c13b95 | |
|
966af3b529 | |
|
5f8bdc7998 |
|
@ -35,9 +35,29 @@ def draw_scatterplot(data, size=5, color="black"):
|
|||
|
||||
def draw_axes(data):
|
||||
"Draws the scatter plot's axes."
|
||||
draw_x_axis()
|
||||
x_values = get_x_values(data)
|
||||
xmin, xmax = bounds(x_values)
|
||||
ticks = get_tick_values(xmin, xmax)
|
||||
for tick in ticks:
|
||||
screen_x_position = scale(tick, xmin, xmax, 0, constants.PLOT_WIDTH)
|
||||
draw_x_tick(screen_x_position, tick)
|
||||
draw_y_axis()
|
||||
y_values = get_y_values(data)
|
||||
ymin, ymax = bounds(y_values)
|
||||
ticks = get_tick_values(ymin, ymax)
|
||||
for tick in ticks:
|
||||
screen_y_position = scale(tick, ymin, ymax, 0, constants.PLOT_HEIGHT)
|
||||
draw_y_tick(screen_y_position, tick)
|
||||
|
||||
def draw_points(data, color, size):
|
||||
"Draws the scatter plot's points."
|
||||
xmin, xmax = bounds(get_x_values(data))
|
||||
ymin, ymax = bounds(get_y_values(data))
|
||||
for x, y in data:
|
||||
sx = scale(x, xmin, xmax, 0, constants.PLOT_WIDTH)
|
||||
sy = scale(y, ymin, ymax, 0, constants.PLOT_HEIGHT)
|
||||
draw_point(sx, sy, color, size)
|
||||
|
||||
with no_delay():
|
||||
data = generate_data(50, 10, 500, 5, 400, 1000)
|
||||
|
|
|
@ -12,8 +12,9 @@ from transform import (
|
|||
def test(function, arguments, expected):
|
||||
observed = function(*arguments)
|
||||
if observed != expected:
|
||||
fname = function.__name__
|
||||
args = ', '.join(str(arg) for arg in arguments)
|
||||
print(f"Error: Expected {function}({args}) to equal {expected}, but it was {observed}")
|
||||
print(f"Error: Expected {fname}({args}) to equal {expected}, but it was {observed}")
|
||||
|
||||
test(maximum, [[0, 1, 2, 3]], 3)
|
||||
test(maximum, [[-10, -20, -30]], -10)
|
||||
|
@ -21,14 +22,14 @@ test(minimum, [[0, 1, 2, 3]], 0)
|
|||
test(minimum, [[-10, -20, -30]], -30)
|
||||
test(bounds, [[0, 1, 2, 3]], [0, 3])
|
||||
test(bounds, [[-10, -20, -30]], [-30, -10])
|
||||
test(clamp, [[10, 0, 100]], 10)
|
||||
test(clamp, [[-10, 0, 100]], 0)
|
||||
test(clamp, [[104, 0, 100]], 100)
|
||||
test(ratio, [[5, 0, 10]], 0.5)
|
||||
test(ratio, [[167, 100, 200]], 0.67)
|
||||
test(ratio, [[8, 10, 0]], 0.2)
|
||||
test(ratio, [[4, 10, 20]], 0.0)
|
||||
test(scale, [[4, 0, 10, 0, 100]], 40)
|
||||
test(scale, [[160, 120, 240, 0, 100]], 100/3)
|
||||
test(clamp, [10, 0, 100], 10)
|
||||
test(clamp, [-10, 0, 100], 0)
|
||||
test(clamp, [104, 0, 100], 100)
|
||||
test(ratio, [5, 0, 10], 0.5)
|
||||
test(ratio, [167, 100, 200], 0.67)
|
||||
test(ratio, [8, 10, 0], 0.2)
|
||||
test(ratio, [4, 10, 20], 0.0)
|
||||
test(scale, [4, 0, 10, 0, 100], 40)
|
||||
test(scale, [180, 120, 240, 0, 100], 50)
|
||||
test(get_x_values, [[[0, 5], [1, 5], [2, 5]]], [0, 1, 2])
|
||||
test(get_y_values, [[[0, 5], [1, 5], [2, 5]]], [5, 5, 5])
|
||||
|
|
17
transform.py
17
transform.py
|
@ -6,38 +6,39 @@
|
|||
|
||||
def maximum(data):
|
||||
"Returns the largest number in data"
|
||||
raise NotImplementedError
|
||||
return max(data)
|
||||
|
||||
def minimum(data):
|
||||
"Returns the smallest number in data"
|
||||
raise NotImplementedError
|
||||
return min(data)
|
||||
|
||||
def bounds(data):
|
||||
"Returns a list of the smallest and largest numbers in data"
|
||||
raise NotImplementedError
|
||||
return [min(data), max(data)]
|
||||
|
||||
def clamp(value, low, high):
|
||||
"""Clamps a value to a range from low to high.
|
||||
Returns value if it is between low and high.
|
||||
If value is lower than low, returns low. If value is higher than high, returns high.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
return min(max(low, value), high)
|
||||
|
||||
def ratio(value, start, end):
|
||||
"""Returns a number from 0.0 to 1.0, representing how far along value is from start to end.
|
||||
The return value is clamped to [0, 1], so even if value is lower than start, the return
|
||||
value will not be lower than 0.0.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
return (value - start) / (end - start)
|
||||
|
||||
def scale(value, domain_min, domain_max, range_min, range_max):
|
||||
"Given a value within a domain, returns the scaled equivalent within range."
|
||||
raise NotImplementedError
|
||||
r = clamp(ratio(value, domain_min, domain_max), 0, 1)
|
||||
return range_min + r * (range_max - range_min)
|
||||
|
||||
def get_x_values(points):
|
||||
"Returns the first value for each point in points."
|
||||
raise NotImplementedError
|
||||
return [x for x, y in points]
|
||||
|
||||
def get_y_values(points):
|
||||
"Returns the second value for each point in points."
|
||||
raise NotImplementedError
|
||||
return [y for x, y in points]
|
||||
|
|
Loading…
Reference in New Issue