如何用python完成:用自顶向下设计方法编写程序:在屏幕上打印三角函数y = sin(x)的图像。
发布网友
发布时间:2022-04-18 01:54
我来回答
共2个回答
热心网友
时间:2022-04-18 03:23
I wrote this in Tkinter for you, in case you don't know Tkinter, it is a built-in mole 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 enrable 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):
pos.append([xPos, int(40*(sin(radians(deg))))]) #1000 too big for my screen
xPos+=1
if deg == 0:
centerX = xPos-1
centerY = 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:
if i[0] == centerX: #center processed already.
continue
x = mWid + xStep*(i[0]-centerX)
# y is smaller, the bigger the value, so use minus
y = mHei - yStep*(i[1]-centerY)
canvas.create_oval(x-radius, y-radius, x+radius, y+radius, fill='green')
canvas.pack()
root.mainloop()
热心网友
时间:2022-04-18 04:41
#coding=utf-8
#name 格子绘图
import math
import random
def box(ox,oy,vv):
"""绘制图形 ox~格长 oy~格高
vv~坐标列表
dss~底色图案 hss绘图图案"""
dss,hss = ".","*"
ds=dss
#print vv
print "X",
for x in range(ox+1):
xs=chr(x+65)
print "%s" % xs,
print
for y in range(oy,0,-1):
print "%.2d" % y,
for j in range(ox):
for v in vv:
if j==v[0]-1 and \
y==v[1]:
ds=hss
print "%s" % ds,
ds=dss
print "%.2d" % y
print "Y",
for x in range(ox+1):
xs=chr(x+65)
print "%s" % xs,
print
def dws(l): #返回坐标列表 l~点数
vzb=[[0,0]]
for i in range(1,l+1):
x=i
y=hansu(x)
vzb.append([x,y])
return vzb
def hansu(x): #计算坐标函数 x~x值
#y=math.sin(x)
y=x
return round(y)
if __name__=="__main__":
box(20,20,dws(40))
追问
另外再问一下这种能显示行数的python是怎么搞出来的?我用的是这种