From 7a15d39c1c8a3013a0f80ba251644ce20737057f Mon Sep 17 00:00:00 2001 From: Chris Mekelburg Date: Sun, 6 Oct 2024 20:41:48 -0400 Subject: [PATCH] Checkpoint 3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wow, what an adventure! I had some trouble with this part of the lab, mainly because of the error messages the terminal kept feeding back to me. I kept looking at the lines the errors referred to and in several cases, could not figure out what was wrong. When a classmate and I finally compared our codes, that helped me see that the only error was in my final function, and I was able to quickly clean that up and the program worked. I did have to look up the zip function to combine lists that I had previously separated, although looking back there may have been a way to do this without the zip function by not separating the original data set into x and y at the beginning. Overall thought, I feel this code worked well and looks pretty clean. I’m hoping this work with the functions here can help me clean up my drawing project (which is a bit of a mess right now after I made so many changes). I’m going to attempt that next and reach out for help as needed. --- scatterplot.py | 28 ++++++++++++++++++++++++++++ transform.py | 14 +++++++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/scatterplot.py b/scatterplot.py index 838e7be..23d36dc 100644 --- a/scatterplot.py +++ b/scatterplot.py @@ -35,9 +35,37 @@ 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_WIDTH) + draw_y_tick(screen_y_position, tick) + def draw_points(data, color, size): "Draws the scatter plot's points." + x_values = get_x_values(data) + y_values = get_y_values(data) + xmin, xmax = bounds(x_values) + ymin, ymax = bounds(y_values) + x_scale = [] + for x in x_values: + x_scale.append(scale(x, xmin, xmax, 0, constants.PLOT_WIDTH)) + y_scale = [] + for y in y_values: + y_scale.append(scale(y, ymin, ymax, 0, constants.PLOT_HEIGHT)) + coordinates = zip(x_scale, y_scale) + for x_scale,y_scale in coordinates: + draw_point(x_scale,y_scale,color,size) with no_delay(): diff --git a/transform.py b/transform.py index b9d9523..9b8c3bd 100644 --- a/transform.py +++ b/transform.py @@ -43,21 +43,25 @@ def clamp(value, low, high): return low if value > high: return high + if value == low: + return low + if value == high: + return 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. """ - ratio = ((value-start)/(end-start)) - ratio_2 = clamp(ratio,0,1) - return ratio_2 + r = (value - start)/(end - start) + return clamp(r,0,1) + def scale(value, domain_min, domain_max, range_min, range_max): "Given a value within a domain, returns the scaled equivalent within range." ratio_2 = ratio(value, domain_min, domain_max) - scale = range_min + ratio_2*(range_max-range_min) - return scale + scaley = range_min + ratio_2 *(range_max - range_min) + return scaley def get_x_values(points): "Returns the first value for each point in points."