CPSC 231: Introduction to Computer Science for Computer Science Majors I Assignment 2: Graphing Calculator Weight: 7% Collaboration Discussing the assignment requirements with others is a reasonable...

1 answer below »
help


CPSC 231: Introduction to Computer Science for Computer Science Majors I Assignment 2: Graphing Calculator Weight: 7% Collaboration Discussing the assignment requirements with others is a reasonable thing to do, and an excellent way to learn. However, the work you hand-in must ultimately be your work. This is essential for you to benefit from the learning experience, and for the instructors and TAs to grade you fairly. Handing in work that is not your original work, but is represented as such, is plagiarism and academic misconduct. Penalties for academic misconduct are outlined in the university calendar. Here are some tips to avoid plagiarism in your programming assignments. 1. Cite all sources of code that you hand-in that are not your original work. You can put the citation into comments in your program. For example, if you find and use code found on a web site, include a comment that says, for example: # the following code is from https://www.quackit.com/python/tutorial/python_hello_world.cfm. Use the complete URL so that the marker can check the source. 2. Citing sources avoids accusations of plagiarism and penalties for academic misconduct. However, you may still get a low grade if you submit code that is not primarily developed by yourself. Cited material should never be used to complete core assignment specifications. You can and should verify and code you are concerned with your instructor/TA before submit. 3. Discuss and share ideas with other programmers as much as you like, but make sure that when you write your code that it is your own. A good rule of thumb is to wait 20 minutes after talking with somebody before writing your code. If you exchange code with another student, write code while discussing it with a fellow student, or copy code from another person’s screen, then this code is not yours. 4. Collaborative coding is strictly prohibited. Your assignment submission must be strictly your code. Discussing anything beyond assignment requirements and ideas is a strictly forbidden form of collaboration. This includes sharing code, discussing code itself, or modelling code after another student's algorithm. You can not use (even with citation) another student’s code. 5. Making your code available, even passively, for others to copy, or potentially copy, is also plagiarism. 6. We will be looking for plagiarism in all code submissions, possibly using automated software designed for the task. For example, see Measures of Software Similarity (MOSS - https://theory.stanford.edu/~aiken/moss/). 7. Remember, if you are having trouble with an assignment, it is always better to go to your TA and/or instructor to get help than it is to plagiarize. A common penalty is an F on a plagiarized assignment. Late Penalty Late assignments will not be accepted. Goal Writing a program with loops and functions Technology Python 3 Submission Instructions You must submit your assignment electronically. Use the Assignment 2 dropbox in D2L for the electronic submission. You can submit multiple times over the top of a previous submission. Do not wait until the last minute to attempt to submit. You are responsible if you attempt this and time runs out. Your assignment must be completed in Python 3 and be executable with Python version 3.6.8+. For graphical drawing you must use the Python turtle library. Description Charting Expressions (Graphing Calculator) You will be creating a small graphical Python 3 program. This program will use the turtle library to draw based on information taken from the user. You should already have all the experience you need with this library from Assignment 1. The assignment requires an additional understanding of converting between Cartesian coordinate systems, using loops, and completing functions as outlined by someone else. You will be provided with a starter code that already implements the basic interaction with the user (something you learned in the previous assignment). This code consists of a completed main function, which sets up the window using a setup function. There is also a calc expression that takes a string of an expression (Ex. expr=“x**2”) and returns the y for a given x value (Ex. expr=“x**2” for x=3 will return 9) None of these three functions should be changed. The main function makes use of several incomplete functions. Your assignment is to complete these functions so that the main function can use them to draw what the user requests. All your code should be written within the incomplete functions. You are allowed to create constants and new functions as long as they are not duplications of existing ones. There should be no global variables in your code (except if you are doing the bonus). There is a bonus that involves identify local minima/maxima and labelling them described at the end of the assignment. Your program will be drawing in an 800-pixel by 600-pixel window with (0,0) as the bottom left corner and (800,600) as the top right corner. The starter code has a setup() function, which sets up the window and returns the turtle drawing object pointer so you can use it. This pointer is passed into the existing starter code functions for you to use. The included starter code already prompts the user for the pixel coordinates for where the chart origin should be placed in the window. The code then prompts the user for a ratio of how many pixels are 1 step of the chart. For example, an origin of (????, ????) = (400, 300) should be the centre of the screen. If the ratio of pixels per single step is 100 pixels per step. Then a position one step up from the origin point to (??,??) = (0, 1) in the charting coordinate system is (???? + ?????????? ∗ ??, ???? + ?????????? ∗ ??) = (400 + 100 ∗ 0, 300 + 100 ∗ 1) = (400, 300 + 100) = (400, 400). Below is an image of 3 curves drawn for a chart with an origin at 400,300 and a ratio of 30. The existing starter code uses the included (but incomplete) functions to draw the x and y axes in black, and then loops to get expressions from the user. The colour used to draw an expression is determined based on the number of previously drawn expressions. So this number is tracked using a counter variable, that increases by 1 for each new expression entered. Your task is to complete the included functions, so they function in the way that the comments and assignment description require. You will likely find the visual examples of operation included in the assignment description helpful. In the following diagram. Green functions are complete and provided. Order of parts is Part 2 Blue – calc_to_screen_coord, calc_minmax_x, calc_minmax_y Part 3 Grey – draw_x_axis, draw_y_axis, draw_line, draw_x_axis_label, draw_x_axis_tick, draw_y_axis_label, draw_y_axis_tick Part 4 Yellow – draw_expression I recommend approaching the functions in stages: Part 1: get_color: First, complete the colour-determination function get_color. This function takes the counter tracked in the expression input loop and returns one of three different colours: red, green, or blue. You should find the remainder division operator (also called modulus) helpful here because it allows you to change an infinitely increasing integer sequence into a range of integers. Part 2: calc_to_screen_coord, calc_minmax_x, calc_minmax_y: There are 3 sub-functions to this part. Each does not take a lot of code, but needs to be mathematically sound. First, complete the coordinate conversion function calc_to_screen_coord. This function uses the parameters that the main function obtains user defined pixel coordinate system (x_origin, y_origin, ratio), along with a calculator coordinate system (??,??) to perform the conversion. The (x_origin, y_origin) is the pixel coordinates of where (0, 0) in the calculator should be located on screen, and ratio is how many pixels are equal to 1 in the calculator coordinate system. calc_to_screen_coord returns the (x, y) coordinate as a pixel screen coordinate (screen_x, screen_y) following the math described previously in the example. Use this function in your code any time you want to convert and draw a calculator location. You will lose marks if you duplicate the math/code functionality of this function somewhere else instead of using this function designed for that purpose. The following image diagrams another example. In it an 800 by 600 window with a 400,300 origin and a ratio of 100. This would mean a (1,1) coordinate system point would fall at (500,400). Now complete the other two functions. These functions are utility functions useful later in your code to determine what coordinates in each axis to draw values in between. To simplify things we will be deciding what axis value is just off the visible screen on each axis. calc_minmax_x is for the x-axis, calc_minmax_y is for the y-axis. For the x-axis we want to know the calculator location of x=0 and x=WIDTH as these are the smallest and largest pixel locations we will be drawing. We can invert the math of calculating a pixel screen coordinate to determine what calculator x values match each of these. Then we will take the floor of 0-conversion value to be the min, and the ceiling of the WIDTH-conversion to be the max. We will do the same for the y-axis function, but for y=0 and y=HEIGHT. Ex. ??_???????????? = 400, ??_???????????? = 300, ?????????? = 100 ?????????????? = ?????????????? + ?? ∗ ?????????? for ????????????_?? = 0 0 = 400 + ?? ∗ 100 −400/100 = ?? ?? = −4.0 ???????? = ??????(??????????(−4.0)) = −4 Part 3: draw_line, draw_x_axis, draw_y_axis (tick/label helpers) Third, complete the functions that draw the x and y axes. These two functions are structurally similar, so complete one of them first then you can copy and modify it to complete the other function. When first drawing your axes, you can ignore the tick marks and labels. These can be added in once you have the lines drawn successfully. Use your part 2 functions to determine the min/max calculator locations for the x-axis/y-axis. Then draw a line between those two locations. To draw this line complete and
Answered 1 days AfterOct 21, 2021

