3 个互连的滑块
我已经为这个问题绞尽脑汁两天了,我尝试了不同的方法,但没有一个起作用。我正在构建一个应用程序,它是一种测验。共有三个包含问题的主题。我想使用 3 个滑块来定义他们想要的每个主题的问题百分比。
ex : slider one = History
slider two = Maths
slider three = Grammar
如果我选择拥有更多历史记录,我会向上滑动历史滑块,其他滑块应根据它们必须达到 3 个滑块的 100% 的值而减少...
对算法有什么想法吗?当一个滑块达到零值时会发生什么?
数学从来都不是我的菜。
任何帮助将不胜感激。
提前致谢。
麦克风
I've been racking my brains over this problem for two days, I've tried different things but none of them work. I'm building an app which is a kind of quizz. There are three subjects which contain questions. I would like to use 3 sliders to define the percentage of questions they want on each subject.
ex : slider one = History
slider two = Maths
slider three = Grammar
If I choose to have more history, I slide the history slider up and the other sliders should decrease according to the values they have to reach 100% for the 3 sliders...
Any idea for an algorithm ? And what happens when one slider reach a zero value ?
Maths has never been my scene.
Any Help would be very much appreciated.
Thanks in advance.
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然 Snowangelic 的答案很好,但我认为按如下方式限制不变值之间的比率更有意义。
令s1、s2、s3为滑块的当前值,因此s1+s2+s3=100。您想要求解 n1、n2、n3 滑块的新值,以便 n1+n2+n3=100。假设用户将 s1 更改为 n1。那么这会添加以下约束:
n2/n3 = s2/s3
因此,当 n1+n2+n3=100 时,问题的解决方案是
n2 = (100-n1)/(s3/s2 + 1) 或 0(如果s2=0) 且
n3 = (100-n1)/(s2/s3 + 1) 或 0(如果 s3=0)
Though Snowangelic's answer is good, I think it makes more sense to to constrain the ratio between the unchanged values as follows.
Let s1, s2, s3 be the current values of the sliders, so that s1+s2+s3=100. You want to solve for n1, n2, n3 the new values of the sliders, so that n1+n2+n3=100. Assume s1 is changed to n1 by the user. Then this adds the following constraint:
n2/n3 = s2/s3
So the solution to the problem, with n1+n2+n3=100, is
n2 = (100-n1)/(s3/s2 + 1) or 0 (if s2=0) and
n3 = (100-n1)/(s2/s3 + 1) or 0 (if s3=0)
在 33.333...% 处启动所有三个滑块
当用户将滑块向上移动 10% 时:将其他两个滑块向下移动 5%。但如果两个滑块之一达到 0 =>只动另外百分之十。所以它给出了这样的内容:
另一种可能性是考虑可用资源的总和为 100,资源被分成 n 个桶(在您的情况下为 3)。当用户移动滑块时,他固定了相应桶中的资源数量。因此,您可以从其他存储桶中获取资源,也可以将资源放入其他存储桶中。
您有类似的情况:
假设修改是正数(修改后的存储桶中的新数字比以前更高)。这个小算法适用于任意数量的存储桶(在您的情况下是滑块)。
Start all three sliders at 33.333...%
When the users moves a slider up say 10% : move the two other sliders down of 5%. But if one of two slider reaches 0 => only move the other one of ten percent. So it gives something like this :
Another possibility would be to consider that the sum os the available ressources is 100, the ressources are separated into n buckets (in your case 3). When the user moves a slider, he fixes the number of ressources in the corresponding bucket. And so you may either take ressources from other bucket or put ressources in these other buckets.
You have something like :
That is assuming the modification is positive (new number in the modified bucket is higher than before). This small algorithm would work with any number of buckets (sliders in your case).
应该审查以下算法,当然还要对其进行优化。这只是我组装的东西,我还没有测试过。
使用最大值和最小值初始化每个滑块,并根据需要设置初始值,但要遵守
x + y + z = 1
。将三个滑块设置为相同的选择器:
选择器应该执行类似的操作:
如果此代码有问题,请告诉我,我可以修复它!
The following algorithm should be reviewed and of course optimized. It is only something that I have put together and I've not tested it.
initialize each slider with a max and minimum value and set the inital value as desired, but respecting that
x + y + z = 1
.Set the three slider to the same selector:
The selector should do something like that:
If there is something wrong with this code, please let me know, and I can fix it!