首页 \ 问答 \ 移动数字范围的中点(Shift Middle Point of Number Range)

移动数字范围的中点(Shift Middle Point of Number Range)

我有一个功能,可以将任何数字范围标准化为新范围。


[123,456][0,10]
要么
[-50,50][-1,1]

我需要帮助改变新范围的中间点。

我正在使用它作为GUI滑块,其范围为[-100,100]但控制值为[-2,2]
GUI Slider默认值为0但默认值为1 (无)。
我的程序是CLI的GUI,我无法将该程序的默认值更改为0

滑块

Slider
-100 --------------||-------------- 100
                    0
Value
-2 |  |  | -1 |  |  0|  |  | 1  |  |  2

Shifted Value
-2 |  | -1 |  | 0 | 1|  |  |  |  |  | 2

规范化

https://dotnetfiddle.net/42jHvM

// Normalize Function
public static double Normalize(double val, double valmin, double valmax, double min, double max, double midpoint) 
{
    return (((val - valmin) / (valmax - valmin)) * (max - min)) + min;
}

double output = Normalize(  0, // slider input
                         -100, // input min
                          100, // input max
                           -2, // normalize min
                            2, // normalize max
                            1  // middle point
                         );

转移中点

在我的例子中,我使用[-100,100]转换为[-2,2]

input [-100,100]的中间点为0
但我需要output [-2,2]的中间点为1而不是0

所以它会倾斜,朝-2慢,朝2快。

示例 I在photoshop中制作以使用渐变可视化中点移位。

转移

缓解

我尝试使用Ease Out Tween来移动中间点,但我不知道要使用什么值。
示例输出需要为Min: -2Mid: 1Max: 2

https://dotnetfiddle.net/nfNlBC

它用:

//t: current time
//b: start value
//c: change in value
//d: duration

中间点移动后:

输入-100应输出-2
输入-50应输出-0.50
输入0应输出1
输入50应输出1.75
输入100应输出2

之前

-100 | -50 | -25  |  0  |  25 |  50 | 100

 -2  | -1  | -0.5 |  0  | 0.5 |  1  |  2

approximated

-100 | -50  | -25  |  0  |  25  |  50  | 100

 -2  |-0.50 | 0.25 |  1  | 1.25 | 1.75 |  2

如何修改Normalize函数,或通过新的Shift / Easing函数运行它的输出来设置范围的新中点并重新调整数字?


I have a function that Normalizes any number range to a new range.

Such as
[123,456] to [0,10]
or
[-50,50] to [-1,1]

I need help shifting the middle point of the new range.

I'm using this for a GUI Slider that has a range of [-100,100] but controls a value of [-2,2].
The GUI Slider default is 0 but the value default is 1 (none).
My program is a GUI for a CLI and I'm not able to change that program's value default to 0.

slider

Slider
-100 --------------||-------------- 100
                    0
Value
-2 |  |  | -1 |  |  0|  |  | 1  |  |  2

Shifted Value
-2 |  | -1 |  | 0 | 1|  |  |  |  |  | 2

Normalize

https://dotnetfiddle.net/42jHvM

// Normalize Function
public static double Normalize(double val, double valmin, double valmax, double min, double max, double midpoint) 
{
    return (((val - valmin) / (valmax - valmin)) * (max - min)) + min;
}

double output = Normalize(  0, // slider input
                         -100, // input min
                          100, // input max
                           -2, // normalize min
                            2, // normalize max
                            1  // middle point
                         );

Shift Middle Point

In my example I use [-100,100] converted to [-2,2].

The middle point of the input [-100,100] is 0.
But I need the output [-2,2] to have a middle point of 1 instead of 0.

So it will be skewed, slower towards -2, faster towards 2.

Example I made in photoshop to visualize the middle point shift using a gradient.

shift

Easing

I tried using an Ease Out Tween to shift the middle point, but I don't know what values to use.
The example output needs to be Min: -2, Mid: 1, Max: 2.

https://dotnetfiddle.net/nfNlBC

It uses:

//t: current time
//b: start value
//c: change in value
//d: duration

After the middle point has been shifted:

Input of -100 should output -2.
Input of -50 should output -0.50?
Input of 0 should output 1.
Input of 50 should output 1.75?
Input of 100 should output 2.

Before

-100 | -50 | -25  |  0  |  25 |  50 | 100

 -2  | -1  | -0.5 |  0  | 0.5 |  1  |  2

After approximated

-100 | -50  | -25  |  0  |  25  |  50  | 100

 -2  |-0.50 | 0.25 |  1  | 1.25 | 1.75 |  2

Question

How can I modify the Normalize function, or run it's output through a new Shift/Easing function to set the new middle point of the range and readjust the numbers?


原文:https://stackoverflow.com/questions/51177865
更新时间:2022-10-28 18:10

最满意答案

好吧,我仍然不确定为什么会发生这种情况,但是找到了一种解决方法,可能不是最优雅的,但是如果您不需要更改隐藏字段的值时模型的值发生变化,它将会起作用。 我不是将模型发布回我的控制器,所以这个解决方案对我来说已经足够了

我改变了隐藏的字段代码

@Html.HiddenFor(model => model.PolicyHolderId, 
new Dictionary<string, object>{{ "id", "policyHolderIdHidden"}})

@Html.Hidden("policyHolderIdHidden", 
Model.PolicyHolderId, new Dictionary<string, object>{{ "id", "policyHolderIdHidden"}})

Ok, I'm still not sure why this is happening but found a workaround, probably not the most elegant but it will work if you do not need the values of your model to change as you change the value of the hidden field. I'm not posting the model back to my controller so this solution was sufficient for me

I changed the hidden field code from

@Html.HiddenFor(model => model.PolicyHolderId, 
new Dictionary<string, object>{{ "id", "policyHolderIdHidden"}})

to

@Html.Hidden("policyHolderIdHidden", 
Model.PolicyHolderId, new Dictionary<string, object>{{ "id", "policyHolderIdHidden"}})

相关问答

更多
  • 如果您的日期已经在页面加载功能中填充,请添加。 $(":asp(LeadEffectiveDateToSetForUserRole)").val(effectiveDate) If your date is already being populated inside a page load function add the there. $(":asp(LeadEffectiveDateToSetForUserRole)").val(effectiveDate)
  • 这就解释了一切。 您在这个问题中遇到与另一个人相同的问题。 请参阅接受的答案以及与您的问题相关的评论 。 and that explains everything. You are experiencing the same problem as another person in this question. Please see accepted answer and the comments to it as it's relevant to your issue.
  • 您不能使用隐藏字段的双重绑定。 解决方案是使用括号: {{data}} 编辑:在github上查看此线程: https : //github.com/angular/angular.js/pull/2574 编辑: 自Angular 1.2以来,您可以使用'ng-value'指令将表达式绑定到input的value属性。 该指令应与输入收音机或复选框一起使用,但与隐藏的输入效果良好。 这里是使 ...
  • 简单: $(document).ready(function() { $("#question_id").val($("#que_id").val()); }); Simple: $(document).ready(function() { $("#question_id").val($("#que_id").val()); });
  • @message = current_user.sent_messages.build(:to => [@original.author.id], :subject => subject, :body => body) render :template => "sent/raply" Figured it out. Kept the controller as such: @message = current_user.sent_messages.build(:to => @original.author ...
  • 问题与JavaScript中的区分大小写有关。 虽然您已为这些字段设置了.Value,但这与.value不同。 更改您的javascript以设置.value,您应该全部设置。