计算左值和宽度值的公式(基于 0% 和 100% 值)

发布于 2024-12-08 13:32:08 字数 673 浏览 1 评论 0原文

如何找到基于百分比值并根据以下 0% 和 100% 值给出左值和宽度值的公式?

对于 0,00 百分比,左和宽度值将是:

  • 宽度:658
  • 左:11

对于 100,00 百分比,左和宽度值将是:

  • 宽度:0(或 1)
  • 左:670

我有一个渐变图像,其上我应用一种颜色来直观地表示百分比值。 渐变图像从 11 像素开始(左:11 表示 0,00%),宽度为 659(左:670 表示 100,00% = 659 + 11)

左和宽度值需要根据百分比,使颜色覆盖渐变。

如果百分比为 0%,则颜色将覆盖整个渐变(左:11,宽度:659)

------------------------------
|____________________________|
0                            100

如果百分比为 100%,则颜色根本不会覆盖渐变(左:670,即 659 + 11,宽度: 1 或 0)

                              -
|____________________________|
0                            100

How can I find the formula that would, based on a percentage value, give me left and width values based on the following 0% and 100% values?

For 0,00 percentage, left and with values would be:

  • Width: 658
  • Left: 11

For 100,00 percentage, left and width values would be:

  • Width: 0 (or 1)
  • Left: 670

I have a gradient image, on which I apply a color to give the percentage value a visual representation.
The gradient image starts at 11 pixels (Left: 11 for 0,00%) and has a width of 659 (Left: 670 for 100,00% = 659 + 11)

The left and width values needs to be calculate dynamically based on the percentage so that the color overlays the gradient.

If the percentage is 0%, the color would overlay the whole gradient (left: 11, width: 659)

------------------------------
|____________________________|
0                            100

If the percentage is 100%, the color would not overlay the gradient at all (left: 670 which is 659 + 11, width: 1 or 0)

                              -
|____________________________|
0                            100

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2024-12-15 13:32:08

您要查找的公式可以表示为 f(x) = ax+b。 f(x)是你想要得到的宽度,x是百分比,a & b 是(当前未知)常数。

如果您有两个点(例如 0 和 100)的 f(x) 值,您可以导出如下公式:

f(0) = valueAtZero = a*0 + b
b = valueAtZero
f(100) = valueAtOneHundred = a*100 + b
valueAtOneHundred - b = a*100
a = (valueAtOneHundred - b) / 100

现在您有了 a 和 b,您可以求解出您想要的任何百分比 x。上面的过程转换为伪代码如下:

function getValueAtXPercent(x, valueAtZero, valueAtOneHundred):
    b = valueAtZero
    a = (valueAtOneHundred - b) / 100
    return a*x + b

现在您可以调用 getValueAtXPercent(50, 658, 1),它将返回 50% 的宽度值。

这是一个具体的实现,在Python中:

def f(x, valueAtZero, valueAtOneHundred):
    b = valueAtZero
    a = (valueAtOneHundred - b) / 100.0
    return a*x + b

def showStats(percent):
    left = f(percent, 11, 670)
    width = f(percent, 658, 0)
    output = "At {0}%, left={1} and width={2}".format(percent, left, width)
    print output

showStats(0)
showStats(50)
showStats(100)

输出:

At 0%, left=11.0 and width=658.0
At 50%, left=340.5 and width=329.0
At 100%, left=670.0 and width=0.0

The formula you're looking for can be expressed as f(x) = ax+b. f(x) is the width you want to get, x is the percentage, and a & b are (currently unknown) constants.

If you have the value of f(x) for two points (0 and 100 for instance), you can derive the formula like so:

f(0) = valueAtZero = a*0 + b
b = valueAtZero
f(100) = valueAtOneHundred = a*100 + b
valueAtOneHundred - b = a*100
a = (valueAtOneHundred - b) / 100

Now that you have a and b, you can solve for any percentage x you want. The process above translates into pseudocode as:

function getValueAtXPercent(x, valueAtZero, valueAtOneHundred):
    b = valueAtZero
    a = (valueAtOneHundred - b) / 100
    return a*x + b

now you can call getValueAtXPercent(50, 658, 1) and it will return your width value for 50%.

Here is a concrete implementation, in python:

def f(x, valueAtZero, valueAtOneHundred):
    b = valueAtZero
    a = (valueAtOneHundred - b) / 100.0
    return a*x + b

def showStats(percent):
    left = f(percent, 11, 670)
    width = f(percent, 658, 0)
    output = "At {0}%, left={1} and width={2}".format(percent, left, width)
    print output

showStats(0)
showStats(50)
showStats(100)

output:

At 0%, left=11.0 and width=658.0
At 50%, left=340.5 and width=329.0
At 100%, left=670.0 and width=0.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文