Login
Community
DARK THEME

Mix() and Clamp() functions

So, I was working on my raymarcher-in-progress and I remembered some interesting functions that I thought would be good to add into microstudio! These would work similarly to the min() or max() functions.

1: Mix() function

This function would take 3 arguments, and would basically mix the first two values according to the third value. The code would look like this:

mix=function(x,y,a)
  output=x*(1-a)+y*a // 'A' would be a value from 0-1. Also, please note that similarly to the min() function it would not actually   
  give a variable output
end

2: Clamp() function

This function would also take 3 arguments, and would basically clamp the first value between the second value. The code would look like this:

clamp=function(x,bottom,top) // Clamps x value between bottom and top value
  output=max(x,bottom)
  output=min(output,top)
  if bottom>top then
    // This would be an error
  end
end

And then you could use it like this:

variable=clamp(variable,0,1)-mix(a,b,0.2)

I mean clamp already exists in mrLman's games-prog library.

As Crafter says. Quite easy to have a collection of helper functions tailored to your needs.

What would be useful would be if we could keep those snippets somewhere easily accessible. Like a library tab, or something like that, where they are available project wide.

In FlowLab every user has his own 'Bundle' folder for that purpose, soooooo handy.

Another one I miss a lot is fmod ; I often reimplement it in many projects I make.

fmod = function(x,y)
  x-floor(x/y)*y
end

I think clamp and mix would be nice additions as well. I wouldn't bother having an error case in clamp though, I would probably stick on the GLSL implementation: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/clamp.xhtml

P.S. your mix() looks like what is commonly known as lerp() function.

https://processing.org/reference/lerp_.html

@gilles ... trying to wrap my head around fmod() ...

What is the difference to the modulus operator? (x%y)

@TinkerSmith well first I called it fmod which is misleading, I should call it simply mod for clarity.

I just dug the subject a bit and found out the % operator in microScript, just like in JavaScript, operates like a remainder operator rather than a modulo operator. You will only see a difference when using the operators with negative values. Let's call the modulo operator mod and the remainder operator rem:

13 mod 10 = 3
13 rem 10 = 3 // same

but:

-13 mod 10 = 7
-13 rem 10 = -3

:embed https://microstudio.io/gilles/modulovsremainder/

Source: https://2ality.com/2019/08/remainder-vs-modulo.html

@tinkersmith I think a library of these functions would be a great idea! Maybe in the documentation tab? We could also do a distance function and a dot product function.

Also, yes, something like the 'lerp' function would work perfectly.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community