Min() max() clamp() resources

Clamp is an Elevator.
The elevator shaft has a bottom. You never want the elevator car to hit the bottom; so you tell the elevator it can never go below the bottom floor.
The elevator shaft has a top; you never want the elevator car to hit the gearworks at the top of the shaft, so you tell the elevator it can never go above the top floor.

Where the elevator is at any point between those two values depends on the users pushing buttons (or for the sake of argument, entering numbers!) into the elevator. (Technically, the translation of this is the browser, but thats “the user” from the coder’s perspective).

You dont care where the elevator car is at any particular moment; or what floor the user wants to go to, as long as its between the top and the bottom. If the user tries to send the car through the bottom of the shaft, you stop them at the ground floor. If the user tries to send the car through the top of the shaft, you stop them at the top floor.

MOST of the time, you’ll see the value inbetween the bottom and the top being linear, because most of the time, you’re using a linear scaling thing like width. But it doesnt matter what the function between the two actually looks like; as long as it is bounded by min and max, it could do anything.

clamp(-0.5,sin(x),0.5);

3 Likes

(also for the record nothing prevents min or max from being non-constant. The graph gets a little silly, and the analogy turns into willie wonka’s elevator, but…)

2 Likes

i came across a term i don’t understand

i saw it in an article above

return

how does return work in clamp()

i know clamp() is a function

how does return work in a function ?

this is a tough concept for me, so please be clear :slight_smile:

i am curious now, but may return here for more clarification when i get into JavaScript

i posted the returns topic over a year ago

i looked it over before sending this reply

please help me out

thanks!!

return ends the function and returns a value to the calling function.

(Pseudocode)
function x($input) {
  if ($input == 1) {
    return 0;
  } else {
    return $input * 2;
  }
}

If I call $y = x(1), $y will have a value of 0 in it. If I call $z = x(2), $z will have a value of 4 in it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.