Hello there,

I have some codes with me to plot my graph that looks like this, but I can't really understand what is going on in the codes below that is used to calculate the Y-coordinate in order to plot out the graph:

Function findY(ByVal index As Integer) As Long
    'Calculate the y-value (equals average of data through the point)
    Dim i As Long
    Dim startx As Long
    Dim endx As Long
    Static prevYneg As Boolean
    
    startx = Int(index / Picture1.ScaleWidth * pWaveHeader.dwBufferLength / 2)
    endx = Int((index + 1) / Picture1.ScaleWidth * pWaveHeader.dwBufferLength / 2)
    If endx > pcSamplesPerSecond * pcMaxSeconds Then endx = pcSamplesPerSecond * pcMaxSeconds
    
    findY = 0
    If (startx = endx) Then
        findY = data(startx)
    Else
        'findY = data(startx)
        For i = startx To endx
            findY = findY + data(i)
        Next i
        findY = findY / (startx - endx)
    End If
    findY = findY / 2 ^ 16 * Picture1.ScaleHeight
    If (prevYneg) Then
        If (findY > TriggerLevel.Text * Picture1.ScaleHeight / 2) Then
            edge_trigger (index)
            prevYneg = False
        End If
    ElseIf (findY < TriggerLevel.Text * Picture1.ScaleHeight / 2) Then
        prevYneg = True
    End If
    If (maxY < findY) Then maxY = findY
End Function

Can someone care to explain?

the code is straight forward but i am afraid that is not the complete code and has some dependency with other parts of the code

I have the full codes, but what I want to know is why the "formula" to find startx, endx and findY are coded that way? And the conditional statements:

If (startx = endx) Then
        findY = data(startx)
    Else
        For i = startx To endx
            findY = findY + data(i)
        Next i
        findY = findY / (startx - endx)
    End If

Why findY = data(startx) when startx = endx and so on. What I want to know is why the "formulas" are as they are? I hope I am making sense.

that depends on the business logic for which the code is defined.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.