Click here to Skip to main content
15,896,432 members
Articles / Programming Languages / Visual Basic

Mathemathics Framework

Rate me:
Please Sign up or sign in to vote.
4.76/5 (56 votes)
16 Sep 2008CPOL6 min read 75.5K   6.2K   171  
.NET Mathematical Framework
Imports BV.Core
Imports BV.Math

Public Class ScaleTranslator

    Public Enum EOver
        Update
        Clamp
    End Enum

    Protected intGamma As Integer
    Protected cteA As Single
    Protected cteB As Single
    Protected intScale As ScaleBasic
    Protected intScale2 As ScaleBasic
    Protected intEOver As EOver

    Public Sub New()
        Me.intGamma = 0
        intScale = New ScaleBasic
        intScale2 = New ScaleBasic
        Me.intScale.Reset(100, 0, 100, 0)
        Me.intScale2.Reset(100, 0, 100, 0)
        Me.UpdateConstants()
    End Sub

    Public Property Gamma() As Integer
        Get
            Return Me.intGamma
        End Get
        Set(ByVal Value As Integer)
            If Me.intGamma <> Value Then
                If Value > 10 Then
                    Me.intGamma = 10
                ElseIf Value < -10 Then
                    Me.intGamma = -10
                Else
                    Me.intGamma = Value
                End If
                Me.UpdateConstants()
            End If
        End Set
    End Property

    ''' <summary>
    ''' Rango de valores de la entrada
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property InputRange() As RangeF
        Get
            Return Me.intScale.Range
        End Get
        Set(ByVal Value As RangeF)
            Me.intScale.Range = Value
            Me.UpdateConstants()
        End Set
    End Property

    ''' <summary>
    ''' Rango de valores de Salida
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property OutputRange() As RangeF
        Get
            Return Me.intScale2.OutputRange
        End Get
        Set(ByVal Value As RangeF)
            Me.intScale2.OutputRange = Value
            Me.UpdateConstants()
        End Set
    End Property

    Public Sub Reset(ByVal A As Single, ByVal B As Single)
        Me.intScale.Range = New RangeF(A, B)
        UpdateConstants()
    End Sub

    Protected Overridable Sub SetMin(ByVal Newmin As Single)
        Me.intScale.Min = Newmin
        UpdateConstants()
    End Sub

    Protected Overridable Sub SetMax(ByVal Newmax As Single)
        Me.intScale.Max = Newmax
        UpdateConstants()
    End Sub

    Protected Sub UpdateConstants()
        Dim tmp As Double

        tmp = Me.intFunctionBase(Me.intScale.OutputMax)
        cteA = Me.intScale2.OutputRange.dx / (tmp - Me.intFunctionBase(intScale.OutputMin))
        cteB = Me.intScale2.OutputMax - tmp * cteA
    End Sub

    Protected Function intFunctionBase(ByVal x As Single) As Single
        If Me.intGamma = 0 Then
            Return x
        ElseIf Me.intGamma > 0 Then
            Return x ^ intGamma
        End If
        Return x ^ (1 / (-intGamma))
    End Function

    Public Function Calc(ByVal x As Single) As Single
        Dim x2 As Double

        If x > Me.intScale.Max Then
            If intEOver = EOver.Update Then
                SetMax(x)
                Return Me.intScale2.OutputMax
            ElseIf intEOver = EOver.Clamp Then
                Return Me.intScale2.OutputMax
                'x = Me.intScale.Max
            End If
        ElseIf x < Me.intScale.Min Then
            If intEOver = EOver.Update Then
                SetMin(x)
                Return Me.intScale2.OutputMin
            ElseIf intEOver = EOver.Clamp Then
                Return Me.intScale2.OutputMin
                'x = Me.intScale.Min
            End If
        End If
        x2 = Me.intScale.Calc(x)
        Return cteA * Me.intFunctionBase(x2) + cteB
    End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Universidad Tecnológica Nacional
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions