Differenze tra le versioni di "Calcolo e rappresentazione grafica in una DrawingArea del Seno e del Coseno"

Da Gambas-it.org - Wikipedia.
 
Riga 2: Riga 2:
 
  Private DrawingArea1 As DrawingArea
 
  Private DrawingArea1 As DrawingArea
 
  Private Const ANGOLO As Single = 0.0  <FONT Color=gray>' ''L'angolo, espresso in gradi, di cui si intende trovare il "Seno".''</font>  
 
  Private Const ANGOLO As Single = 0.0  <FONT Color=gray>' ''L'angolo, espresso in gradi, di cui si intende trovare il "Seno".''</font>  
  Private raggio As Single = 200.0      <FONT Color=gray>' ''La lunghezza del raggio dell'ipotetica circonferenza.''</font>  
+
  Private raggio As Single             <FONT Color=gray>' ''La lunghezza del raggio dell'ipotetica circonferenza.''</font>  
 
  Private w2 As Single
 
  Private w2 As Single
 
  Private h2 As Single
 
  Private h2 As Single
Riga 30: Riga 30:
 
   w2 = DrawingArea1.W / 2
 
   w2 = DrawingArea1.W / 2
 
   h2 = DrawingArea1.H / 2
 
   h2 = DrawingArea1.H / 2
 +
  raggio = DrawingArea1.W / 3
 
   
 
   
 
  '''End'''  
 
  '''End'''  
Riga 43: Riga 44:
 
     grd = CStr(ANGOLO + c) & "°"
 
     grd = CStr(ANGOLO + c) & "°"
 
  <FONT Color=gray>' ''Calcola la lunghezza del "Coseno" = 𝜌 cos 𝜃 :''</font>
 
  <FONT Color=gray>' ''Calcola la lunghezza del "Coseno" = 𝜌 cos 𝜃 :''</font>
     x = <FONT Color=#B22222>RAGGIO * (cs)</font>
+
     x = <FONT Color=#B22222>raggio * (cs)</font>
 
  <FONT Color=gray>' ''Calcola la lunghezza del "Seno" = 𝜌 sen 𝜃 :''</font>
 
  <FONT Color=gray>' ''Calcola la lunghezza del "Seno" = 𝜌 sen 𝜃 :''</font>
     y = <FONT Color=#B22222>RAGGIO * (sn)</font>
+
     y = <FONT Color=#B22222>raggio * (sn)</font>
 
     DrawingArea1.Refresh
 
     DrawingArea1.Refresh
 
     Select Case c
 
     Select Case c
Riga 69: Riga 70:
 
   
 
   
 
   With Paint
 
   With Paint
     .DrawText("ρ = " & CStr(RAGGIO) & " pixel", 10, 10, .Font.TextWidth("X"), .Font.TextHeight("X"), Align.Normal)
+
     .DrawText("ρ = " & CStr(raggio) & " pixel", 10, 10, .Font.TextWidth("X"), .Font.TextHeight("X"), Align.Normal)
 
     .DrawText(grd, w2, h2 - dis, .Font.TextWidth(grd), .Font.TextHeight(grd), Align.Normal)
 
     .DrawText(grd, w2, h2 - dis, .Font.TextWidth(grd), .Font.TextHeight(grd), Align.Normal)
 
     .LineWidth = 2.4
 
     .LineWidth = 2.4

Versione attuale delle 08:10, 6 gen 2022

Mostriamo di seguito un possibile codice per calcolare e disegnare su una DrawingArea il Seno e il Coseno, avendo noti il raggio di una ipotetica circonferenza e un angolo interno adiacente all'ipotenusa.

Private DrawingArea1 As DrawingArea
Private Const ANGOLO As Single = 0.0  ' L'angolo, espresso in gradi, di cui si intende trovare il "Seno". 
Private raggio As Single              ' La lunghezza del raggio dell'ipotetica circonferenza. 
Private w2 As Single
Private h2 As Single
Private sn As Single
Private cs As Single
Private x As Single    ' La lunghezza del "Coseno" da calcolare.
Private y As Single    ' La lunghezza del "Seno" da calcolare.
Private c As Short
Private grd As String
Private dis As Short = -10

Public Sub _new()

 With Me
   .W = 600
   .H = 600
   .Arrangement = Arrange.Fill
 End With
 With DrawingArea1 = New DrawingArea(Me) As "DrawingArea1"
   .Background = Color.White
 End With

End

Public Sub Form_Arrange()

 w2 = DrawingArea1.W / 2
 h2 = DrawingArea1.H / 2
 raggio = DrawingArea1.W / 3

End 

Public Sub Form_Open()

 Me.Show
 
' Avvia un ciclo infinito:
 Do
   cs = Cos(Rad(ANGOLO + c))
   sn = Sin(Rad(ANGOLO + c))
   grd = CStr(ANGOLO + c) & "°"
' Calcola la lunghezza del "Coseno" = 𝜌 cos 𝜃 :
   x = raggio * (cs)
' Calcola la lunghezza del "Seno" = 𝜌 sen 𝜃 :
   y = raggio * (sn)
   DrawingArea1.Refresh
   Select Case c
     Case 91 To 180
       grd = CStr(180 - c) & "°"
     Case 181 To 270
       grd = CStr(c - 180) & "°"
       dis = 20
     Case 271 To 360
       grd = CStr(360 - c) & "°"
       If c == 360 Then
         c = 0
         dis = -10
       Endif
   End Select
   Wait 0.2
   Inc c
 Loop 

End

Public Sub DrawingArea1_Draw()

 With Paint
   .DrawText("ρ = " & CStr(raggio) & " pixel", 10, 10, .Font.TextWidth("X"), .Font.TextHeight("X"), Align.Normal)
   .DrawText(grd, w2, h2 - dis, .Font.TextWidth(grd), .Font.TextHeight(grd), Align.Normal)
   .LineWidth = 2.4
   .MoveTo(w2, h2)
   .LineTo(w2 + x, h2 - y)
   .Stroke

   .LineWidth = 1.0
   .Brush = .Color(Color.Red)
   .DrawText("Seno:  " & "Rad = " & Format(sn, "0.000") & "    ρ(sen θ) = " & Format(y, "#.0") &
             " pixel", 10, 15 + .Font.TextHeight("S"), .Font.TextWidth("Seno"), .Font.TextHeight("S"), Align.Normal)
   .MoveTo(w2 + x, h2)
   .LineTo(w2 + x, h2 - y)
   .Stroke

   .Brush = .Color(Color.Blue)
   .DrawText("Coseno: " & "Rad = " & Format(cs, "0.000") & "    ρ(cos θ) = " & Format(x, "#.0") &
                      " pixel", 10, 35 + .Font.TextHeight("C"), .Font.TextWidth("Coseno"), .Font.TextHeight("C"), Align.Normal)
   .MoveTo(w2, h2)
   .LineTo(w2 + x, h2)
   .Stroke

   .End
 End With

End

Public Sub Form_Resize()

 DrawingArea1.Resize(Me.W, Me.H)

 ' Fa sì che il raggio non sia mai superiore ad alcuna delle due dimensioni della "DrawingArea":
 raggio = Min(DrawingArea1.W, DrawingArea1.H) / 3

End