p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; min-height: 14.0px}
I need to set up a programme in Python 3 that would allow the user to draw lines by moving their mouse (Screenshot 1). The color of the line should change depending on the location of the cursor; the length of the line needs to change depending on the length of the line.
Task 1: using the existing code make it so the cursor isn’t able to draw outside the canvas by using functions line_drag_handler() and clamp(), thereby limiting the x and y coordinates.
Task 2: add colors to the program using the functions new_colour(), green_value(), red_value(), blue_value(), remap()
Task 3: Change the size of the line based on its length every time the function line_drag_handler() is used using the variables previous_x, previous_y that count how far the user has dragged the cursor
"""turtle_painter.py: Paint program developed using the turtle library.""" import argparse import turtle CANVAS_WIDTH = 500 CANVAS_HEIGHT = 500 MIN_PEN_SIZE = 1 MAX_PEN_SIZE = 10 BRUSH_TYPES = ["line"] # brush types available WHITE = (255, 255, 255) # RGB value representing white previous_x: int # Keep track of the previous x position of our drawing turtle previous_y: int # Keep track of the previous y position of our drawing turtle def green_value(x_position: int, y_position: int, canvas_width: int, canvas_height: int) -> int: """ Calculates the green value based on how far the turtle is from the centre of the canvas (0, 0). :param canvas_height: maximum height :type canvas_height: int :param canvas_width: maximum width :type canvas_width: int :param x_position: x position of turtle on the canvas :type x_position: int :param y_position: y position of turtle on the canvas :type y_position: int :return: The level of green between 0 and 255 :rtype: int >>> green_value(0, 0, 500, 500) 0 >>> green_value(250, 0, 500, 500) 180 >>> green_value(-250, 0, 500, 500) 180 >>> green_value(0, 250, 500, 500) 180 >>> green_value(0, -250, 500, 500) 180 >>> green_value(250, 250, 500, 500) 255 >>> green_value(-250, -250, 500, 500) 255 """ green_val: int = 0 # ADD YOUR CODE HERE return green_val def red_value(x_position: int, canvas_width: int) -> int: """ Calculate the red value based on how far the turtle is from the left hand side of the canvas. The red value increases the further to the right that the turtle moves. At the right hand side of the canvas the red value will be 255. :param canvas_width: the width of the canvas :type canvas_width: int :param x_position: x position of turtle on the canvas :type x_position: int :return: number containing the level of red between 0 and 255 :rtype: int Examples: >>> red_value(0, 500) 127 >>> red_value(-250, 500) 0 >>> red_value(250, 500) 255 """ red_val: int = 0 # ADD YOUR CODE HERE return red_val def blue_value(y_position: int, canvas_height: int) -> int: """ Calculates the blue value based on how far the turtle is from the bottom of the canvas. The blue value increases the closer to the top that the turtle moves. At the top of the canvas the blue value will be 255. At the bottom of the canvas the blue value will be 0. :param y_position: y position of turtle :type y_position: int :param canvas_height: height of the turtle canvas :type canvas_height: int :return: The level of blue between 0 and 255 :rtype: int Examples: >>> blue_value(0, 500) 127 >>> blue_value(250, 500) 255 >>> blue_value(-250, 500) 0 """ blue: int = 0 return blue def new_color(x_position: int, y_position: int, canvas_width: int, canvas_height: int, ) -> tuple: """ Return a new color based on the position of the turtle on the canvas. :param x_position: the x coordinate of the turtle on the canvas :type x_position: int :param y_position: the y coordinate of the turtle on the canvas :type y_position: int :param canvas_width: :type canvas_width int :param canvas_height: :type canvas_height int :return: r, g, b values :rtype: tuple """ red: int = 0 green: int = 0 blue: int = 0 # ADD YOUR CODE HERE return red, green, blue def clamp(value: int, range_start: int, range_end: int) -> int: """ Ensure that value falls within the given range. If it does not then return either the minimum or the maximum value in the range. If ```range_end``` is less than ```range_start``` then reverse the range values and print a warning. :param value: value to be checked against range :type value: int :param range_start: start of range :type value: int :param range_end: end of range :type value: int :return: a value within the range given :rtype: int Examples: >>> clamp(12, 0, 255) 12 >>> clamp(-12, 0, 255) 0 >>> clamp(300, 0, 255) 255 >>> clamp(-12, 255, 0) 0 """ value: int = 0 # ADD YOUR CODE HERE return value def distance_dragged(start_x: int, start_y: int, end_x: int, end_y: int) -> float: """ Calculate the distance the between two points. Hint: use math.hypot(). :param start_x: start x coordinate :type: start_x: int :param start_y: start y coordinate :type: start_y: int :param end_x: end x coordinate :type: end_x: int :param end_y: end y coordinate :type: end_y: int :return: float Examples: >>> distance_dragged(0, 0, 100, 100) 141.4213562373095 >>> distance_dragged(-250, -250, 250, 250) 707.1067811865476 """ distance: float = 0.0 # ADD YOUR CODE HERE return distance def new_pensize(drag_distance: int, canvas_width: int, canvas_height: int, min_pen_size: int, max_pen_size: int): """ Calculate a new pen size (thickness) based on the distance that the turtle has been dragged. The greater the drag length, the thicker the pen size. Hint: calculate the maximum drag distance which is the diagonal length of the canvas. Use the remap() function to translate the drag distance to a value between the minimum and maximum pen size. :param drag_distance: distance mouse dragged :type drag_distance: int :param canvas_width: the canvas width :type canvas_width: int :param canvas_height: height of canvas :type canvas_height: int :param min_pen_size: the minimum pen size in range :type min_pen_size: int :param max_pen_size: the maximum pen size in range :type max_pen_size: int :return: new pen size based on the distance dragged :rtype: int Examples: >>> new_pensize(0, 500, 500, 1, 10) 1 >>> new_pensize(350, 500, 500, 1, 10) 5 >>> new_pensize(700, 500, 500, 1, 10) 10 >>> new_pensize(1000, 500, 500, 1, 10) 10 >>> new_pensize(-7, 500, 500, 1, 10) 1 """ pen_width: int = 0 return pen_width def remap(value: float, old_min: float, old_max: float, new_min: float, new_max: float) -> float: """ Translate a value within a specified range to a value in a target range. :param value: value to be mapped into target range :type value: float :param old_min: the start value of the initial range :type old_min: float :param old_max: the end value of the initial range :type old_max: float :param new_min: the start value of the target range :type new_min: float :param new_max: the end value of the target range :type new_max: float :return: the adjusted value in the new range :rtype: float Examples: >>> remap(1, 1, 5, 0, 255) 0.0 >>> remap(5, 1, 10, 1, 100) 45.0 >>> remap(10, 1, 10, 1, 100) 100.0 """ result: float = 0.0 # ADD YOUR CODE HERE return result def line_drag_handler(x_position: int, y_position: int): """ Function called when the turtle is dragged.