# Hello, how to calculate a slider value

**URL:** https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554
**Category:** Coding Questions
**Created:** [June 8, 2024, 9:14am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554 "2024-06-08T09:14:45Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![zlfp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zlfp/32/19543_2.png) [@zlfp](https://discourse.processing.org/u/zlfp)
#### Post date: [June 8, 2024, 9:14am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/1 "2024-06-08T09:14:45Z")

</div>

so, umm i have code

```auto
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](https://processing.org/examples/scrollbar.html) is not for me  
its too uncustomizable

so can you help me please?

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 8, 2024, 3:27pm UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/2 "2024-06-08T15:27:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zlfp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zlfp/32/19543_2.png) [@zlfp](https://discourse.processing.org/u/zlfp)
#### Post date: [June 9, 2024, 8:12am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/3 "2024-06-09T08:12:51Z")

</div>

> 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

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [June 9, 2024, 12:34pm UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/4 "2024-06-09T12:34:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 9, 2024, 1:37pm UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/5 "2024-06-09T13:37:27Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zlfp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zlfp/32/19543_2.png) [@zlfp](https://discourse.processing.org/u/zlfp)
#### Post date: [June 9, 2024, 2:01pm UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/6 "2024-06-09T14:01:27Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 9, 2024, 2:26pm UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/8 "2024-06-09T14:26:04Z")

</div>

I think you are wrong… 😉

---

<div class="post-metadata">

### Author: ![ProcessingOrg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/processingorg/32/18341_2.png) [@ProcessingOrg](https://discourse.processing.org/u/ProcessingOrg)
#### Post date: [June 11, 2024, 9:12am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/9 "2024-06-11T09:12:53Z")

</div>

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”:

- [Guidelines—Asking Questions - #8](https://discourse.processing.org/t/guidelines-asking-questions/2147/8)
- [FAQ - Processing Foundation](https://discourse.processing.org/faq)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 11, 2024, 10:22am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/10 "2024-06-11T10:22:30Z")

</div>

my version

```auto

// 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);
  }
}

```

---

<div class="post-metadata">

### Author: ![zlfp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zlfp/32/19543_2.png) [@zlfp](https://discourse.processing.org/u/zlfp)
#### Post date: [June 11, 2024, 10:55am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/11 "2024-06-11T10:55:37Z")

</div>

no, see this

```auto
  void drag() {
    if ((mouseX > px && mouseX <= px + line.width) && (mouseY > py && mouseY <= py + line.height) && mousePressed) {
      bpx = mouseX; // <- bpx is a x of ball
    }
  }

```

---

<div class="post-metadata">

### Author: ![zlfp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zlfp/32/19543_2.png) [@zlfp](https://discourse.processing.org/u/zlfp)
#### Post date: [June 11, 2024, 11:00am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/12 "2024-06-11T11:00:23Z")

</div>

```auto
Slider slider = new Slider(100, 300,
  1,
  0, 50);

// my brain is not braining about this

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 11, 2024, 11:35am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/13 "2024-06-11T11:35:19Z")

</div>

> [@zlfp](#):
>
> no, see 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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 11, 2024, 11:37am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/14 "2024-06-11T11:37:14Z")

</div>

> [@zlfp](#):
>
> `// my brain is not braining about this`

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

The comments are in the class :

> [@Chrisir](#):
>
> ```auto
> // 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()
> 
> ```

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 11, 2024, 9:36pm UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/15 "2024-06-11T21:36:12Z")

</div>

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:

> [@zlfp](#):
>
> ```auto
> int px, py, ipp, bpx;
> 
> float calcNumb() {
> return (bpx - px) / ipp; //there is problem
> }
> 
> ```

Discussed here:

_[Troubleshooting · benfry/processing4 Wiki · GitHub](https://github.com/benfry/processing4/wiki/Troubleshooting#why-does-2--5--0-instead-of-04)_ \< Why does 2 / 5 = 0 instead of 0.4?

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

`:)`

---

<div class="post-metadata">

### Author: ![zlfp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zlfp/32/19543_2.png) [@zlfp](https://discourse.processing.org/u/zlfp)
#### Post date: [June 12, 2024, 6:13am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/16 "2024-06-12T06:13:33Z")

</div>

i need PImages

the question is there:

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

```

what are you looking about?

---

<div class="post-metadata">

### Author: ![zlfp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zlfp/32/19543_2.png) [@zlfp](https://discourse.processing.org/u/zlfp)
#### Post date: [June 12, 2024, 6:35am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/17 "2024-06-12T06:35:05Z")

</div>

oh, i got it, sorry

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

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 12, 2024, 6:38am UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/18 "2024-06-12T06:38:05Z")

</div>

> [@zlfp](#):
>
> ` float calcNumb() {`

I suggest to use calcNumb2

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 12, 2024, 12:28pm UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/19 "2024-06-12T12:28:19Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zlfp](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/zlfp/32/19543_2.png) [@zlfp](https://discourse.processing.org/u/zlfp)
#### Post date: [June 12, 2024, 8:02pm UTC](https://discourse.processing.org/t/hello-how-to-calculate-a-slider-value/44554/20 "2024-06-12T20:02:40Z")

</div>

oh, i dont thinking that, thank’s
