# would like to draw a function in python turtle in cartesian plane

Page 1 of 1

## 7 Replies - 418 Views - Last Post: 18 October 2019 - 02:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417584&amp;s=b57d1bb541b05a68102a1ba2162bffb4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 haris.bashir501

Reputation: 0
• Posts: 4
• Joined: 16-October 19

# would like to draw a function in python turtle in cartesian plane

Posted 16 October 2019 - 09:28 PM

i have the main function for my code built but there are incomplete smaller functions that i do not understand how to do. here is the code so far:

```from math import *
import turtle

# CONSTANTS
WIDTH = 800
HEIGHT = 600
XORIGIN = 400
YORIGIN = 300
AXISCOLOR = "black"

#
#  Returns the screen (pixel based) coordinates of some (x, y) graph location base on configuration
#
#  Parameters:
#   xo, yo : the pixel location of the origin of the  graph
#   ratio: the ratio of pixels to single step in graph (i.e 1 step is ratio amount of pixels)
#   x, y: the graph location to change into a screen (pixel-based) location
#
#  Usage -> screenCoor(xo, yo, ratio, 1, 0)
#
#  Returns: (screenX, screenY) which is the graph location (x,y) as a pixel location in the window
#
def screenCoor(xo, yo, ratio, x, y):
return 0, 0

#
#  Returns a string of the colour to use for the current expression being drawn
#  This colour is chosen based on which how many expression have previously been drawn
#  The counter starts at 0, the first or 0th expression, should be red, the second green, the third blue
#  then loops back to red, then green, then blue, again
#
#  Usage -> getColor(counter)
#
#  Parameters:
#  counter: an integer where the value is a count (starting at 0) of the expressions drawn
#
#  Returns: 0 -> "red", 1 -> "green", 2 -> "blue", 3 -> "red", 4 -> "green", etc.
#
def getColor(counter):
return "black"

#
#  Draw in the window an xaxis label (text) for a point at (screenX, screenY)
#  the actual drawing points will be offset from this location as necessary
#  Ex. for (x,y) = (1,0) or x-axis tick/label spot 1, draw a tick mark and the label 1
#
#  Usage -> drawXAxisLabelTick(pointer, 1, 0, "1")
#
#  Parameters:
#  pointer: the turtle drawing object
#  screenX, screenY): the pixel screen location to drawn the label and tick mark for
#  text: the text of the label to draw
#
#  Returns: Nothing
#
def drawXAxisLabelTick(pointer, screenX, screenY, text):
pointer.up()
pointer.color(AXISCOLOR)
screenX = xo + XORIGIN * ratio
screenY = yo + YORIGIN * ratio
pointer.goto(screenX,screenY)
pointer.down()
pass

#
#  Draw in the window an yaxis label (text) for a point at (screenX, screenY)
#  the actual drawing points will be offset from this location as necessary
#  Ex. for (x,y) = (0,1) or y-axis tick/label spot 1, draw a tick mark and the label 1
#
#  Usage -> drawXAxisLabelTick(pointer, 0, 1, "1")
#
#  Parameters:
#  pointer: the turtle drawing object
#  screenX, screenY): the pixel screen location to drawn the label and tick mark for
#  text: the text of the label to draw
#
#  Returns: Nothing
#
def drawYAxisLabelTick(pointer, screenX, screenY, text):

pass

#
#  Draw in the window an xaxis (secondary function is to return the minimum and maximum graph locations drawn at)
#
#  Usage -> drawXAxis(pointer, xo, yo, ratio)
#
#  Parameters:
#  pointer: the turtle drawing object
#  xo, yo : the pixel location of the origin of the  graph
#  ratio: the ratio of pixels to single step in graph (i.e 1 step is ratio amount of pixels)
#
#  Returns: (xmin, ymin) where xmin is minimum x location drawn at and xmax is maximum x location drawn at
#
def drawXAxis(pointer, xo, yo, ratio):
xmin = 0
xmax = 0
return xmin, xmax

#
#  Draw in the window an yaxis
#
#  Usage -> drawYAxis(pointer, xo, yo, ratio)
#
#  Parameters:
#  pointer: the turtle drawing object
#  xo, yo : the pixel location of the origin of the  graph
#  ratio: the ratio of pixels to single step in graph (i.e 1 step is ratio amount of pixels)
#
#  Returns: Nothing
#
def drawYAxis(pointer, xo, yo, ratio):
pass

#
#  Draw in the window the given expression (expr) between [xmin, xmax] graph locations
#
#  Usage -> drawExpr(pointer, xo, yo, ratio, xmin, xmax, expr)
#
#  Parameters:
#  pointer: the turtle drawing object
#  xo, yo : the pixel location of the origin of the  graph
#  ratio: the ratio of pixels to single step in graph (i.e 1 step is ratio amount of pixels)
#  expr: the expression to draw (assumed to be valid)
#  xmin, ymin : the range for which to draw the expression [xmin, xmax]
#
#  Returns: Nothing
#
def drawExpr(pointer, xo, yo, ratio, xmin, xmax, expr):
expression =
#Draw expression
pass

#
#  Setup of turtle screen before we draw
#  DO NOT CHANGE THIS FUNCTION
#
#  Returns: Nothing
#
def setup():
pointer = turtle.Turtle()
screen = turtle.getscreen()
screen.setup(WIDTH, HEIGHT, 0, 0)
screen.setworldcoordinates(0, 0, WIDTH, HEIGHT)
pointer.hideturtle()
screen.delay(delay=0)
return pointer

#
#  Main function that attempts to graph a number of expressions entered by the user
#  The user is also able to designate the origin of the chart to be drawn, as well as the ratio of pixels to steps (shared by both x and y axes)
#  The window size is always 800 width by 600 height in pixels
#  DO NOT CHANGE THIS FUNCTION
#
#  Returns: Nothing
#
def main():
#Setup window
pointer = setup()

#Get input from user
xo, yo = eval(input("Enter pixel coordinates of origin: "))
ratio = int(input("Enter ratio of pixels per step: "))

#Set color and draw axes (store discovered visible xmin/xmax to use in drawing expressions)
pointer.color(AXISCOLOR)
xmin, xmax = drawXAxis(pointer, xo, yo, ratio)
drawYAxis(pointer, xo, yo, ratio)

#Loop and draw experssions until empty string "" is entered, change expression colour based on how many expressions have been drawn
expr = input("Enter an arithmetic expression: ")
counter = 0
while expr != "":
pointer.color(getColor(counter))
drawExpr(pointer, xo, yo, ratio, xmin, xmax, expr)
expr = input("Enter an arithmetic expression: ")
counter += 1

#Run the program
main()
```

