map() Command with different intervals

Hi,

according to my understanding from the reference page of processing and also experimenting a bit, both lerp() and map() commands work always linearly.

What I mean about that is if I m mapping numbers between 1 and 100 to 1-2; 50 will be always 1.5

Is there a way -- rather a simple way to modify map() command so that is does not map numbers linearly but lets say with a hyperbolic curve?

Once you map values in grasshopper with a graph-mapper it lets you modify the curve. My aim is to understand and also use the map() command to generate different mapping methods.

If anyone did or knows how to edit the map() function like this, I would appreciate the help.

Best, Omer

Tagged:

Answers

  • edited July 2016

    Dunno about hyperMap(), but here's map()'s Java source:
    https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L4811

    static public final float map(float value,
                                  float start1, float stop1,
                                  float start2, float stop2) {
      float outgoing =
        start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
      String badness = null;
      if (outgoing != outgoing) {
        badness = "NaN (not a number)";
    
      } else if (outgoing == Float.NEGATIVE_INFINITY ||
                 outgoing == Float.POSITIVE_INFINITY) {
        badness = "infinity";
      }
      if (badness != null) {
        final String msg =
          String.format("map(%s, %s, %s, %s, %s) called, which returns %s",
                        nf(value), nf(start1), nf(stop1),
                        nf(start2), nf(stop2), badness);
        PGraphics.showWarning(msg);
      }
      return outgoing;
    }
    
  • Just write your own Function

  • Answer ✓

    Is there a way -- rather a simple way to modify map() command so that is does not map numbers linearly but lets say with a hyperbolic curve?

    No the map function is linear and cannot be changed. The only option would be to create your own 'map' function.

    You can do it like this

    void setup() {
      for (float n = 0; n <= 10; n++) {
        print(n );
        print("\t " +  mapSquared(n, 0, 10, 0, 100) );
        println("\t " +  mapCubed(n, 0, 10, 0, 100) );
      }
    }
    
    float mapSquared(float value, float start1, float stop1, float start2, float stop2) {
      float inT = map(value, start1, stop1, 0, 1);
      float outT = inT * inT;
      return map(outT, 0, 1, start2, stop2);
    }
    
    float mapCubed(float value, float start1, float stop1, float start2, float stop2) {
      float inT = map(value, start1, stop1, 0, 1);
      float outT = inT * inT * inT;
      return map(outT, 0, 1, start2, stop2);
    }
    

    Which has the output

    0.0  0.0                     0.0
    1.0  0.50000006  0.05
    2.0  2.0000002   0.4
    3.0  4.5                     1.3500001
    4.0  8.000001            3.2
    5.0  12.5                    6.25
    6.0  18.0                    10.800001
    7.0  24.499998   17.15
    8.0  32.000004   25.6
    9.0  40.499996   36.449997
    10.0     50.0                    50.0
    
  • Hi!

    thank you for the answers.

    I was trying to map the already mapped numbers, thinking that is would be squared; makes much more sense this way.

    I am slowly starting to understand what is going on in this example code. Thank you very much!

  • edited July 2016

    float inT = map(value, start1, stop1, 0, 1); can be replaced w/:
    float inT = norm(value, start1, stop1); *-:)

    https://Processing.org/reference/norm_.html

  • quark's approach with little graphic and more functions

    void setup() {
      size(700, 700); 
    
      float factor = 5.0; 
      float max = 100.0;// initial range max 
    
      for (float n = 0; n <= max; n++) {
        print(n );
    
        // normal map 
        fill(2, 2, 255); //BLUE
        ellipse(n*factor+10, height-20-factor*map(n, 0, max, 0, 100), 5, 5); 
        print("\t " +  map(n, 0, max, 0, 100) );
    
        // map Squared
        fill(255, 2, 2); //RED
        ellipse(n*factor+10, height-20-factor*mapSquared(n, 0, max, 0, 100), 5, 5); 
        print("\t " +  mapSquared(n, 0, max, 0, 100) );
    
        // map Cubed
        fill(2, 255, 2);//GREEN 
        ellipse(n*factor+10, height-20-factor*mapCubed(n, 0, max, 0, 100), 5, 5); 
        println("\t " +  mapCubed(n, 0, max, 0, 100) );
    
        // map New I 
        fill(2, 255, 255); 
        ellipse(n*factor+10, height-20-factor*mapNew(n, 0, max, 0, 100), 5, 5); 
        println("\t " +  mapNew(n, 0, max, 0, 100) );
    
        // map New II 
        fill(255, 255, 255); 
        ellipse(n*factor+10, height-20-factor*mapNew2(n, 0, max, 0, 100), 5, 5); 
        println("\t " +  mapNew2(n, 0, max, 0, 100) );
      } // for 
    
      // Coord --- 
      float n=0; 
      stroke(255); 
    
      line (n*factor+10, height-20-0, 
        width-66, height-20-0 );
    
      line (n*factor+10, height-20-0, 
        n*factor+10, 66);
    }
    
    float mapSquared(float value, float start1, float stop1, float start2, float stop2) {
      float inT = map(value, start1, stop1, 0, 1);
      float outT = inT * inT;
      return map(outT, 0, 1, start2, stop2);
    }
    
    float mapCubed(float value, float start1, float stop1, float start2, float stop2) {
      float inT = map(value, start1, stop1, 0, 1);
      float outT = inT * inT * inT;
      return map(outT, 0, 1, start2, stop2);
    }
    
    float mapNew(float value, float start1, float stop1, float start2, float stop2) {
      float inT = map(value, start1, stop1, -1, 1);
      float outT = inT * inT;
      return map(outT, 0, 1, start2, stop2);
    }
    
    float mapNew2(float value, float start1, float stop1, float start2, float stop2) {
      float inT = map(value, start1, stop1, -1, 1);
      float outT = inT * inT;
      return stop2- map(outT, 0, 1, start2, stop2);
    }
    // 
    
  • new version:

    float factor = 5.0; 
    
    void setup() {
      size(700, 700); 
    
      float max = 100.0;// initial range max 
      float result; 
    
      for (float n = 0; n <= max; n++) {
        println(n);
    
        // normal map()
        result = map(n, 0, max, 0, 100) ; 
        output(n, result, color(2, 2, 255)); // BLUE 
    
        // map Squared
        result = mapSquared(n, 0, max, 0, 100);
        output(n, result, color(255, 2, 2)); // RED 
    
        // map Cubed
        result = mapCubed(n, 0, max, 0, 100);
        output(n, result, color(2, 255, 2));//GREEN 
    
        // map New I (an U-like parabole)
        result = mapNew(n, 0, max, 0, 100);
        output(n, result, color(2, 255, 255));//
    
        // map New II (an inverse U-like parabole)
        result = mapNew2(n, 0, max, 0, 100);
        output(n, result, color(255, 2, 255));// 
        println("");
      } // for 
    
      // Coord --- 
      coordSystem();
    }
    
    void draw() {
      //
    }
    
    void output(float n, float resultLocal, color colLocal) {
      fill(colLocal); //COLOR 
      ellipse(n*factor+10, height-20-factor*resultLocal, 5, 5); 
      print("\t " +  resultLocal);
    }
    
    void coordSystem() {
      // Coord --- 
      float n=0; 
      stroke(255); 
    
      line (n*factor+10, height-20-0, 
        width-66, height-20-0 );
      fill(255); 
      text("x", width-66+6, height-20-0+4);
    
      line (n*factor+10, height-20-0, 
        n*factor+10, 66);
      fill(255); 
      text("y", n*factor+10-2, 66-6);
    
      // vertical center line |
      stroke(155); 
      n=100.0;
      float xValue=n*factor+10;
      xValue=xValue/2; 
      line (xValue, height-12, 
        xValue, 155);
    }
    
    float mapSquared(float value, float start1, float stop1, float start2, float stop2) {
      float inT = map(value, start1, stop1, 0, 1);
      float outT = inT * inT;
      return map(outT, 0, 1, start2, stop2);
    }
    
    float mapCubed(float value, float start1, float stop1, float start2, float stop2) {
      float inT = map(value, start1, stop1, 0, 1);
      float outT = inT * inT * inT;
      return map(outT, 0, 1, start2, stop2);
    }
    
    float mapNew(float value, float start1, float stop1, float start2, float stop2) {
      float inT = map(value, start1, stop1, -1, 1);
      float outT = inT * inT;
      return map(outT, 0, 1, start2, stop2);
    }
    
    float mapNew2(float value, float start1, float stop1, float start2, float stop2) {
      float inT = map(value, start1, stop1, -1, 1);
      float outT = inT * inT;
      return stop2- map(outT, 0, 1, start2, stop2);
    }
    // 
    
Sign In or Register to comment.