Calcolo e rappresentazione grafica in una DrawingArea del Seno e del Coseno
Vai alla navigazione
Vai alla ricerca
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 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