espint.py

Created by dan-wallez

Created on December 20, 2024

10.4 KB

3 Fonctions d’intersection associées à l’espace à 3 dimensions, Menu : 1 :Droite d’intersection de 2 plans non parallèles 2 :Point d’intersection d’une droite avec un plan (si droite non parallèle au plan) 3 :Point d’intersection de 2 droites si sécantes Attention données à entrer sous forme de fraction.Police python format petit


class Fraction:
  def __init__(self,x,y):
    self.numerator=int(x)
    self.denominator=int(y)
  def __add__(self,fract):
    return Fraction(self.numerator*fract.denominator + self.denominator*fract.numerator , self.denominator*fract.denominator).simplify()
  def fmult(self,fract):
    return Fraction(self.numerator*fract.numerator , self.denominator*fract.denominator).simplify()
  def fdiv(self,fract):
    return Fraction(self.numerator*fract.denominator , self.denominator*fract.numerator).simplify()
  def __sub__(self,fract):
    return Fraction(self.numerator*fract.denominator - self.denominator*fract.numerator , self.denominator*fract.denominator).simplify()
  def simplify(self):
    divc = pgcd(self.numerator,self.denominator)
    a=self.numerator // divc
    b=self.denominator // divc
    return Fraction(a,b)
  def fstring(self):
    self=self.simplify()
    if self.denominator==1 :
      return "{}".format(self.numerator)
    else :
      return "{}/{}".format(self.numerator,self.denominator)

def coord(coord,n):
  num = int(input("\nNumérateur   de " + coord + n + " = "))
  den = int(input(  "Dénominateur de " + coord + n + " = "))
  return(Fraction(num,den))

def saisie(nom):
  x = coord("x",nom)
  y = coord("y",nom)
  z = coord("z",nom)
  return(x,y,z)

def pgcd(a,b):
  if b==0:
    return int(a)
  else:
    r=a%b
    return int(pgcd(int(b),int(r)))

def solution(a,b,d,c,aa,bb,dd,cc):
  den = a.fmult(bb) - aa.fmult(b)
  return((dd.fmult(b)-d.fmult(bb)).fdiv(den), (cc.fmult(b)-c.fmult(bb)).fdiv(den), (d.fmult(aa)-dd.fmult(a)).fdiv(den), (c.fmult(aa)-cc.fmult(a)).fdiv(den))

def trouve_t(a,b,c,aa,bb,cc,d):
  t1 = (c.fmult(bb) - cc.fmult(b)).fdiv(d)
  t2 = (a.fmult(cc) - aa.fmult(c)).fdiv(d)
  return(t1,t2)

#     PROGRAMME PRINCIPAL

print("\nIMPORTANT POUR LA SAISIE")
print("Si donnée sous forme de fraction :")
print("Numérateur   entier relatif")
print("Dénominateur entier relatif non nul")

nul = Fraction(0,1)

