1

I am creating a slider control, I have problems setting the value though

_x = 512;
_y = 256;
_width = 128;
_height = 12;
_min = 0;
_max = 10;

I do something like,

// set the value
_input = 5;
_value = median(_x-(_width/2)+(_input/_max),_x-((_width/2)),_x+((_width/2)))

Setting the _input to 5 should put the slider in the middle, but it doesn't, I think I have the calculation wrong in some way.

_x-(_width/2) will give you 0 in percent, 0 in value

_x+(_width/2) will give you 100 in percent, 10 in value. The max.

user780756
  • 1,376
  • 3
  • 19
  • 31
  • what about _value = _x + floor(_input / _max * _width) – graywolf Jul 19 '14 at 19:04
  • Didn't work :C Doing something like _value = _x + floor(7 / _max * _width); gives me 11.95 – user780756 Jul 19 '14 at 19:20
  • I don't see how it could.. floor() should return whole number and in _x you probably have whole number too. So it really shouldn't be 11.95.. _value = _x + floor(7 / _max * _width) = 512 + floor(7 / 10 * 128) = 512 + 89 = 601 – graywolf Jul 20 '14 at 16:45
  • I worked on it since then and finally resolved it. Seems I pretty much had to map the range to the min/max. Followed the solution here http://stackoverflow.com/questions/5731863/mapping-a-numeric-range-onto-another Thanks in any case. – user780756 Jul 21 '14 at 05:49

1 Answers1

0

Mapping x0 € [_x-_width/2 .. _x+_width/2] to sl € [0..10] linearly and vica versa is not a big deal.

If having x0: offset of -(_x-_width/2) transforms it to 0.._width, so a scale of 10/_width is needed for 0..10:

sl == ( x0 - (_x-_width/2) ) * 10 / _width    // float div unless 10 | _width

Reversed:

x0 == (_x-_width/2) + sl * _width / 10

Take care of int vs. float divisions and rounding (+.5).

renonsz
  • 571
  • 1
  • 4
  • 17