C#是否可以将点或位置转换为int? [英] C# Is it possible to convert a point or location to an int?
本文介绍了C#是否可以将点或位置转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能在一个点或位置,因为这是iv尝试和失败
是否可以转换为点或位置到int。这就是我尝试但失败了。
Is it possible to int a point or location, coz this is what iv tried and failed
Is it possible to convert a point or location to an int. This is what I've tried but it failed.
int d = (206,362);
任何人都可以帮帮我吗?
Can anyone help me out?
推荐答案
您可以将它映射到十进制
(如果组件int
值不是太大),这是其核心和整数类型。
我会考虑用它中的两个值创建一个字符串,用空格字符分隔。
You could map it to adecimal
(if the componentint
values aren't too big) which is, at its core, and integer type.
I would look into just making a string with the two values in it, separated by a space character.
适用于16位坐标,不大于:
Works for 16-bit coordinates, no larger:
int intPt = (x << 16) | (y);
提取x-coord:
To extract x-coord:
int x = (intPt & 0xFF00) >> 16;
和y-coord:
and y-coord:
int y = intPt & 0x00FF;
那说,我真的不推荐它。您到底想要做什么?
That said, I really wouldn't recommend it. What on earth are you trying to do?
在给定以下限制的情况下,您可以轻松地将点映射到int。点x和y分量也是int。 x的范围例如是[0..255],y的范围也是[0..255]。然后通过以下方式计算点到int的映射:
You can easily map a point to an int given the following restrictions. The points x and y components are also int. The range of x is for example [0..255] and the range for y is also [0..255]. Then the mapping of the point to an int is calculated by:
//This code is pure pseudo code
//Declare the range and a point
Range r = new Range(0,255);
Point<int,int> aPoint = new Point<int,int>(50,60,r);
//Here we map the point to an int
int mappedPoint = aPoint.x + aPoint.y * (r.Upper + 1);
//Here we map the int back to a point
int x = mappedPoint % (r.Upper + 1);
int y = mappedPoint / (r.Upper + 1);
Point<int,int> mappedPointFromInt = new Point<int,int>(x, y);
因此,鉴于观察到上述约束,确实可以进行映射。应该引入一个检查以确保映射的点适合int。
干杯!
So yes a mapping is indeed possible given that the above constraints are observed. A check should be introduced to make sure that the mapped point will fit into an int.
Cheers!
这篇关于C#是否可以将点或位置转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文