choix = 0
while choix > -1 and choix < 3 :

  print("\nINTERSECTION entre :\n")
  print("2 plans P1 et P2    : choix 0")
  print("Droite (AB) et plan : choix 1")
  print("2 droites (AB),(CD) : choix 2")
  print("Quitter             : choix 3")

  choix = int(input("\nChoix = "))

  if choix == 0 :
    
    print("Si les 2 plans sont sécants :")
    print("\nDétermination d'une équation")
    print("paramétrique de la droite")
    print("intersection des 2 plans")

    print("\nSaisie des équations cartésiennes")
    print("ax+by+cz+d = 0 des plans P1 et P2")   
    print("\nPlan P1")
    a1, b1, c1, d1 = coord("a1",""), coord("b1",""), coord("c1",""), coord("d1","")
    print("\nPlan P2")
    a2, b2, c2, d2 = coord("a2",""), coord("b2",""), coord("c2",""), coord("d2","")

    if (b1.fmult(c2)-c1.fmult(b2)).numerator==0 and (c1.fmult(a2)-a1.fmult(c2)).numerator==0 and (a1.fmult(b2)-b1.fmult(a2)).numerator==0 :
      print("\nPlans parallèles")
    else :
      print("\nPlan P1 d'équation cart. :")
      print(a1.fstring()+" x + "+b1.fstring()+" y + "+c1.fstring()+" z + "+d1.fstring()+" = 0")
      print("\nPlan P2 d'équation cart. :")
      print(a2.fstring()+" x + "+b2.fstring()+" y + "+c2.fstring()+" z + "+d2.fstring()+" = 0")

      print("\nUne équation paramétrique de la")
      print("droite intersection des 2 plans :\n")
      equation_trouvée = False
      
      if ((b1.fmult(c2)) - (b2.fmult(c1))).numerator != 0 :
        rep = solution(b1,c1,d1,a1,b2,c2,d2,a2)
        print("x = t")
        print("y = "+rep[0].fstring()+" + "+rep[1].fstring()+" * t") if rep[1].numerator!=0 else print("y = "+rep[0].fstring())       
        print("z = "+rep[2].fstring()+" + "+rep[3].fstring()+" * t") if rep[3].numerator!=0 else print("z = "+rep[2].fstring())
        equation_trouvée = True
       
      if ((a1.fmult(c2)) - (a2.fmult(c1))).numerator != 0 and not equation_trouvée :
        rep = solution(a1,c1,d1,b1,a2,c2,d2,b2)
        print("x = "+rep[0].fstring())
        print("y = t")
        print("z = "+rep[2].fstring()+" + "+rep[3].fstring()+" * t") if rep[3].numerator!=0 else print("z = "+rep[2].fstring())
        equation_trouvée = True

      if not equation_trouvée :
        rep = solution(a1,b1,d1,c1,a2,b2,d2,c2)
        print("x = "+rep[0].fstring())
        print("y = "+rep[2].fstring())
        print("z = t")

  if choix == 1 :

    print("\nIntersection droite (AB) avec plan")

    print("\nSaisie des points A et B de la droite (AB)")

    print("\nSaisie de A(xA;yA;zA)")
    xA, yA, zA = saisie("A")
    print("\nSaisie de B(xB;yB;zB)")
    xB, yB, zB = saisie("B")
    
    xAB, yAB, zAB = xB-xA, yB-yA, zB-zA

    if xAB.numerator==0 and yAB.numerator==0 and zAB.numerator==0 :
      print("A et B confondus")
    else :
      print("\nSaisie de l'équation cartésienne")
      print("ax + by + cz + d = 0 du plans P1")      
      a, b, c, d = coord("a",""), coord("b",""), coord("c",""), coord("d","")

      print("\nPlan P d'équation cart. :")
      print(a.fstring()+" x + "+b.fstring()+" y + "+c.fstring()+" z + "+d.fstring()+" = 0")
      print("\nDroite (AB) avec :")
      print("A("+xA.fstring()+" ; "+yA.fstring()+" ; "+zA.fstring()+")")
      print("B("+xB.fstring()+" ; "+yB.fstring()+" ; "+zB.fstring()+")")

      A_Plan = (a.fmult(xA) + b.fmult(yA) + c.fmult(zA) + d).numerator == 0
      B_Plan = (a.fmult(xB) + b.fmult(yB) + c.fmult(zB) + d).numerator == 0

      if A_Plan and B_Plan :
        print ("\nDroite (AB) confondue avec le plan\n")
      elif A_Plan :
        print ("\nA point d'intersection")
        print ("de la droite (AB) avec le plan")
        print("\nA ("+xA.fstring()+" ; "+yA.fstring()+" ; "+zA.fstring()+")")
      elif B_Plan :
        print ("\nB point d'intersection")
        print ("de la droite (AB) avec le plan")
        print("\nB ("+xB.fstring()+" ; "+yB.fstring()+" ; "+zB.fstring()+")")
      else :
        if (a.fmult(xAB) + b.fmult(yAB) + c.fmult(zAB)).numerator == 0 :
          print ("\nDte (AB) strictement parallèle")
          print ("avec le plan : pas de pt d'intersection\n")
        else :
          t_num = nul - (a.fmult(xA) + b.fmult(yA) + c.fmult(zA) + d)
          t_den = a.fmult(xAB) + b.fmult(yAB) + c.fmult(zAB)
          t     = t_num.fdiv(t_den)

          print("\nI pt d'intersection")
          print("de la droite (AB) avec le plan")
          xI = xA + t.fmult(xAB)
          yI = yA + t.fmult(yAB)
          zI = zA + t.fmult(zAB)
          print("\nI ("+xI.fstring()+" ; "+yI.fstring()+" ; "+zI.fstring()+")")
     
          xIdec = round(xI.numerator/xI.denominator,3)
          yIdec = round(yI.numerator/yI.denominator,3)
          zIdec = round(zI.numerator/zI.denominator,3)

          print("I en décimal (précision au millième)")
          print("I (",xIdec,";",yIdec,";",zIdec,")")

  if choix == 2 :

    print("\nDétermination du pt d'intersection I")
    print("de 2 droites (AB) et (CD) si sécantes")

    print("\nSaisie des points A et B de la droite (AB)")

    print("\nSaisie de A(xA;yA;zA)")
    xA, yA, zA = saisie("A")    
    print("\nSaisie de B(xB;yB;zB)")
    xB, yB, zB = saisie("B")

    xAB, yAB, zAB = xB-xA, yB-yA, zB-zA
    
    if xAB.numerator==0 and yAB.numerator==0 and zAB.numerator==0 :
      
      print("\nA et B confondus :")
      print("(AB) n'est pas une droite")
      
    else :

      print("\nSaisie des points C et D de la droite (CD)")

      print("\nSaisie de C(xC;yC;zC)")
      xC, yC, zC = saisie("C")
      print("\nSaisie de D(xD;yD;zD)")
      xD, yD, zD = saisie("D")
    
      xCD, yCD, zCD = xD-xC, yD-yC, zD-zC
      
      if xCD.numerator==0 and yCD.numerator==0 and zCD.numerator==0 :
        
        print("\nC et D confondus :")
        print("(CD) n'est pas une droite")
        
      else :

        print("\nDroite (AB) avec :")
        print("A("+xA.fstring()+" ; "+yA.fstring()+" ; "+zA.fstring()+") et B("+xB.fstring()+" ; "+yB.fstring()+" ; "+zB.fstring()+")")
        print("Droite (CD) avec :")
        print("C("+xC.fstring()+" ; "+yC.fstring()+" ; "+zC.fstring()+") et D("+xD.fstring()+" ; "+yD.fstring()+" ; "+zD.fstring()+")")

        a1 = yAB.fmult(zCD) - zAB.fmult(yCD)
        b1 = zAB.fmult(xCD) - xAB.fmult(zCD)
        c1 = xAB.fmult(yCD) - yAB.fmult(xCD)

        if a1.numerator==0 and b1.numerator==0 and c1.numerator==0 :

          xAD, yAD, zAD = xD-xA, yD-yA, zD-zA
          xCB, yCB, zCB = xB-xC, yB-yC, zB-zC

          a2 = yAD.fmult(zCB) - zAD.fmult(yCB)
          b2 = zAD.fmult(xCB) - xAD.fmult(zCB)
          c2 = xAD.fmult(yCB) - yAD.fmult(xCB)

          if a2.numerator==0 and b2.numerator==0 and c2.numerator==0 :
            print ("\nDroites (AB) et (CD) confondues")
          else :
            print ("\nDroites strictement parallèles")
            print ("donc coplanaires : aucun point commun")

        else :

          OK_I = False

          den = yAB.fmult(xCD) - xAB.fmult(yCD)
          if den.numerator != 0 :
            t1, t2 = trouve_t(xAB,nul-xCD,xC-xA,yAB,nul-yCD,yC-yA,den)
            OK_I = ( zAB.fmult(t1) - zCD.fmult(t2) + zA - zC ).numerator == 0

          if not OK_I :
            den = zAB.fmult(xCD) - xAB.fmult(zCD)
            if den.numerator != 0 :
              t1, t2 = trouve_t(xAB,nul-xCD,xC-xA,zAB,nul-zCD,zC-zA,den)
              OK_I = ( yAB.fmult(t1) - yCD.fmult(t2) + yA - yC ).numerator == 0

          if not OK_I :
            den = zAB.fmult(yCD) - yAB.fmult(zCD)
            if den.numerator != 0 :
              t1, t2 = trouve_t(yAB,nul-yCD,yC-yA,zAB,nul-zCD,zC-zA,den)
              OK_I = ( xAB.fmult(t1) - xCD.fmult(t2) + xA - xC ).numerator == 0

          if OK_I :
            print ("\nDroites (AB) et (CD) coplanaires")
            print ("sécantes en I\n")
            xI = xA + t1.fmult(xAB)
            yI = yA + t1.fmult(yAB)
            zI = zA + t1.fmult(zAB)
            print("I ("+xI.fstring()+" ; "+yI.fstring()+" ; "+zI.fstring()+")")

            xIdec = round(xI.numerator/xI.denominator,3)
            yIdec = round(yI.numerator/yI.denominator,3)
            zIdec = round(zI.numerator/zI.denominator,3)
            
            print("I en décimal (précision au millième)")
            print("I (",xIdec,";",yIdec,";",zIdec,")")

          else :
            print ("\nAucun point d'intersection entre ces")
            print ("2 droites (AB) et (CD) non parallèles")
            print ("ces 2 droites ne sont pas coplanaires")

  if choix > 2 :
    print("\nAu revoir")
  else :
    bidon = int(input("\nSaisir un chiffre pour revenir au menu : "))
# Type your text here

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.