[英]Coordinate Algorithm - Rotate around the center
By looking at this image I think you will understand my problem pretty well: 通过查看此图像,我认为您会很好地理解我的问题:
(image removed - url no longer valid, returns advertising now) (图片已删除-网址不再有效,现在返回广告)
So basically I want a function that takes an object as parameter and gives this object the correct coordinates based on how many objects I've added before. 因此,基本上我想要一个函数,该函数将一个对象作为参数,并根据我之前添加的对象数为该对象提供正确的坐标。
Let's say I would add all these objects to an array: 假设我会将所有这些对象添加到数组中:
objectArray[]
Each time I add a new object: objectArray.add(object) 每次我添加一个新对象:objectArray.add(object)
The object.x
and object.y
coordinates will be set based on some algorithm: object.x
和object.y
坐标将基于以下算法设置:
object.x = ?
object.y = ?
(I'm working in Java) (我正在使用Java)
Thanks for any help. 谢谢你的帮助。
Here's the closed-form solution that doesn't rely on a loop... I'm not handy with Java, so it's in C#, but it uses basic operations. 这是不依赖循环的闭式解决方案...我不方便使用Java,因此它在C#中,但是使用基本操作。
static void SpiralCalc(int i) {
i -= 2;
// Origin coordinates
int x = 100, y = 100;
if (i >= 0) {
int v = Convert.ToInt32(Math.Truncate(Math.Sqrt(i + .25) - .5));
int spiralBaseIndex = v * (v + 1);
int flipFlop = ((v & 1) << 1) - 1;
int offset = flipFlop * ((v + 1) >> 1);
x += offset; y += offset;
int cornerIndex = spiralBaseIndex + (v + 1);
if (i < cornerIndex) {
x -= flipFlop * (i - spiralBaseIndex + 1);
} else {
x -= flipFlop * (v + 1);
y -= flipFlop * (i - cornerIndex + 1);
}
}
// x and y are now populated with coordinates
Console.WriteLine(i + 2 + "\t" + x + "\t" + y);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.