Friday, March 3, 2017

Monkey-X - Beginners - Vector Projection Helper - code example


Import mojo

' This is the vector class. It has a 
' x and y float variable.
'
Class vectorf
    Field x:Float,y:Float
    Method New(x:Float = 0,y:Float = 0)
        Self.x = x
        Self.y = y
    End Method
End Class

Class MyGame Extends App
    ' Here we create the 'a,b' variables using the
    ' vectorf class
    Field a:vectorf,b:vectorf
    ' cx and cy contain the center x and center y
    ' coordinates of the screen.
    Field cx:Int
    Field cy:Int
    ' time is a variable used to change(refresh)
    ' the information on the screen.
    Field time:Int
    Method OnCreate()
        SetUpdateRate(1)
        ' randomize using the time function
        Seed = GetDate[4] + GetDate[5]
        cx = DeviceWidth()/2
        cy = DeviceHeight()/2
        ' Here we create new vectors (a,b)
        ' with random values
        a = New vectorf(Rnd(-10,10),Rnd(10,10))
        b = New vectorf(Rnd(-5,5),Rnd(5,5))
    End Method
    Method OnUpdate()
        '                 
        time+=1
        If time>5 Then
            time=0
            a = New vectorf(Rnd(-10,10),Rnd(-10,10))
            b = New vectorf(Rnd(-5,5),Rnd(5,5))
        End If
    End Method
    Method OnRender()
        ' pointx and y hold the coordinates
        ' that are used to draw on the screen.
        Local pointx:Int
        Local pointy:Int
        Cls 0,0,0 
        SetColor 255,255,255
        ' Here we draw the helper screen part.
        DrawLine cx,0,cx,DeviceHeight
        DrawLine 0,cy,DeviceWidth,cy
         DrawText "-X",0,cy
         DrawText "+X",DeviceWidth()-30,cy
         DrawText "-Y",cx,0
         DrawText "+Y",cx,DeviceHeight()-30
         DrawText "0,0",cx,cy,.5,.5
         DrawText "Origin",cx+5,cy-20
        '
        ' Here we draw the vector a.
        SetColor 255,255,255
        pointx = (a.x*13) + cx
        pointy = (a.y*13) + cy
        DrawCircle pointx,pointy,7
        DrawLine cx,cy,pointx,pointy
        DrawText "a",pointx,pointy,.5,.5
        '
        ' Here we draw the vector b.
        SetColor 255,255,255
        pointx = (b.x*13) + cx
        pointy = (b.y*13) + cy
        DrawCircle pointx,pointy,7
        DrawLine cx,cy,pointx,pointy
        DrawText "b",pointx,pointy,.5,.5
        '
        ' Here we create the projected vector
        ' We project vector b onto a.
        Local c:vectorf = project_vector(b,a)
        '
        ' Here we draw the projected vector
        SetColor 255,255,0
        pointx = (c.x*13) + cx
        pointy = (c.y*13) + cy
        DrawCircle pointx,pointy,7
        DrawLine cx,cy,pointx,pointy
        DrawText "c",pointx,pointy,.5,.5        
        
        '        
        ' Here we draw the Screen info
        SetColor 255,255,255
        Scale 1.2,1.2
        SetAlpha 1
        '
        ' String(mystring)[0..4] creates a string with 4 
        ' characters. [ 0,1,2,3 ] Left to right.
        '
        DrawText "Vector Projection b onto a",0,0
        DrawText "A projection is a vector mapped onto another vector.",0,20
        DrawText "",0,40
        SetAlpha 0.6
        DrawText "Vector Projection :",cx-30,DeviceHeight-160
        DrawText "d = dot_product(a,a)",cx-20,DeviceHeight-140
        DrawText "dp = dot_product(b,a)",cx-20,DeviceHeight-120
        SetColor 255,255,0
        DrawText "c = multiply_vector(a,dp/d)",cx-20,DeviceHeight-100

    End Method
End Class

' This function return the dot product of two vectors.
' It returns a value.
'
Function dot_product:Float(a:vectorf,b:vectorf)
    Return a.x * b.x + a.y * b.y 
End Function

' This function multiplies a vector.
' It returns a vector.
'
Function multiply_vector:vectorf(v:vectorf,scalar:Float)
    Local r:vectorf = New vectorf()
    r.x = v.x * scalar
    r.y = v.y * scalar
    Return r
End Function

' This function maps a vector onto the input vector.
' It returns a vector
'
Function project_vector:vectorf(project:vectorf, onto:vectorf)
    ' d is a float variable that holds the dot product
    ' of onto,onto
    Local d:Float = dot_product(onto,onto)
    If (0<d)
        ' dp is a variable that holds the dot product
        ' of project and onto
        Local dp:Float = dot_product(project, onto)
        Return multiply_vector(onto,dp/d)
    End If
    Return onto
End Function

Function Main()
    New MyGame()
End Function

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.