- Joined
- Jan 2, 2020
- Messages
- 1,414
I'm converting a touch routine that I'm very happy with into Cerberus because I think it would be a great demo what Cerberus can do using very little code! Right now it's a bit off, but it's WIP
EDIT btw this is on 9yrs old hardware and you get amazing speed on anything. It just feels good.
EDIT btw this is on 9yrs old hardware and you get amazing speed on anything. It just feels good.
Code:
Import mojo2
Function Main()
New MyApp
End Function
Class MyApp Extends App
Field g:Bool
Field g0Matrix : Matrix
Field g1Matrix : Matrix
Field canvas : Canvas
Method OnCreate()
canvas = New Canvas
SetSwapInterval 1 ; SetUpdateRate 0
g0Matrix = New Matrix()
g1Matrix = New Matrix()
End Method
Method OnRender()
canvas.Clear 0,0,1
canvas.PushMatrix
If Not g And TouchDown(0) And TouchDown(1)
' g0 = -g1 * GestureMatrix() ; g0 = -g0
' -------------- Converision try#1 of above line -------------
g0Matrix.setValues g1Matrix.mA,g1Matrix.mB,g1Matrix.mC,g1Matrix.mD,g1Matrix.mTx,g1Matrix.mTy
g0Matrix.invert
GestureMatrix(g0Matrix)
g0Matrix.invert
' --------------------------------------------------------------
g = True
Endif
If g And TouchDown(0) And TouchDown(1)
' g1 = GestureMatrix() * g0
' ------------- Converision try#1 of above line -------------
g1Matrix.setValues 1.0,0.0,0.0,1.0,0.0,0.0
GestureMatrix(g1Matrix)
g1Matrix.concatMatrix(g0Matrix)
' --------------------------------------------------------------
Else
g= False
Endif
canvas.SetMatrix g1Matrix.mA,g1Matrix.mB,g1Matrix.mC,g1Matrix.mD,g1Matrix.mTx,g1Matrix.mTy
For Local tempx := 0 To 1440 Step 32
canvas.DrawLine(tempx-200,0,tempx-20,200)
Next
canvas.PopMatrix
canvas.Flush
End Method
Method GestureMatrix(matrix:Matrix)
Local p0x:Float = TouchX(0)
Local p0y:Float = TouchY(0)
Local p1x:Float = TouchX(1)
Local p1y:Float = TouchY(1)
Local dx:Float = p1x-p0x
Local dy:Float= p1y-p0y
Local s:Float = Sqrt(dx * dx + dy * dy)
Local r:Float = -ATan2(dy,dx)
' --------------------------------------------------------------
matrix.translateBy p0x,p0y ' Try #1
matrix.rotateBy r '
matrix.scaleBy s,s '
' --------------------------------------------------------------
End
End Class
' canvas.SetMatrix g1[0],g1[1],g1[2],g1[3],g1[4],g1[5]
' canvas.Transform g0Matrix.mA,g0Matrix.mB,g0Matrix.mC,g0Matrix.mD,g0Matrix.mTx,g0Matrix.mTy
Class Matrix
Field mA : Float
Field mB : Float
Field mC : Float
Field mD : Float
Field mTx : Float
Field mTy : Float
' Initializes an identity matrix.
Method New()
Self.mA = 1.0
Self.mB = 0.0
Self.mC = 0.0
Self.mD = 1.0
Self.mTx = 0.0
Self.mTy = 0.0
End Method
' Initializes a matrix with the specified components.
Method New(a : Float,b : Float,c : Float,d : Float,tx : Float,ty : Float)
Self.mA = a
Self.mB = b
Self.mC = c
Self.mD = d
Self.mTx = tx
Self.mTy = ty
End Method
Function matrixWithValues : Matrix(a : Float,b : Float,c : Float,d : Float,tx : Float,ty : Float)
Return New Matrix(a,b,c,d,tx,ty)
End Function
Function matrixWithIdentity : Matrix()
Return New Matrix()
End Function
' Setter
Function setValues(matrix : Matrix,a : Float,b : Float,c : Float,d : Float,tx : Float,ty : Float)
matrix.mA = a
matrix.mB = b
matrix.mC = c
matrix.mD = d
matrix.mTx = tx
matrix.mTy = ty
End Function
' Compares two matrices.
Method isEqual : Bool(other : Matrix)
If other = Self Then Return True
Return (other.mA = Self.mA) And (other.mB = Self.mB) And (other.mC = Self.mC) And (other.mD = Self.mD) And (other.mTx = Self.mTx) And (other.mTy = Self.mTy)
End Method
' Clone matrix
Method clone : Matrix()
Return New Matrix(Self.mA,Self.mB,Self.mC,Self.mD,Self.mTx,Self.mTy)
End Method
' Sets a matrix with the with the specified components
Method setValues(a : Float,b : Float,c : Float,d : Float,tx : Float,ty : Float)
Self.mA = a
Self.mB = b
Self.mC = c
Self.mD = d
Self.mTx = tx
Self.mTy = ty
End Method
' Concatenates a matrix with the current matrix, combining the geometric effects of the two.
Method concatMatrix : Void(matrix : Matrix)
Local a : Float = matrix.mA * Self.mA + matrix.mC * Self.mB
Local b : Float = matrix.mB * Self.mA + matrix.mD * Self.mB
Local c : Float = matrix.mA * Self.mC + matrix.mC * Self.mD
Local d : Float = matrix.mB * Self.mC + matrix.mD * Self.mD
Local tx : Float = matrix.mA * Self.mTx + matrix.mC * Self.mTy + matrix.mTx
Local ty : Float = matrix.mB * Self.mTx + matrix.mD * Self.mTy + matrix.mTy
Self.setValues(a,b,c,d,tx,ty)
End Method
' Determinate of matrix
Method determinant : Float()
Return (Self.mA * Self.mD) - (Self.mC * Self.mB)
End Method
' Translates the matrix along the x And y axes.
Method translateBy : Void(dx : Float,dy : Float)
Self.mTx = Self.mTx + dx
Self.mTy = Self.mTy + dy
End Method
' Applies a scaling transformation To the matrix.
Method scaleBy : Void(sx : Float,sy : Float)
Self.mA = Self.mA * sx
Self.mB = Self.mB * sy
Self.mC = Self.mC * sx
Self.mD = Self.mD * sy
Self.mTx = Self.mTx * sx
Self.mTy = Self.mTy * sy
End Method
' Applies a uniform scaling transformation To the matrix.
Method scaleBy : Void(scale : Float)
Self.scaleBy(scale,scale)
End Method
' Applies a rotation on the matrix.
Method rotateBy : Void(angle : Float)
Local rotMatrix : Matrix = New Matrix(Cos(angle),Sin(angle),-Sin(angle),Cos(angle),0,0)
Self.concatMatrix(rotMatrix)
End Method
' Applies a shearing transformation To the matrix.
Method shearBy : Void(sx : Float,sy : Float)
Self.mC = Self.mC + sx
Self.mB = Self.mB + sy
End Method
' Applies a uniform shearing transformation To the matrix.
Method shearBy : Void(shear : Float)
Self.mC = Self.mC * shear
Self.mB = Self.mB * shear
End Method
' Sets each matrix Property To a value that causes a Null transformation.
Method identity : Void()
Self.setValues(1.0,0.0,0.0,1.0,0.0,0.0)
End Method
' Performs the opposite transformation of the matrix.
Method invert : Void()
Local det : Float = Self.determinant()
Local a : Float = Self.mD / det
Local b : Float = -Self.mB / det
Local c : Float = -Self.mC / det
Local d : Float = Self.mA / det
Local tx : Float = ((Self.mC * Self.mTy) - (Self.mD * Self.mTx)) / det
Local ty : Float = ((Self.mB * Self.mTx) - (Self.mA * Self.mTy)) / det
Self.setValues(a,b,c,d,tx,ty)
End Method
' Applies the geometric transformation represented by the matrix To the specified point.
Method transformPoint : Point(point : Point)
Local x : Float = (Self.mA * point.mX) + (Self.mC * point.mY) + Self.mTx
Local y : Float = (Self.mB * point.mX) + (Self.mD * point.mY) + Self.mTy
Return New Point(x,y)
End Method
' Returns a string containing the components of the matrix
Method description : String()
Return "(a = " + Self.mA + "," + "b = " + Self.mB + "," + "c = " + Self.mC + "," + "d = " + Self.mD + "," + "tx = " + Self.mTx + "," + "ty = " + Self.mTy + ")"
End Method
End Class
Last edited: