转换光标坐标

时间:2013-03-08 05:34:28

标签: c#

我有这段代码将鼠标光标的 X 坐标转换为float值:

    private float XToFloat(int x)
    {
        return (float)(x / (float)this.Width);
    }

结果是这样的 0.01234567 现在如何将其转换回原始坐标?有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

它只是一个简单的数学。使用代码。

private float XToFloat(int x)
{
   return (float)(x / (float)this.Width);
}

示例:

//If Width Value
this.Width = 10;

调用函数并将值传递给XToFloat(5);之类的参数 该函数将返回 0.5 的值。

//inside the function
    (float)(x / (float)this.Width); => (float)(5 / (float)10);
//z = x/y

将其转换回原始值,我们创建一个函数,我们将浮点值乘以宽度。

private int FloatToX(float f){
        return (int)((float)this.Width*f);
    }

示例FloatToX(0.5) 0.5x10 将返回 5 的整数值

//inside the function
    (int)((float)this.Width*f) => (int)((float)10*0.5)
//x = zy

在数学中它只是

得到z

z = x/y

获取x

x = zy

得到y

y = x/z

答案 1 :(得分:0)

这是简单的数学:

float floatValue = (float)(x / (float)this.Width);
int cursorX = floatValue * this.Width;

编辑:将浮动值视为百分比。范围从0到1,相当于0-100%。然后,x坐标的浮点版本只是宽度的百分比:

0%   width   100%
+-------------+
|             |
|        80%  |
|         X   |
|             |
+-------------+

因此,要获得原始坐标,请将宽度乘以百分比。

答案 2 :(得分:0)

我们这样说。宽度为Y现在

转换值(0.01234567)为Z

所以你正在做的是

z=x/y 

你想要的是x,这次你有Z这样

Z*Y=X