About Monkey 2 › Forums › Monkey 2 Code Library › [Math] RemapValue
This topic contains 2 replies, has 2 voices, and was last updated by
cocon 1 year, 4 months ago.
Viewing 3 posts - 1 through 3 (of 3 total)
-
AuthorPosts
-
June 17, 2017 at 7:37 am #8791
This is a very important and useful function that will allow you to remap a value from the source range to the target range.
Some examples of using would be for converting float values 0..1 to bytes, or when dealind with joystick input -1..1 to a game value n..n, etc.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657#Import "<mojo>"Using mojo..Using std.graphics#rem monkeydoc Remaps a value from the source range to the target range.The first range is the initial value range.The second range is the target value range.@param firstStart The start value of the first range.@param firstStop The stop value of the first range.@param secondStart The start value of the second range.@param secondStop The stop value of the second range.@return value remapped to the target value range of [secondStart, secondStop].#endFunction Remap:Float(value:Float, firstStart:Float, firstStop:Float, secondStart:Float, secondStop:Float)Return secondStart + (secondStop - secondStart) * ((value - firstStart) / (firstStop - firstStart))EndClass MyWindow Extends WindowMethod OnRender(canvas:Canvas) OverrideLocal position := Remap(Mouse.X, 0, Width, 100, 400)Local rotation := Remap(Mouse.X, 0, Width, 0, Pi/2)Local color := Remap(Mouse.X, 0, Width, 1, 0)App.RequestRender()canvas.Clear(Color.Black)canvas.Color = Color.Whitecanvas.DrawText("Input Value:" + Mouse.X, 0, 15*1, 0, 0.5)canvas.DrawText("Position:" + position, 0, 15*2, 0, 0.5)canvas.DrawText("Rotation:" + rotation, 0, 15*3, 0, 0.5)canvas.DrawText("Color:" + color, 0, 15*4, 0, 0.5)canvas.PushMatrix()canvas.Translate(position, position)canvas.Rotate(rotation)canvas.Color = New Color(1, color, color)canvas.DrawRect(0, -0.5, 100, 100)canvas.PopMatrix()EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndDecember 15, 2017 at 4:27 am #12295Formatting of the post is broken.
December 15, 2017 at 8:39 am #12299Thanks for mentioning it, I removed the “grave accent” character from doc variables.
(Probably I will have to scroll my posts and fix them all
)
-
AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic.