Advertisement
Gianvy

Sinusoide di Gianluca Mancusi 3ainf

Apr 10th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.88 KB | None | 0 0
  1. Imports System.Drawing
  2. Public Class Form1
  3.     'DICHIARAZIONI VARIABILI GLOBALI
  4.     Dim disegno As Graphics
  5.     Dim Asse As Graphics
  6.     Dim OrigineX As Integer
  7.     Dim OrigineY As Integer
  8.     Dim Told As Integer
  9.     Dim Tnew As Integer = 0
  10.     Dim Yold As Double
  11.     Dim YoldINT As Integer
  12.     Dim Ynew As Double = 0
  13.     Dim YnewINT As Integer
  14.     Dim STOPval As Boolean = False
  15.     'LA PRIMA FUNCTION CHE VIENE CHIAMTA (MAIN in C)
  16.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  17.         disegnagrafico()
  18.     End Sub
  19.  
  20.     'QUANDO PREMI IL PULSANTE GENERA
  21.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  22.         Button3.Enabled = True
  23.         ResetGrafico()
  24.         OrigineX = 15
  25.         OrigineY = 150
  26.         Told = OrigineX
  27.         Yold = OrigineY
  28.         disegnagrafico()
  29.         Disegnatore.Interval = QuantoTempoT.Text
  30.         Disegnatore.Start()
  31.     End Sub
  32.  
  33.     'QUANTO PREMI IL PULSANTE RESET
  34.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  35.         ResetGrafico()
  36.         Button3.Enabled = False
  37.     End Sub
  38.  
  39.     'QUANDO PREMI IL PULSANTE STOP
  40.     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  41.         If (STOPval = True) Then
  42.             Disegnatore.Stop()
  43.             STOPval = False
  44.         Else
  45.             Disegnatore.Start()
  46.             STOPval = True
  47.         End If
  48.     End Sub
  49.  
  50.     'DISEGNO DELLA SINUSOIDE
  51.     Private Sub Disegnatore_Tick(sender As Object, e As EventArgs) Handles Disegnatore.Tick
  52.         STOPval = True
  53.         InfoLabel.Text = "Generazione sinusoide avviata..."
  54.         disegno = PictureBox1.CreateGraphics
  55.         Tnew = Told + 1
  56.         Ynew = OrigineY + (ValoreMassimoT.Text * Math.Sin((angolo2radiante(RotazioneT.Text) / PeriodoT.Text) * Tnew + angolo2radiante(AngoloSfasamentoT.Text)))
  57.         YnewINT = Convert.ToInt32(Int(Ynew))
  58.         YoldINT = Convert.ToInt32(Int(Yold))
  59.         disegno.DrawLine(Pens.Red, Told, YoldINT, Tnew, YnewINT)
  60.         Told = Tnew
  61.         Yold = Ynew
  62.         If (Tnew > TempodapercorrereT.Text Or YnewINT > 500) Then
  63.             InfoLabel.Text = "La sinusoide si è fermata, grafico completato. Premere RESET."
  64.             Disegnatore.Stop()
  65.             Button3.Enabled = False
  66.         End If
  67.     End Sub
  68.  
  69.     'Testo delle informazioni in basso
  70.     Private Sub Informante_Tick(sender As Object, e As EventArgs) Handles Informante.Tick
  71.         Xvalore.Text = "X= " & Tnew - OrigineX
  72.         Yvalore.Text = "Y= " & YnewINT - OrigineY
  73.     End Sub
  74.  
  75.     'FUNCTIONS
  76.     Function angolo2radiante(ByVal angolo As Integer) As Double
  77.         Dim risultato As Double
  78.         risultato = (Math.PI * angolo) / 180
  79.         Return risultato
  80.     End Function
  81.  
  82.     Function disegnagrafico()
  83.         Asse = PictureBox1.CreateGraphics
  84.         Asse.DrawLine(Pens.LightGray, 0, OrigineY, 500, OrigineY)
  85.         Asse.DrawLine(Pens.LightGray, OrigineX, OrigineY + 150, OrigineX, 0)
  86.         Return 0
  87.     End Function
  88.  
  89.     Function ResetGrafico()
  90.         InfoLabel.Text = "Reset grafico in elaborazione..."
  91.         Disegnatore.Stop()
  92.         PictureBox1.Refresh()
  93.         disegnagrafico()
  94.         InfoLabel.Text = "Grafico pulito e ridisegnato."
  95.         Return 0
  96.     End Function
  97.  
  98.  
  99.  
  100.     '==================== CONTROLLI ANTI BUG ========================
  101.     Private Sub RotazioneT_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RotazioneT.KeyPress
  102.         If (Not IsNumeric(e.KeyChar)) And (Asc(e.KeyChar) <> 8) Then 'FUNZIONE PER SOLI NUMERI
  103.             e.Handled = True
  104.             InfoLabel.Text = "Puoi scrivere solo numeri."
  105.         End If
  106.     End Sub
  107.  
  108.     Private Sub PeriodoT_KeyPress(sender As Object, e As KeyPressEventArgs) Handles PeriodoT.KeyPress
  109.         If (Not IsNumeric(e.KeyChar)) And (Asc(e.KeyChar) <> 8) Then 'FUNZIONE PER SOLI NUMERI
  110.             e.Handled = True
  111.             InfoLabel.Text = "Puoi scrivere solo numeri."
  112.         End If
  113.     End Sub
  114.  
  115.     Private Sub AngoloSfasamentoT_KeyPress(sender As Object, e As KeyPressEventArgs) Handles AngoloSfasamentoT.KeyPress
  116.         If (Not IsNumeric(e.KeyChar)) And (Asc(e.KeyChar) <> 8) Then 'FUNZIONE PER SOLI NUMERI
  117.             e.Handled = True
  118.             InfoLabel.Text = "Puoi scrivere solo numeri."
  119.         End If
  120.     End Sub
  121.  
  122.     Private Sub ValoreMassimoT_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ValoreMassimoT.KeyPress
  123.         If (Not IsNumeric(e.KeyChar)) And (Asc(e.KeyChar) <> 8) Then 'FUNZIONE PER SOLI NUMERI
  124.             e.Handled = True
  125.             InfoLabel.Text = "Puoi scrivere solo numeri."
  126.         End If
  127.     End Sub
  128.  
  129.     Private Sub QuantoTempoT_KeyPress(sender As Object, e As KeyPressEventArgs) Handles QuantoTempoT.KeyPress
  130.         If (Not IsNumeric(e.KeyChar)) And (Asc(e.KeyChar) <> 8) Then 'FUNZIONE PER SOLI NUMERI
  131.             e.Handled = True
  132.             InfoLabel.Text = "Puoi scrivere solo numeri."
  133.         End If
  134.     End Sub
  135.  
  136.     Private Sub TempodapercorrereT_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TempodapercorrereT.KeyPress
  137.         If (Not IsNumeric(e.KeyChar)) And (Asc(e.KeyChar) <> 8) Then 'FUNZIONE PER SOLI NUMERI
  138.             e.Handled = True
  139.             InfoLabel.Text = "Puoi scrivere solo numeri."
  140.         End If
  141.     End Sub
  142.  
  143.     Private Sub PeriodoT_TextChanged(sender As Object, e As EventArgs) Handles PeriodoT.TextChanged
  144.         If PeriodoT.Text = "0" Then 'Il periodo non può essere 0
  145.             PeriodoT.Clear()
  146.             InfoLabel.Text = "Non puoi impostare il periodo a 0."
  147.         End If
  148.     End Sub
  149.  
  150.     Private Sub QuantoTempoT_TextChanged(sender As Object, e As EventArgs) Handles QuantoTempoT.TextChanged
  151.         If QuantoTempoT.Text = "0" Then
  152.             InfoLabel.Text = "Attenzione: Non puoi inserire 0 tempo!"
  153.             QuantoTempoT.Clear()
  154.         End If
  155.     End Sub
  156.  
  157.    
  158. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement