map() producing out of range result

boxxofrobots

Well-known member
I have a simplex noise generator that has been shown to reliably produce values between -1 and 1, yet when I run this I get values out of range. Can someone explain what I might be doing wrong?

Code:
#include "perlinFunctions.h"
#include "SimplexNoise.h"
#include <Arduino.h>

extern SimplexNoise perlin; // Declare the perlin object

void getPerlinDisplayValues(double xSize, double ySize, double xOffset, double yOffset, double scale, uint8_t* tableData) {
  double startX = xOffset - ((xSize / 2) * scale);
  double startY = yOffset + ((ySize / 2) * scale);

  for (int y = 0; y < ySize; y++) {
    for (int x = 0; x < xSize; x++) {
      double perlinX = startX + (x * scale);
      double perlinY = startY - (y * scale);
      uint8_t noiseValue = map(perlin.noise(perlinX, perlinY),-1,1,0,255);
      int index = y * xSize + x;
      tableData[index] = noiseValue;
    }
  }
}

It is invoked using this:

Code:
  uint8_t* displayValues = new uint8_t[xDisplay * yDisplay];
  
  perlin.init(3); // ALWAYS CALL PERLIN.INIT BEFORE CALLING PERLIN.NOISE!

  startTime = micros();

  getPerlinDisplayValues(xDisplay, yDisplay, xOffset, yOffset, scale, displayValues);

  printElapsedTime();
 
You might try adding a constrain() to the value returned from perlin.noise() to ENSURE that the value is between -1 and 1
 
Thanks. I guess I should delete this thread, I got it figured out a few minutes ago, turns out it was an error where I was calling String(thisnumber,3)! Doh! Got rid of the ,3 and that fixed it.
 
Back
Top