About Monkey 2 › Forums › Monkey 2 Code Library › Rotating a rectangle using the matrix
Tagged: Rotation example
This topic contains 3 replies, has 3 voices, and was last updated by 
 nerobot 1 year, 4 months ago.
- 
		AuthorPosts
 - 
		
			
				
November 19, 2017 at 8:56 pm #11832
The following example shows how to rotate a rectangle without any trigonometry.
It uses only matrix hardware commands (translate & rotate) and can be used to rotate sprites drawn using DrawRect, which has no inbuilt rotation parameter.
[/crayon]Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455[crayon-5cb9cc1cc52c3666947540 inline="true" ]Namespace Myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class Myapp Extends WindowMethod New()Super.New("Window",1920,1080,WindowFlags.Resizable)EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()' **************************************************************' Using matrix to rotate rectangle'' Rectangle (x,y,w,h) x,y refers to the middlepointLocal r:Float = Millisecs()*.0003 ' Rotation in radiansLocal x:Float = App.MouseLocation.x ' x position of rectangleLocal y:Float = App.MouseLocation.y ' y position of rectangleLocal w:Int = 500 ' Width of rectangleLocal h:Int = 100 ' Height of rectangleLocal rx :Float = -(w * 0.5) ' Calculate the handle for x (0.5 = middle)Local ry :Float = -(h * 0.5) ' Calculate the handle for y (0.5 = middle)canvas.PushMatrix()canvas.DrawLine(x,0,x,1080-1)canvas.DrawLine(0,y,1920-1,y)' canvas.Translate((-x-w)/2.0,(-y-h)/2.0)' canvas.Rotate(r)' canvas.Translate((x+w)/2.0,(y+h)/2.0)' canvas.DrawRect(0,0,w,h)canvas.Translate(x,y)canvas.Rotate(r)canvas.DrawRect(rx,ry,w,h)canvas.PopMatrix()' **************************************************************End MethodEndFunction Main()New AppInstanceNew MyappApp.Run()End FunctionNovember 20, 2017 at 3:37 am #11842The same result via canvas extension:
Monkey123456789101112131415161718192021Class Canvas Extension' func - is a function that will do needed drawing stuff' it is affected with translate/rotate/scaleMethod DrawFunc( func:Void(canvas:Canvas),x:Float,y:Float,rot:Float=0,sx:Float=1,sy:Float=1 )Local canvas:=Selfcanvas.PushMatrix()canvas.Translate( x,y )canvas.Rotate( rot )canvas.Scale( sx,sy )' call our drawing functionfunc( canvas )canvas.PopMatrix()EndEndand adapted buildable example:
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879Namespace Myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class Myapp Extends WindowMethod New()Super.New( "Window",800,600,WindowFlags.Resizable )EndMethod OnRender( canvas:Canvas ) OverrideIf Keyboard.KeyReleased(Key.Escape) Then App.Terminate()Local x:Float = App.MouseLocation.x ' x position of rectangleLocal y:Float = App.MouseLocation.y ' y position of rectangleLocal r:Float = Millisecs()*.0003 ' Rotation in radiansLocal sx:Float = Cos( Millisecs()*.0003 ) ' x scaleLocal sy:Float = Sin( Millisecs()*.0003 ) ' y scale' draw cross at cursorcanvas.DrawLine( x,0,x,Self.Height )canvas.DrawLine( 0,y,Self.Width,y )' use our extension function herecanvas.DrawFunc( DrawAll,x,y,r,sx,sy )App.RequestRender()End' all our drawing logic that will be translated/rotated/scaled' here: draw single rectangleMethod DrawAll( canvas:Canvas )Local w:Int = 500 ' Width of rectangleLocal h:Int = 100 ' Height of rectangleLocal rx :Float = -(w * 0.5) ' Calculate the handle for x (0.5 = middle)Local ry :Float = -(h * 0.5) ' Calculate the handle for y (0.5 = middle)canvas.DrawRect(rx,ry,w,h)EndEndClass Canvas Extension' func - is a function that will do needed drawing stuff' it is affected with translate/rotate/scaleMethod DrawFunc( func:Void(canvas:Canvas),x:Float,y:Float,rot:Float=0,sx:Float=1,sy:Float=1 )Local canvas:=Selfcanvas.PushMatrix()canvas.Translate( x,y )canvas.Rotate( rot )canvas.Scale( sx,sy )' call our drawing functionfunc( canvas )canvas.PopMatrix()EndEndFunction Main()New AppInstanceNew MyappApp.Run()End FunctionNovember 30, 2017 at 7:59 am #12026mmmm.
The first example, nice simple readable code
The second ‘extension’ code is really (sorry to say it) horrible. much more complex, going into the ‘information hiding’ territory.I would have thought that code should be simple (for those) to understand?
November 30, 2017 at 8:08 am #12027I just shown another point of view. I don’t use such code in my projects.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.