# File: tcurve.py
# program to plot a curve (here, x**2), connecting dots with lines
#
# Matt Bishop, MHI 289I, Fall 2022
#
import turtle # allows us to use turtles
#
# these control the plotting and start of the function
#
xscale = 1 # need to scale to make the plot visible
xstart = -13 # x co-ordinate of lower point of the curve
xend = 13 # x co-ordinate of upper point of the curve
ytop = 170 # top of y axis; at least max(xstart**2, xend**2)
npoints = 500 # how many points to use tio draw the curve
#
# set up the drawing
#
# window stuff first
wn = turtle.Screen() # draw the window for the picture
wn.bgcolor("blue") # make the background color blue
wn.title("Graph of x * x") # put up a title
# this is tricky. If we leave the co-ordinate (0,0) in the center
# of the window, then we're losing the lower two quadrants as
# y can never be negative. So we adjust the co-ordinates of the
# window, so the lower left corner is (-25, -25) -- why we leave
# 25 units at the bottom is to let us print something -- and the
# upper right corner is (25, ytop + 25) -- again, some room so
# we can print something
wn.setworldcoordinates(xstart - 3, -25, xend + 3, ytop + 25)
# turtle (or pen) stuff next
kelly = turtle.Turtle() # create a turtle, called "kelly"
kelly.shape("classic") # just an arrow
kelly.color("white") # white contrasts nicely with red
kelly.hideturtle()
#
# draw the axes
#
# first, the x axis
kelly.penup()
kelly.setpos((xend + 1) * xscale, 0)
kelly.pendown()
kelly.goto((xstart - 1) * xscale, 0)
# now the y axis
kelly.penup()
kelly.setpos(0 * xscale, ytop)
kelly.pendown()
kelly.goto(0 * xscale, 0)
kelly.penup()
#
# mark the axes
#
# first the x axis, from xstart to xend
for i in range(xstart, xend + 1):
kelly.goto(i * xscale, -1)
kelly.pendown()
kelly.goto(i * xscale, 1)
kelly.penup()
if i % 5 == 0:
kelly.goto(i * xscale, -5)
kelly.write(str(i), False, "center", ("Arial", 12, "normal"))
#
# next the y axis, from 0 to ytop
# positioning the text beside the line is trial and error
for i in range(0, ytop + 1):
if i % 10 == 0 and i != 0:
kelly.goto(-0.5, i)
kelly.pendown()
kelly.goto(0.5, i)
kelly.penup()
kelly.goto(-0.7, i-1.5)
kelly.write(str(i), False, "right", ("Arial", 12, "normal"))
#
# now write f(x) above the y axis
kelly.penup()
kelly.goto(0 * xscale, ytop + 5)
kelly.write("f(x)", False, "center", ("Arial", 24, "normal"))
#
# now write what we are graphing below the x axis
kelly.goto(0 * xscale, -15)
s = "Graph of f(x) = x * x on [" + str(xstart) + "," + str(xend) + "]"
kelly.write(s, False, "center", ("Arial", 24, "normal"))
#
# figure out where the npoints will go
# they are equally spaced between xstart and xend
# so incr is the increment to x
#
incr = (xend - xstart) / npoints
#
# move to the first point
#
x = xstart
kelly.setpos(x * xscale, x * x)
# start the line there
kelly.pendown()
# now loop; put down a new point and draw a line from
# the previous point to it during each iteration
for _ in range(0, npoints):
# compute the next x value
x = x + incr
# move to the position; this draws the line (pen is down)
kelly.goto(x * xscale, x * x)
#
# now hide the turtle and just sit there, displaying the result
#
kelly.hideturtle() # make the turtle invisible
wn.mainloop() # wait until the user closes the window