This post has been edited by DK3250: 17 October 2019 - 12:24 AM
Reason for edit:: Code tags added

Is This A Good Question/Topic? 0

## Replies To: would like to draw a function in python turtle in cartesian plane

### #2 DK3250

• Pythonian

Reputation: 561
• Posts: 1,791
• Joined: 27-December 13

## Re: would like to draw a function in python turtle in cartesian plane

Posted 17 October 2019 - 12:31 AM

First of all:
Welcome to Dream.In.Code, - I hope you will find it helpful.

When uploading code, please use code tags - when the input window is active, you will find a [CODE] icon just above the input field. I have added code tags in your post above.

What is it that you don't understand?
Or what did you expect the code to do and what happens actually.

### #3 haris.bashir501

Reputation: 0
• Posts: 4
• Joined: 16-October 19

## Re: would like to draw a function in python turtle in cartesian plane

Posted 18 October 2019 - 12:11 PM

DK3250, on 17 October 2019 - 12:31 AM, said:

First of all:
Welcome to Dream.In.Code, - I hope you will find it helpful.

When uploading code, please use code tags - when the input window is active, you will find a [CODE] icon just above the input field. I have added code tags in your post above.

What is it that you don't understand?
Or what did you expect the code to do and what happens actually.

i would like to know what to include in the incomplete functions above the setup functions in order to complete the program. i am having trouble drawing the x and y axis with the pixel cooridnates given.

### #4 DK3250

• Pythonian

Reputation: 561
• Posts: 1,791
• Joined: 27-December 13

## Re: would like to draw a function in python turtle in cartesian plane

Posted 18 October 2019 - 02:11 PM

haris.bashir501, on 18 October 2019 - 08:11 PM, said:

i am having trouble drawing the x and y axis with the pixel cooridnates given.
Well, you don't even try!
All you show is empty functions.
You need to show a decent attempt before requesting help.
And if you cannot draw two straight lines, then your ambitions with this project are too high.

### #5 haris.bashir501

Reputation: 0
• Posts: 4
• Joined: 16-October 19

## Re: would like to draw a function in python turtle in cartesian plane

Posted 18 October 2019 - 02:14 PM

I have researched a lot and I have no clue but thanks for your reply. I will wait for someone else's reply.

### #6 DK3250

• Pythonian

Reputation: 561
• Posts: 1,791
• Joined: 27-December 13

## Re: would like to draw a function in python turtle in cartesian plane

Posted 18 October 2019 - 02:26 PM

You are essentially asking: "Give me the code".
We don't do this.
You have to show an effort. Then we will be glad to help.

Quote

I have researched a lot...

Start by making a horizontal and a vertical line in the middle of the turtle screen.
This only requires quite basic research...

### #7 haris.bashir501

Reputation: 0
• Posts: 4
• Joined: 16-October 19

## Re: would like to draw a function in python turtle in cartesian plane

Posted 18 October 2019 - 02:34 PM

hi sorry if i was a bit rude. the problem is that this assignment is due at midnight today but its for minimal marks only. i was asking for help since i had two midterms today. have one in three hours. i know if you would provide the code that will be cheating but i dont intend to submit the code as given here. even if i wanted to i cant since my instructor has very strict policies regarding that matter. i had missed a lot of lectures and couldnt do my first assingment which involved only drawing the x and y axis and i asked the instructor to provide me the key to the assingment to whcih he said no and therefore this assingments basics is to build the axis and then to draw the function. i dont even know the basics so how can i possibly tackle this assingment. i have researched google and nothing comes up to help with the code. so i was hoping only if someone can help me with this. this will also guide me with my comp sci midterm which is next monday

### #8 DK3250

• Pythonian

Reputation: 561
• Posts: 1,791
• Joined: 27-December 13

## Re: would like to draw a function in python turtle in cartesian plane

Posted 18 October 2019 - 02:46 PM