fx_2.0 python一/二次函數(shù)繪圖
在學python
畫圖好難
#2.0 !!!
#介紹
print('\n\n\n\nfx_2.0可以繪制二次函數(shù)圖像和一次函數(shù)圖像',end='')
print('''
2.0更新內容:
1 圖像由1.0版本旋轉90°與通常的坐標軸方向一致
2 改善了縮放機制 相對河里
3 改進輸入 可以防止輸入錯誤值如字符導致的異常
4 增加說明、注釋
5 解決了換行問題 而且圖像可以很大了
''',end='')
#輸入
def fxin(n):
? ? str(n)
? ? while True:
? ? ? ? try :
? ? ? ? ? ? a = float(input('%s=' % n))
? ? ? ? ? ? break
? ? ? ? except ValueError:
? ? ? ? ? ? print('請輸入數(shù)字')
? ? ? ?
? ? return(a)
while True:
? ? a,b,c,x_min,x_max,scale = fxin('a'),fxin('b'),fxin('c'),fxin('x min'),fxin('x max'),fxin('圖像縮放')
? ? #輸入內容
? ? print('\nf(x)=',end='')
? ? if a == 1:
? ? ? ? ? ? print('x2',end='')
? ? elif a == -1:
? ? ? ? ? ? print('-x2',end='')
? ? elif a != 0:
? ? ? ? ? ? print('%fx2' % a,end='')
? ? if b == 1 and a == 0:
? ? ? ? ? ? print('x',end='')
? ? elif b == 1 and a != 0:
? ? ? ? ? ? print('+x',end='')
? ? elif b == -1:
? ? ? ? ? ? print('-x',end='')
? ? elif b != 0:
? ? ? ? ? ? if b > 0 and a != 0:
? ? ? ? ? ? ? ? print('+%fx' % b,end='')
? ? ? ? ? ? if b > 0 and a == 0:
? ? ? ? ? ? ? ? print('%fx' % b,end='')
? ? ? ? ? ? if b <= 0:
? ? ? ? ? ? ? ? print('%fx' % b,end='')
? ? if a == 0 and b == 0:
? ? ? ? ? ? print(c)
? ? elif a != 0 or b != 0:
? ? ? ? ? ? if c > 0:
? ? ? ? ? ? ? ? print('+%f' % c)
? ? ? ? ? ? if c < 0:
? ? ? ? ? ? ? ? print(c)
? ? ? ? ? ? if c == 0:
? ? ? ? ? ? ? ? print('')
? ? #確認
? ? while True:
? ? ? ? yrq = input('輸入y確認\n輸入r重新輸入\n輸入q退出\n')
? ? ? ? if yrq == 'y' or yrq == 'r':
? ? ? ? ? ? break
? ? ? ? elif yrq == 'q':
? ? ? ? ? ? exit()
? ? ? ? else:
? ? ? ? ? ? print('\n僅限輸入 y 或 r 或 q')
? ? if yrq == 'y':
? ? ? ? ?del yrq
? ? ? ? ?break ?
#計算函數(shù)值
def f_x(x_min,x_max,a,b,c,scale):
? ? '''計算函數(shù)值 列表A=B A排序B也會跟著 沒辦法 就干脆定義一個函數(shù)罷:('''
? ? fx = []
? ? while x_min <= x_max :
? ? ? ? fx.append(int(round((a*x_min**2+b*x_min+c)*scale,0)))
? ? ? ? x_min+=(1/scale)
? ? return fx
fx = f_x(x_min,x_max,a,b,c,scale) ?#我愛咋咋
fx_sequence = set(f_x(x_min,x_max,a,b,c,scale))
fx_sequence = list(fx_sequence)
fx_sequence.sort(reverse=True)
#繪圖
for e_fxs in fx_sequence:
? ? count = fx.count(e_fxs)
? ? count2,plen,pplen = 0,0,0
? ? while count2 < count:
? ? ? ? #print(pplen,' ? ',len(fx)-1,' ? ',fx)
? ? ? ? plen = fx.index(e_fxs,pplen,len(fx))
? ? ? ? indicator = fx[plen]
? ? ? ? print(' '*(plen-pplen), end='')
? ? ? ? print('*', end='')
? ? ? ? pplen = plen+1
? ? ? ? count2+=1
? ? if fx_sequence.index(e_fxs) < (len(fx_sequence)-1):
? ? ? ? print('\n'* (e_fxs - fx_sequence[fx_sequence.index(e_fxs)+1]),end="")