require "tk"
load "tela.rb"
load "sistema.rb"
class App
def initialize()
@radice=TkRoot.new do
title "Circonferenza"
end
inizializza_componenti
@tela=Foglio::Grafica.new(@canTela)
@tela.isoDimensiona(-20.0, -14.13, 20.0)
cancella
@radice.mainloop()
end
def disegna()
a = Foglio::Punto.new
b = Foglio::Punto.new
c = Foglio::Punto.new
centro=Foglio::Punto.new
a.x = @textPunto1_x.get(1.0, 'end')
a.y = @textPunto1_y.get(1.0, 'end')
b.x = @textPunto2_x.get(1.0, 'end')
b.y = @textPunto2_y.get(1.0, 'end')
c.x = @textPunto3_x.get(1.0, 'end')
c.y = @textPunto3_y.get(1.0, 'end')
@tela.cancella("#101507")
@tela.penna.spessore = 1
@tela.assi("#808080")
begin
cerchio=Sistema::Circonferenza.new(a, b, c)
soluzioni=cerchio.soluzioni
centro.x=soluzioni[0] / -2.0
centro.y=soluzioni[1] / -2.0
raggio=(centro.x ** 2 + centro.y ** 2 - soluzioni[2]) ** 0.5
@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]
traccia(centro, raggio, '#ff9012')
traccia(a, 0.25, '#90ff12')
traccia(b, 0.25, '#90ff12')
traccia(c, 0.25, '#90ff12')
rescue Exception => ex
@labRisultato['text']="I punti inseriti sono allineati, o coincidenti."
end
end
def traccia(centro, raggio, colore)
penna=Foglio::Pennino.new
penna.spessore = 2
penna.colore = colore
@tela.penna = penna
@tela.cerchio(centro, raggio)
end
def cancella()
@textPunto1_x.delete(1.0, 'end')
@textPunto1_y.delete(1.0, 'end')
@textPunto2_x.delete(1.0, 'end')
@textPunto2_y.delete(1.0, 'end')
@textPunto3_x.delete(1.0, 'end')
@textPunto3_y.delete(1.0, 'end')
@labRisultato['text'] = "Equazione: X^2 + Y^2 + a*X + b*Y + c = 0"
@tela.cancella("#101507")
@tela.penna.spessore = 1
@tela.assi("#808080")
end
private
def inizializza_componenti()
mioFont=TkFont.new('courier 12')
istanza=self
#lab1
@lab1=TkLabel.new(@radice) do
place( 'x' => 50, 'y' => 10)
end
@lab1['text'] = "Punto 1"
@lab1['font'] = mioFont
#lab2
@lab2=TkLabel.new(@radice) do
place( 'x' => 185, 'y' => 10)
end
@lab2['text'] = "Punto 2"
@lab2['font'] = mioFont
#lab3
@lab3=TkLabel.new(@radice) do
place( 'x' => 320, 'y' => 10)
end
@lab3['text'] = "Punto 3"
@lab3['font'] = mioFont
#lab4
@lab4=TkLabel.new(@radice) do
place( 'x' => 20, 'y' => 35)
end
@lab4['text'] = "X"
@lab4['font'] = mioFont
#lab5
@lab5=TkLabel.new(@radice) do
place( 'x' => 20, 'y' => 60)
end
@lab5['text'] = "Y"
@lab5['font'] = mioFont
#labRisultato
@labRisultato=TkLabel.new(@radice) do
place( 'x' => 10, 'y' => 630)
end
@labRisultato['text'] = ""
@labRisultato['font'] = mioFont
@labRisultato['justify'] = 'left'
#textPunto1_x
@textPunto1_x=TkText.new(@radice)
@textPunto1_x['font']=mioFont
@textPunto1_x['width']=12
@textPunto1_x['height']=1
@textPunto1_x.place('x' => 50, 'y' => 35)
#textPunto1_y
@textPunto1_y=TkText.new(@radice)
@textPunto1_y['font']=mioFont
@textPunto1_y['width']=12
@textPunto1_y['height']=1
@textPunto1_y.place('x' => 50, 'y' => 60)
#textPunto2_x
@textPunto2_x=TkText.new(@radice)
@textPunto2_x['font']=mioFont
@textPunto2_x['width']=12
@textPunto2_x['height']=1
@textPunto2_x.place('x' => 185, 'y' => 35)
#textPunto2_y
@textPunto2_y=TkText.new(@radice)
@textPunto2_y['font']=mioFont
@textPunto2_y['width']=12
@textPunto2_y['height']=1
@textPunto2_y.place('x' => 185, 'y' => 60)
#textPunto3_x
@textPunto3_x=TkText.new(@radice)
@textPunto3_x['font']=mioFont
@textPunto3_x['width']=12
@textPunto3_x['height']=1
@textPunto3_x.place('x' => 320, 'y' => 35)
#textPunto3_y
@textPunto3_y=TkText.new(@radice)
@textPunto3_y['font']=mioFont
@textPunto3_y['width']=12
@textPunto3_y['height']=1
@textPunto3_y.place('x' => 320, 'y' => 60)
#btnTraccia
@btbTraccia=TkButton.new(@radice)do
command proc{istanza.disegna}
place('x' => 470, 'y' => 10)
end
@btbTraccia['font'] = mioFont
@btbTraccia['text'] = 'Traccia'
#btnCancella
@btbCancella=TkButton.new(@radice)do
command proc{istanza.cancella}
place('x' => 650, 'y' => 10)
end
@btbCancella['font'] = mioFont
@btbCancella['text'] = 'Cancella'
#canTela
@canTela=TkCanvas.new(@radice)do
width 750
height 530
place('x' => 10, 'y' => 95)
end
@radice['width']=770
@radice['height']=700
end
end
print"Cerchio passante per tre punti.\n"
puts
puts
App.new