Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Cerchio3p - Main.rb

Main.rb

Caricato da: Poggi Marco
Scarica il programma completo

  1. require "tk"
  2. load "tela.rb"
  3. load "sistema.rb"
  4.  
  5. class App
  6.         def initialize()
  7.                 @radice=TkRoot.new do
  8.                         title "Circonferenza"
  9.                 end
  10.                 inizializza_componenti
  11.                 @tela=Foglio::Grafica.new(@canTela)
  12.                 @tela.isoDimensiona(-20.0, -14.13, 20.0)
  13.                 cancella
  14.                 @radice.mainloop()
  15.         end
  16.        
  17.         def disegna()
  18.             a = Foglio::Punto.new
  19.                 b = Foglio::Punto.new
  20.                 c = Foglio::Punto.new
  21.                 centro=Foglio::Punto.new
  22.                 a.x = @textPunto1_x.get(1.0, 'end')
  23.                 a.y = @textPunto1_y.get(1.0, 'end')
  24.                 b.x = @textPunto2_x.get(1.0, 'end')
  25.                 b.y = @textPunto2_y.get(1.0, 'end')
  26.                 c.x = @textPunto3_x.get(1.0, 'end')
  27.                 c.y = @textPunto3_y.get(1.0, 'end')
  28.                 @tela.cancella("#101507")
  29.                 @tela.penna.spessore = 1
  30.                 @tela.assi("#808080")
  31.                 begin
  32.                         cerchio=Sistema::Circonferenza.new(a, b, c)
  33.                         soluzioni=cerchio.soluzioni
  34.                         centro.x=soluzioni[0] / -2.0
  35.                     centro.y=soluzioni[1] / -2.0
  36.                     raggio=(centro.x ** 2 + centro.y ** 2 - soluzioni[2]) ** 0.5
  37.                         @labRisultato['text']="Equazione: X^2 + Y^2 + a*X + b*Y + c = 0\na = %0.4f b = %0.4f c = %0.4f\ncentro in x = %0.4f y = %0.4f e raggio = %0.5f" % [soluzioni[0], soluzioni[1], soluzioni[2], centro.x, centro.y, raggio]
  38.                         traccia(centro, raggio, '#ff9012')
  39.                         traccia(a, 0.25, '#90ff12')
  40.                         traccia(b, 0.25, '#90ff12')
  41.                         traccia(c, 0.25, '#90ff12')
  42.                         rescue Exception => ex
  43.                                 @labRisultato['text']="I punti inseriti sono allineati, o coincidenti."
  44.                 end    
  45.         end
  46.        
  47.         def traccia(centro, raggio, colore)
  48.                 penna=Foglio::Pennino.new
  49.                 penna.spessore = 2
  50.                 penna.colore = colore
  51.                 @tela.penna = penna
  52.                 @tela.cerchio(centro, raggio)
  53.         end
  54.        
  55.         def cancella()
  56.                 @textPunto1_x.delete(1.0, 'end')
  57.                 @textPunto1_y.delete(1.0, 'end')
  58.                 @textPunto2_x.delete(1.0, 'end')
  59.                 @textPunto2_y.delete(1.0, 'end')
  60.                 @textPunto3_x.delete(1.0, 'end')
  61.                 @textPunto3_y.delete(1.0, 'end')
  62.                 @labRisultato['text'] = "Equazione: X^2 + Y^2 + a*X + b*Y + c = 0"
  63.                 @tela.cancella("#101507")
  64.                 @tela.penna.spessore = 1
  65.                 @tela.assi("#808080")
  66.         end
  67.        
  68.         private
  69.         def inizializza_componenti()
  70.                 mioFont=TkFont.new('courier 12')
  71.                 istanza=self
  72.                
  73.                 #lab1
  74.                 @lab1=TkLabel.new(@radice) do
  75.                         place( 'x' => 50, 'y' => 10)
  76.                 end
  77.                 @lab1['text'] = "Punto 1"
  78.                 @lab1['font'] = mioFont
  79.                
  80.                 #lab2
  81.                 @lab2=TkLabel.new(@radice) do
  82.                         place( 'x' => 185, 'y' => 10)
  83.                 end
  84.                 @lab2['text'] = "Punto 2"
  85.                 @lab2['font'] = mioFont
  86.                
  87.                 #lab3
  88.                 @lab3=TkLabel.new(@radice) do
  89.                         place( 'x' => 320, 'y' => 10)
  90.                 end
  91.                 @lab3['text'] = "Punto 3"
  92.                 @lab3['font'] = mioFont
  93.                
  94.                 #lab4
  95.                 @lab4=TkLabel.new(@radice) do
  96.                         place( 'x' => 20, 'y' => 35)
  97.                 end
  98.                 @lab4['text'] = "X"
  99.                 @lab4['font'] = mioFont
  100.                
  101.                 #lab5
  102.                 @lab5=TkLabel.new(@radice) do
  103.                         place( 'x' => 20, 'y' => 60)
  104.                 end
  105.                 @lab5['text'] = "Y"
  106.                 @lab5['font'] = mioFont
  107.                
  108.                 #labRisultato
  109.                 @labRisultato=TkLabel.new(@radice) do
  110.                         place( 'x' => 10, 'y' => 630)
  111.                 end
  112.                 @labRisultato['text'] = ""
  113.                 @labRisultato['font'] = mioFont
  114.                 @labRisultato['justify'] = 'left'
  115.                
  116.                 #textPunto1_x
  117.                 @textPunto1_x=TkText.new(@radice)
  118.                 @textPunto1_x['font']=mioFont
  119.                 @textPunto1_x['width']=12
  120.                 @textPunto1_x['height']=1
  121.                 @textPunto1_x.place('x' => 50, 'y' => 35)
  122.                
  123.                 #textPunto1_y
  124.                 @textPunto1_y=TkText.new(@radice)
  125.                 @textPunto1_y['font']=mioFont
  126.                 @textPunto1_y['width']=12
  127.                 @textPunto1_y['height']=1
  128.                 @textPunto1_y.place('x' => 50, 'y' => 60)
  129.                
  130.                 #textPunto2_x
  131.                 @textPunto2_x=TkText.new(@radice)
  132.                 @textPunto2_x['font']=mioFont
  133.                 @textPunto2_x['width']=12
  134.                 @textPunto2_x['height']=1
  135.                 @textPunto2_x.place('x' => 185, 'y' => 35)
  136.                
  137.                 #textPunto2_y
  138.                 @textPunto2_y=TkText.new(@radice)
  139.                 @textPunto2_y['font']=mioFont
  140.                 @textPunto2_y['width']=12
  141.                 @textPunto2_y['height']=1
  142.                 @textPunto2_y.place('x' => 185, 'y' => 60)
  143.                
  144.                 #textPunto3_x
  145.                 @textPunto3_x=TkText.new(@radice)
  146.                 @textPunto3_x['font']=mioFont
  147.                 @textPunto3_x['width']=12
  148.                 @textPunto3_x['height']=1
  149.                 @textPunto3_x.place('x' => 320, 'y' => 35)
  150.                
  151.                 #textPunto3_y
  152.                 @textPunto3_y=TkText.new(@radice)
  153.                 @textPunto3_y['font']=mioFont
  154.                 @textPunto3_y['width']=12
  155.                 @textPunto3_y['height']=1
  156.                 @textPunto3_y.place('x' => 320, 'y' => 60)
  157.                
  158.                 #btnTraccia
  159.                 @btbTraccia=TkButton.new(@radice)do
  160.                         command proc{istanza.disegna}
  161.                         place('x' => 470, 'y' => 10)
  162.                 end
  163.                 @btbTraccia['font'] = mioFont
  164.                 @btbTraccia['text'] = 'Traccia'
  165.                
  166.                 #btnCancella
  167.                 @btbCancella=TkButton.new(@radice)do
  168.                         command proc{istanza.cancella}
  169.                         place('x' => 650, 'y' => 10)
  170.                 end
  171.                 @btbCancella['font'] = mioFont
  172.                 @btbCancella['text'] = 'Cancella'
  173.                
  174.                 #canTela
  175.                 @canTela=TkCanvas.new(@radice)do
  176.                         width 750
  177.                         height 530
  178.                         place('x' => 10, 'y' => 95)
  179.                 end
  180.                 @radice['width']=770
  181.                 @radice['height']=700
  182.         end
  183. end
  184.  
  185. print"Cerchio passante per tre punti.\n"
  186. puts
  187. puts
  188. App.new