Hello, how to calculate a slider value

so, umm i have code

class slide {
  PImage ball, line;
  int px, py, ipp, bpx;
  
  slide(PImage sb, PImage sl, int spx, int spy, int sipp) {
    ball = sb;
    line = sl;
    px = spx;
    py = spy;
    ipp = sipp;
    
    bpx = spx;
  }
  
  void drag() {
    if ((mouseX > px && mouseX <= px + line.width) && (mouseY > py && mouseY <= py + line.height) && mousePressed) {
      bpx = mouseX;
    }
  }
  
  void dragWioutPressed() {
    if ((mouseX > px && mouseX <= px + line.width) && (mouseY > py && mouseY <= py + line.height)) {
      bpx = mouseX;
    }
  }
  
  private float calculateTheMassOfTheSun() {
    return 1.989E30;
  }
  
  float calcNumb() {
    return (bpx - px) / ipp; //there is problem
  }
  
  void img() {
    image(line,px,py);
    image(ball,bpx,py);
  }
}

the ipp is section count

anyways i need to calculate the value of pozition of ball width one formula

and, no
this Scrollbar / Examples / Processing.org is not for me
its too uncustomizable

so can you help me please?

I’m unable to run the code that you posted.

I’m unable to run the code that you posted.

this is processong 4.3 btw

so if you use 3.xx or 3.x sure you cant run it

I’m using Processing 4.3. All that you posted is a class; there is no setup() or draw(). It also apparently requires two images which we don’t have.

px,py is the position of the slider (its left end)

bpx is the ball position (current slider
Value)

The value returned is the slider value minus slider x position.
So just the value.
For ipp you can say 1 then the pixels are just the value.

otherwise ipp = how many sections/ resolution you
have on the slider

1 Like
  1. px py is right end of the slider
  2. bpx is just x of ball not a current slider Value
  3. i need to how calculate the slider Value

please SEE the code before posting comment (no offence)

1 Like

I think you are wrong… :wink:

1 Like

Hello @zlfp! To help the community better understand your question and provide more relevant advice, please provide a small self-contained sketch that demonstrates the issue you are facing.

A message was hidden from this thread. For more information about forum rules, please read the following, especially the FAQ section “Be Agreeable, Even When You Disagree”:

my version



// make slider; explanation see class
Slider slider = new Slider(100, 300,
  1,
  0, 50);

// --------------------------------------------------------------------------------

void setup() {
  size(640, 360);
  frameRate(30);
}

void draw() {
  background(0);

  slider.display();
  slider.drag();

  fill(255);
  float num = slider.calcNumb2();
  text ("result: "
    +num
    +"     \n"
    +int(num), 200, 200);
}

// =================================================================================

class Slider {

  //PImage ball, line;
  int px, py,
    ipp, bpx;

  float width1=200;

  float min1;
  float max1;

  // constr
  Slider( int spx, int spy, // pos left corner
    int sipp, // segments, use 1
    float smin, float smax) { // desired min and max for result - see function calcNumb2()

    //    ball = sb;
    //    line = sl;

    px = spx;
    py = spy;
    ipp = sipp;

    min1=smin;
    max1=smax;

    bpx = spx;
  } // constr

  void drag() {
    if ((mouseX >= px && mouseX <= px + width1) &&
      (mouseY > py && mouseY <= py + 30) &&
      mousePressed) {
      bpx = mouseX;
    }
  }

  void dragWioutPressed() {
    if ((mouseX >= px && mouseX <= px + width1) &&
      (mouseY > py && mouseY <= py + 30)) {
      bpx = mouseX;
    }
  }

  private float calculateTheMassOfTheSun() {
    return
      1.989E30;
  }

  float calcNumb2() {
    // new version
    float num =
      (bpx - px) / ipp;
    // using map() command
    num = map(num, 0, width1,
      min1, max1);
    return num;
  }

  float calcNumb() {
    return
      (bpx - px) / ipp; //there is problem
  }

  void display() {
    stroke(255);
    line(px, py, px+width1, py);
    line(px, py+30, px+width1, py+30);
    text("min "
      +min1
      +"; max "
      +max1,
      px+width1+30, py+15);

    // value
    fill(0, 255, 0);
    ellipse( bpx, py+15, 12, 12);
  }
}

1 Like

no, see this

  void drag() {
    if ((mouseX > px && mouseX <= px + line.width) && (mouseY > py && mouseY <= py + line.height) && mousePressed) {
      bpx = mouseX; // <- bpx is a x of ball
    }
  }
Slider slider = new Slider(100, 300,
  1,
  0, 50);

// my brain is not braining about this

I just changed this:
since I don’t use the image “line” I inserted values width1 and 30

That’s all I changed.

When you want a real answer please write what you mean in a longer way and explain it.

1 Like

Here we make a new slider from the
constructor in the class.

The comments are in the class :

  • since we use min and max as the values the slider can return we have better control of the slider result.
1 Like

Hello @zlfp,

You are doing integer math (in your original example) here and it will yield a result that is an integer which is then returned as a float:

Discussed here:

Troubleshooting · benfry/processing4 Wiki · GitHub < Why does 2 / 5 = 0 instead of 0.4?

Try it using floats for the variables so you do floating point math.

:)

1 Like

i need PImages

the question is there:

  float calcNumb() {
    return (bpx - px) / ipp; //there is problem
  }

what are you looking about?

oh, i got it, sorry

slide test1 = new slide(loadImage("Sprite-0012.png"), loadImage("Sprite-0013.png"), 0, 0, 5);
1 Like

I suggest to use calcNumb2

1 Like

You are right, I removed the images.
Because I don’t have them.

My goal was to show a calculation of the return value.

To do this I suggested you pass a lower and upper value to the class which are the minimum and maximum return value of the slider.

Now you can tell the range of the return value.

1 Like

oh, i dont thinking that, thank’s

1 Like