0

I've recently saw this code in a discord server:

const image = Image.new(1024, 1024)
const a = 32
let g = (b, c) => b+a * c+a // what's the math behind this? b and c is undefined.
image.fill(g)

(image is an image manipulation module)

My question is, how the variable g works when it generates the image? Image.fill() goes through the each pixel of the image and set a color, but I don't understand the math behind let g = (b, c) => b+a * c+a

Lmao 123
  • 111
  • 1
  • 8
  • `b` and `c` are *parameters*. They will not be `undefined` - their value would be whatever is passed into the `g` function. For example `g(4, 2)` -> `b = 4`, `c = 2` – VLAZ Jun 20 '20 at 14:25
  • Thanks, but when I fill the image, i only fill ''g''. Also I declare them ` => b+a * c+a` and I don't understand the math behind that too. How can it calculate b+a * c+a if b and c is undefined at that moment and time? – Lmao 123 Jun 20 '20 at 14:27
  • You *don't* calculate them at that moment in time. `g` is a function, it takes two parameters - `b`, and `c`. Once `g` is *executed* (which `.fill` should do) it will be supplied by some values (again, `.fill` must be doing this). So, when executed *with* arguments, it will perform the calculation. – VLAZ Jun 20 '20 at 14:29
  • 1
    `g` is a function. `b` and `c` are parameters it will receive its first two arguments in when/if it's called. Its body takes `a` (which the function closes over) and uses it with the `b` and `c` parameters to create a number. `image.fill(g)` is calling `image.fill` and passing in the function. Apparently, `fill` calls the function with values for `b` and `c` (my guess is they're `x` and `y` coordinates; very oddly-named parameters). (It would help if you told us what `Image` is, since it clearly isn't the browser's built-in `Image` function.) – T.J. Crowder Jun 20 '20 at 14:31

0 Answers0