geo2d.py

Created by frattini-fabrice

Created on July 05, 2020

3.33 KB

Programme permettant de construire un repère du plan (dont on définit les bornes), des points, des polygones, des droites (à l’aide de deux points ou d’une équation cartésienne), des segments et des cercles.


from math import *
from kandinsky import *

class Rep:
  def __init__(self,xmin,xmax,ymin,ymax):
    self.xmin = xmin
    self.xmax = xmax
    self.ymin = ymin
    self.ymax = ymax
    self.xpix = (self.xmax-self.xmin)/320
    self.ypix = (self.ymax-self.ymin)/240
    
  
  def dot(self,p,c=color(0,0,0)):
    set_pixel(int(p[0]*320/(self.xmax-self.xmin)-320*self.xmin/(self.xmax-self.xmin)),240-int(p[1]*240/(self.ymax-self.ymin)-240*self.ymin/(self.ymax-self.ymin)),c)
  
  def seg(self,p,q,c=color(0,0,0)):
    if p[0]!=q[0] or p[1]!=q[1]:
      if p[0]!=q[0]:
        x=p[0]
        y=p[1]
        if p[0]<q[0]:
          s=1   
        else:
          s=-1
        
        pas=s*(self.xmax-self.xmin)/(320+fabs((q[1]-p[1])/(q[0]-p[0]))*500)
        while (x-q[0])*s<0:
          self.dot([x,y],c)
          x=x+pas
          y=(q[1]-p[1])*x/(q[0]-p[0])+p[1]-(q[1]-p[1])*p[0]/(q[0]-p[0])
        
      else:
        x=p[0]
        y=p[1]
        if p[1]<q[1]:
          s=1   
        else:
          s=-1
        
        pas=s*(self.ymax-self.ymin)/240
        while (y-q[1])*s<0:
          self.dot([x,y],c)
          y=y+pas
     
      
    
  def axex(self):
    self.seg([self.xmin,0],[self.xmax,0])
    self.seg([self.xmax,0],[self.xmax-5*(self.xmax-self.xmin)/320,5*(self.ymax-self.ymin)/240])
    self.seg([self.xmax,0],[self.xmax-5*(self.xmax-self.xmin)/320,-5*(self.ymax-self.ymin)/240])
    for i in range(self.xmin+1,self.xmax):
      self.seg([i,-2*(self.ymax-self.ymin)/240],[i,2*(self.ymax-self.ymin)/240])
    
  def axey(self):
    self.seg([0,self.ymin],[0,self.ymax])
    self.seg([0,self.ymax],[5*(self.xmax-self.xmin)/320,self.ymax-5*(self.ymax-self.ymin)/240])
    self.seg([0,self.ymax],[-5*(self.xmax-self.xmin)/320,self.ymax-5*(self.ymax-self.ymin)/240])
    for i in range(self.ymin+1,self.ymax):
      self.seg([-2*(self.xmax-self.xmin)/320,i],[2*(self.xmax-self.xmin)/320,i])
    
  def axes(self):
    self.axex()
    self.axey()
  
  def grille(self):
    for i in range(self.ymin+1,self.ymax):
      self.seg([self.xmin,i],[self.xmax,i],color(200,200,200))
    for i in range(self.xmin+1,self.xmax):
      self.seg([i,self.ymin],[i,self.ymax],color(200,200,200))
  
  def point(self,p):
    r=3*self.xpix
    for t in range(0,100):
      self.seg(p,[p[0]+r*cos(t*0.0628),p[1]+r*sin(t*0.0628)])
  
  def cercle(self,p,r):
    q = [p[0]+r,p[1]]
    for t in range(0,101):
      self.seg(q,[p[0]+r*cos(t*0.0628),p[1]+r*sin(t*0.0628)])
      q = [p[0]+r*cos(t*0.0628),p[1]+r*sin(t*0.0628)]
  
  def poly(self,*args):
    q = args[0]
    for i in range(1,len(args)):
      self.seg(q,args[i])
      q = args[i]
    self.seg(args[len(args)-1],args[0])
  
  def milieu(self,p,q):
    self.point([(p[0]+q[0])/2,(p[1]+q[1])/2])
  
  def droite(self,p,q):
    if p[0]==q[0]:
      self.seg([p[0],self.ymin],[p[0],self.ymax])
    else:
      a=(q[1]-p[1])/(q[0]-p[0])
      b=q[1]-a*q[0]
      self.seg([self.xmin,self.xmin*a+b],[self.xmax,self.xmax*a+b])
  
  def eqcart(self,a,b,c):
    if a!=0:
      self.droite([c/a,0],[(c-b*self.ymax)/a,self.ymax])
    else:
      self.droite([self.xmin,c/b],[self.xmax,c/b])
  


      


rep = Rep(-10,10,-8,8)
rep.grille()
rep.axes()
A=(5,5)
B=(7,-2)
C=(3,2)
D=(-3,-1)
rep.point(A)
rep.point(B)
rep.point(C)
rep.point(D)
#rep.seg(A,B)
#rep.cercle(A,3)
rep.poly(A,B,C,D)
#rep.milieu(A,B)
#rep.droite(A,B)
#rep.eqcart(1,2,-4)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.