如何用python完成:用自顶向下设计方法编写程序:在屏幕上打印三角函数y = sin(x)的图像.

1个回答

  • I wrote this in Tkinter for you,in case you don't know Tkinter,it is a built-in module for most python versions.

    If you want a commandline version,you can ask me,but tell you what,since those values are all

    float numbers,so it's hard to get a precise graph in commandline window.

    Well,in this version,I enlarged each element's position by 40 and then change them to integer,guess this is an endurable loss of precision.

    #

    from math import radians

    from math import sin

    from Tkinter import *

    pos = []

    xPos = 0

    centerX = 0

    centerY = 0

    for deg in range(-360, 361, 10):

    x09pos.append([xPos, int(40*(sin(radians(deg))))]) #1000 too big for my screen

    x09xPos+=1

    x09if deg == 0:

    x09x09centerX = xPos-1

    x09x09centerY = pos[-1][1]

    root = Tk()

    root.title('trianble graph from -180 to 180')

    width, height = 550, 450

    mHei = height/2

    mWid = width/2

    canvas = Canvas(root, width=width, height=height)

    canvas.create_line(0, mHei, width, mHei)#x axis

    canvas.create_line(mWid, 0, mWid, height)#y axis

    xStep = (width-150)/len(pos)

    yStep = (height-150)/len(pos)

    radius = 3

    # the middle point (sin(0) is first drawn and used as position reference for all

    canvas.create_oval(mWid-radius, mHei-radius, mWid+radius, mHei+radius, fill='green')

    print pos

    print xStep, yStep, centerX, centerY

    #exit(0)

    for i in pos:

    x09if i[0] == centerX: #center processed already.

    x09x09continue

    x09x = mWid + xStep*(i[0]-centerX)

    x09# y is smaller, the bigger the value, so use minus

    x09y = mHei - yStep*(i[1]-centerY)

    x09canvas.create_oval(x-radius, y-radius, x+radius, y+radius, fill='green')

    x09

    canvas.pack()

    root.mainloop()

    x09

    x09

    x09