Programme Python
Librairie graphique python en (pur) python
- Fichier
- Type: Programme python
- File type: py (python)
- Télécharger:
- Description
- Librairie graphique python en (pur) python
- Mots clé
- python, Libxy, librairie graphique, TICE
- Voir aussi:
#-*-coding:utf-8;-*-
# Libxy without PIL or Pillow,
# writing directly to ppm image format
# (thanks to
# https://solarianprogrammer.com/2017/10/25/ppm-image-python-3/ )
#
__version__="1.1-ppm"
__author__="Y. Morel"
__about__="Libxy - ppm image format output version - Library for graphical functions and tools with pure python - written by Y. Morel, detailled infos on http://xymaths.free.fr/Libxy "
import sys, os.path
import array
def InitGraph (Xmin=-10,Xmax=10,Ymin=-10,Ymax=10,**kwargs):
global Width, Height, image
global xmin,xmax,ymin,ymax
global ptsz,lw
ptsz=kwargs.get('PointSize',1)
lw=kwargs.get('LineWidth',1)
Width=kwargs.get('Width',500)
Height=kwargs.get('Height',500)
xmin,xmax,ymin,ymax=float(Xmin),float(Xmax),float(Ymin),float(Ymax)
image = array.array('B', [255, 255, 255] * Width * Height)
def SaveGraph (Name="Picture"):
path=os.path.abspath(os.path.dirname(__file__))
fullPicName=path+'/'+Name+'.ppm'
#PPM Header
ppm_header = 'P6 ' + str(Width) + ' ' + str(Height) + ' ' + str(255) + '\n'
#ppm_header = f'P6 {width} {height} {maxval}\n'
with open(fullPicName, 'wb') as f:
f.write(bytearray(ppm_header, 'ascii'))
image.tofile(f)
print("Image file "+str(Name)+".ppm written in "+str(path)+"\n")
def Point (*args,**kwargs):
sz=int (kwargs.get ('size',ptsz))
fill=kwargs.get ('fill',"red")
if (len (args)==1):
A=(args [0][0],args [0][1])
elif (len (args)==2):
A=(args[0],args[1])
if sz<=1:
(x,y)=coordim (*A)
x=int(x);y=int(y)
index = int(3 * (y * Width + x))
#print("point=",x,y,index)
image[index]=255
image[index+1]=0
image[index+2]=0
#dr.point (coordim (*A),fill=fill)
else:
(x,y)=coordim (*A)
index = 3 * (y * Width + x)
for i in range (-int(sz/2),(sz/2)):
for j in range (-(sz/2),(sz/2)):
image[index]=255
image[index+1]=0
image[index+2]=0
# Change the (x, y) pixel color to red
#x=100;y=200
#index = 3 * (y * width + x)
#image[index] = 255 # red channel
#image[index + 1] = 0 # green channel
#image[index + 2] = 0 # blue channel
def Line (A,B,**kwargs):
fill=kwargs.get ('fill',"red")
width=kwargs.get ('width',lw)
#dr.line ([coordim (*A),coordim (*B)],fill=fill,width=width)
N=500
if not (B[0]==A[0]):
m=(B[1]-A[1])/(B[0]-A[0])
p=B[1]-m*B[0]
dx=(B[0]-A[0])/(N*1.0)
for i in range(N):
x=A[0]+i*dx
y=m*x+p
M=(x,y)
Point(M)
else:
dy=(B[1]-A[1])/(N*1.0)
for i in range(N):
x=A[0]
y=A[1]+i*dy
M=(x,y)
Point(M)
def coord (X,Y):
x=X*(xmax-xmin)/Width+xmin
y=(Height-Y)*(ymax-ymin)/Height+ymin
return x,y
def coordim (x,y):
X=float (0.99*Width*(x-xmin)/(xmax-xmin))
Y=float (0.99*(Height-Height*(y-ymin)/(ymax-ymin)))
return X,Y
def Axes (xtick=0,ytick=0):
if xtick<=0: xtick=(xmax-xmin)/10
for i in range(int (xmin/xtick),int(xmax/xtick)+1):
x=i*xtick
y=(ymax-ymin)/200.0
#print(x,y)
Line ((x,y), (x,-y),fill="blue")
#Text ((x,-2*y),str (i*xtick),fill="blue")
if ytick<=0: ytick= (ymax-ymin)/10
for i in range(int (ymin/ytick),int(ymax/ytick)+1):
y=i*ytick
x=(xmax-xmin)/200.0
#print(x,y)
Line ((x,y), (-x,y),fill="blue")
#Text ((-4*x,y),str (i*ytick),fill="blue")
Line ((xmin,0), (xmax,0),fill="blue",width=2)
Line ((0,ymin), (0,ymax),fill="blue",width=2)
global XtickAxes, YtickAxes
XtickAxes,YtickAxes=xtick,ytick
def Grid (Dx=0,Dy=0):
if not Dx>0:
try:
Dx=XtickAxes
except NameError:
Dx=(xmax-xmin)/10
if not Dy>0:
try:
Dy=YtickAxes
except NameError:
Dy=(ymax-ymin)/10
for i in range(int (xmin/Dx),int(xmax/Dx)+1):
x=i*Dx
for j in range (0,100):
y=ymin+j*(ymax-ymin)/100
Point ((x,y),size=1,fill="orange")
for i in range(int (ymin/Dy),int(ymax/Dy)+1):
y=i*Dy
for j in range (0,100):
x=xmin+j*(xmax-xmin)/100
Point ((x,y),size=1,fill="orange")
if __name__ == '__main__':
print("\nLibxy, version : "
+str (__version__)
+str(__about__)+
"""\n
Usage:
form Libxyppm import *
InitGraph()
#
#Graphical and python programming instructions
#
SaveGraph()
See xymaths.free.fr/Libxy for detailled informations
""")
#from Libxyppm import *
InitGraph()
A=(-3,-3);B=(7,7)
#Line(A,B)
Axes(),Grid()
#Point(0.1,-10)
#Line((0.1,-10),(-0.1,-10))
def f(x):
return x**3+3*x**2
n=5000
for i in range (n):
x=-10+i*20.0/n;y=f(x)
if (abs(y)<10):
M=(x,y)
Point (M)
SaveGraph()