Coordinate Algorithm - Rotate around the center(坐标算法 - 围绕中心旋转)
问题描述
通过查看这张图片,我想你会很好地理解我的问题:
By looking at this image I think you will understand my problem pretty well:
(图片已删除 - 网址不再有效,现在返回广告)
所以基本上我想要一个以对象为参数的函数,并根据我之前添加的对象数量为该对象提供正确的坐标.
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[]
每次我添加一个新对象时:objectArray.add(object)
Each time I add a new object:
objectArray.add(object)
object.x 和 object.y 坐标将基于某种算法设置:
The object.x and object.y coordinates will be set based on some algorithm:
object.x = ?
object.y = ?
(我正在使用 Java)
(I'm working in Java)
感谢您的帮助.
推荐答案
这是不依赖循环的封闭式解决方案...我对 Java 不太熟悉,所以它在 C# 中,但它使用基本操作.
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.
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 + " " + x + " " + y);
}
这篇关于坐标算法 - 围绕中心旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:坐标算法 - 围绕中心旋转
基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
