C# 转换光标坐标
我有一段代码,它将鼠标光标的X坐标转换为C# 转换光标坐标,c#,C#,我有一段代码,它将鼠标光标的X坐标转换为float值: private float XToFloat(int x) { return (float)(x / (float)this.Width); } 结果是这样的0.01234567现在如何将其转换回原始坐标?有人能帮忙吗?这是简单的数学: float floatValue = (float)(x / (float)this.Width); int cursorX = floatValue * this
float
值:
private float XToFloat(int x)
{
return (float)(x / (float)this.Width);
}
结果是这样的0.01234567现在如何将其转换回原始坐标?有人能帮忙吗?这是简单的数学:
float floatValue = (float)(x / (float)this.Width);
int cursorX = floatValue * this.Width;
编辑:将浮点值视为百分比。其范围为0到1,相当于0-100%。x坐标的浮动版本只是宽度的一个百分比:
0% width 100%
+-------------+
| |
| 80% |
| X |
| |
+-------------+
为了得到原始坐标,你将宽度乘以百分比。让我们这样说。现在宽度为Y 转换值(0.01234567)为Z 所以你现在做的是
z=x/y
你想要的是x,这次你有Z,所以
Z*Y=X
这只是一个简单的数学。使用代码
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 = x/z
谢谢你的详细解释,我现在明白了,我应该在数学课上注意,因为它适合你的用户名,你介意进一步解释代码,或者它是如何工作的吗?我已经编辑了我的答案。对不起,我不太擅长解释:)