Answer To: CPSC 231: Introduction to Computer Science for Computer Science Majors I Assignment 2: Graphing...

Sathishkumar answered on Oct 23 2021
114 Votes
# COURSE CPSC 231 FALL 2021
# INSTRUCTOR: Jonathan Hudson
# Tutorial: TA Name
# ID: XXXXXXXX
# Date:
# Description:
from math import *
import turtle
# Constants
BACKGROUND_COLOR = "white"
WIDTH = 800
HEIGHT = 600
AXIS_COLOR = "black"
def get_color(equation_counter):
"""
Get color for an equati
on based on counter of how many equations have been drawn (this is the xth equation)
:param equation_counter: Number x, for xth equation being drawn
:return: A string color for turtle to use
"""
color=['red','blue','green','yellow','orange','pink','cyan','brown','grey']
# DEFAULT return black, needs to be changed
return color[equation_counter]
def calc_to_screen_coord(x, y, x_origin, y_origin, ratio):
"""
Convert a calculator (x,y) to a pixel (screen_x, screen_y) based on origin location and ratio
:param x: Calculator x
:param y: Calculator y
:param x_origin: Pixel x origin of pixel coordinate system
:param y_origin: Pixel y origin of pixel coordinate system
:param ratio: Ratio of pixel coordinate system (each 1 in calculator is worth ratio amount of pixels)
:return: (screen_x, screen_y) pixel version of calculator (x,y)
"""
return -1, -1
def calc_minmax_x(x_origin, ratio):
"""
Calculate smallest and largest calculator INTEGER x value to draw for a 0->WIDTH of screen
Smallest: Convert a pixel x=0 to a calculator value and return integer floor
Largest : Convert a pixel x=WIDTH to a calculator value and return integer ceiling
:param x_origin: Pixel x origin of pixel coordinate system
:param ratio: Ratio of pixel coordinate system (each 1 in calculator is worth ratio amount of pixels)
:return: (Smallest, Largest) x value to draw for a 0->WIDTH of screen
"""
return -1, -1
def calc_minmax_y(y_origin, ratio):
"""
Calculate smallest and largest calculator INTEGER y value to draw for a 0->HEIGHT of screen
Smallest: Convert a pixel y=0 to a calculator value and return integer floor
Largest : Convert a pixel y=HEIGHT to a calculator value and return integer ceiling
:param y_origin: Pixel y origin of pixel coordinate system
:param ratio: Ratio of pixel coordinate system (each 1 in calculator is worth ratio amount of pixels)
:return: (Smallest, Largest) y value to draw for a 0->HEIGHT of screen
"""
return -1, -1
def draw_line(pointer, screen_x1, screen_y1, screen_x2, screen_y2):
"""
Draw a line between tow pixel coordinates (screen_x_1, screen_y_1) to (screen_x_2, screen_y_2)
:param pointer: Turtle pointer to draw with
:param screen_x1: The pixel x of line start
:param screen_y1: The pixel y of line start
:param screen_x2: The pixel x of line end
:param screen_y2: The pixel y of line end
:return: None (just draws in turtle)
"""
pass
def draw_x_axis_tick(pointer, screen_x, screen_y, label_text):
"""
Draw an x-axis tick for location (screen_x, screen_y)
:param pointer: Turtle pointer to draw with
:param screen_x: The pixel x of tick location on axis
:param screen_y: The...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here