Running Graph - Petr - 03-12-2023
Input values are considered in the range -1 to 1, the input is not guarded (internal checking is disabled) so using higher values will render outside the intended range. I think it might be useful for someone.
Code: (Select All) _Title "Running Graph"
'Wroted by Petr Preclik, 11.March 2023
Type RG
position As Integer
SO As Long 'array in array StartOffset for RG_HELPER
Recs As Long 'how much records graph contains (record lenght in array RG_Helper)
End Type
ReDim Shared RG(0) As RG
ReDim Shared RG_Helper(0) As Single
Screen _NewImage(800, 600, 256)
test = NewRG(1, 500)
test2 = NewRG(1, 203)
test3 = NewRG(1, 300) 'test, test2, test3 is returned index record from array RG
Do
i = i + .1
j = j + .012
t = Sin(i)
UpdateRG test, t 'update values in array RG_Helper using array RG in RG_Helper SUB
v = Cos(j)
UpdateRG test2, v
UpdateRG test3, (v + t) 'both previous
ShowRG 100, 150, test, "Sinus"
ShowRG 100, 300, test2, "Cosinus" 'Draw it - use RG array to drive RG_Helper array and show values RG_Helper array on the screen
ShowRG 100, 450, test3, "Both mixed"
_Display
_Limit 200
Loop
Function NewRG (value, records) 'create new graph handle, reserve place in RG_Helper, write to RG_Helper array first value and this value position in RG_Helper array
u = records
u2 = UBound(RG_Helper)
u3 = UBound(RG)
RG(u3).SO = u2
RG(u3).Recs = u
RG(u3).position = 1
NewRG = u3
RG_Helper(u2) = value
ReDim _Preserve RG_Helper(u2 + u + 1) As Single
ReDim _Preserve RG(u3 + 1) As RG
End Function
Sub UpdateRG (identity, value) ' update and shift values in RG_Helper array using RG array (identity is RG array index)
Id = identity
V = value
If RG(Id).position < RG(Id).Recs Then
RG(Id).position = RG(Id).position + 1
i2 = RG(Id).position
u = RG(Id).SO
RG_Helper(u + i2) = value
Exit Sub
Else
shift = RG(Id).SO
Do Until shift = RG(Id).SO + RG(Id).Recs
RG_Helper(shift) = RG_Helper(shift + 1)
shift = shift + 1
Loop
RG_Helper(RG(Id).SO + RG(Id).Recs) = value
End If
End Sub
Sub ShowRG (x, y, id, index$) ' Draw graph to screen
xx = x
s2 = RG(id).Recs
s = RG(id).SO
_PrintMode _KeepBackground
p = xx - 10 + s2 / 2 - _PrintWidth(index$) / 2 'printstring X
Line (xx - 20, y - 70)-(xx + 20 + s2, y + 50), 30, BF
Line (xx - 17, y - 67)-(xx + 17 + s2, y + 47), , B
C = _DefaultColor
Color 0
_PrintString (p, y - 64), index$
Color C
_PrintMode _FillBackground
Line (xx - 20, y - 70)-(xx + 20 + s2, y + 50), , B
Line (xx - 17, y - 47)-(xx + 17 + s2, y + 47), , B
ss = s
Do Until ss = s2 + s - 1
v = RG_Helper(ss)
v2 = RG_Helper(ss + 1)
GoTo notthis
If Abs(v) > 1 Then
Do Until Abs(v) <= 1
v = v / 2
Loop
End If
notthis:
xx = xx + 1
Line (xx, y + v * 15)-(xx + 1, y + v2 * 15), 0
ss = ss + 1
Loop
xx = 0
End Sub
RE: Running Graph - Dimster - 03-12-2023
Hi Petr - if I were to add color, would that command go in the Do Loop or the Subroutine displaying the results? Typically it's in the display routine, but where the graph peaks and hits the lowest part of the valley, those points are found in Do Loop?????
RE: Running Graph - Petr - 03-12-2023
Hi,
NewRg must be before the DO LOOP loop - usually at the beginning as DIM, the rest in the DO LOOP loop. ShowRG statement have the points X, Y as the upper left corner of the graph. UpdateRG inserts the current results into the graph, ShowRG displays them. The range of inserted values is calculated from -1 to 1, zero is in the middle of the graph.
|