MyBASIC2 Script Example - Distance Travelled

' This sample calculates distance travelled of Point #1

 

OPTION EXPLICIT ON

 

DIM nPoints AS INTEGER

DIm nNewPoint AS INTEGER

DIm nFrames AS INTEGER

DIM i AS INTEGER

 

' Get No Of Points and Frames

 

nPoints = Video.GetNoOfPoints()

nFrames = Video.GetNoOfFrames()

 

' Make sure we don't have any invalid points

 

FOR i = 1 TO nPoints

  Point.Interpolate( i, 30 )

ENDFOR

 

' Create a new point if we don't already have it

 

nNewPoint = Point.GetNumber( "Point #1.Travel" )

 

IF nNewPoint < 1 THEN

  nNewPoint = nPoints + 1

  Video.SetNoOfPoints( nNewPoint )

  Point.SetName( nNewPoint, "Point #1.Travel" )

ENDIF

 

' Make the new point the same as Point #1 but make it ahead one frame

' so we can use this to get the distance travelled between 2 frames

' Note; We're only doing that for Point #1 in this example

 

Point.Copy( 1, 1, nNewPoint, 2, nFrames - 1 )

 

' If we have set T0 we should set the first frame to invalid so that

' we don't have any travelled distance until 2nd frame

 

IF Video.GetT0() > 1 THEN

  Point.set( Video.GetT0(), nNewPoint, 2, 0 )

ENDIF

 

' Delete distance in case it already exists

 

Distance.Delete( 1 )

 

' Create distance tool between Point #1 and new Point

 

Distance.Create( 2, "Point #1.Travel", 1, nNewPoint )

 

' Now, simply add all the distances between T0 and T1 to get the

' total distance travelled. 

' NOTE; You can also export the distance and do this in Excel etc.

 

DIM fTotalDistance AS REAL

fTotalDistance = 0;

 

FOR i = Video.GetT0() + 1 To Video.GetT1()

  fTotalDistance = fTotalDistance + Distance.Get( i, Distance.GetNumber( "Point #1.Travel" ) )

ENDFOR

 

DIM msg AS STRING

 

msg = FORMAT( "Total Distance Travelled = %1.3f %s", fTotalDistance, Video.GetUnits( "Pixels" ) )

App.MsgBox( msg, 0 )

 

END