我正在为L-Systems创建一个Android应用程序,并且在使用旋转矩阵旋转我的点后得到不同的线长度,这会导致分形错误。
示例:
如您所见,旋转线的长度不同,旋转120°的线比旋转240°的线短。
为了旋转点我使用这些Android Matrix Operations基于this answer here。 我正在围绕其起点旋转一条直线的终点。
代码:
Matrix transform;
public Point rotate(Point angleP, Point p, float degrees) {
// This rotates around the previous Point by degrees
transform.setRotate(degrees, angleP.x, angleP.y);
// Create new float[] to hold the rotated coordinates
float[] pts = new float[2];
// Initialize the array with our Coordinate
pts[0] = p.x;
pts[1] = p.y;
// Use the Matrix to map the points
transform.mapPoints(pts);
// NOTE: pts will be changed by transform.mapPoints call
// after the call, pts will hold the new cooridnates
// Now, create a new Point from our new coordinates
Point newPoint = new Point((int)pts[0], (int)pts[1]);
// Return the new point
return newPoint;
}
基于以上代码的前五个点的对应点+旋转:
- forward by 30pt, angle is: 0.0°, rotate/translate Point(384, 433) -> Point(384, 433)
- changed rotation by120.0 from 0.0->120.0
- forward by 30pt, angle is: 120.0°, rotate/translate Point(384, 403) -> Point(409, 447)
- changed rotation by-120.0 from 120.0->0.0
- forward by 30pt, angle is: 0.0°, rotate/translate Point(409, 417) -> Point(409, 417)
- changed rotation by-120.0 from 0.0->-120.0
- forward by 30pt, angle is: -120.0°, rotate/translate Point(409, 387) -> Point(383, 431)
- changed rotation by120.0 from -120.0->0.0
- forward by 30pt, angle is: 0.0°, rotate/translate Point(383, 401) -> Point(383, 401)
前五行的调试 - 日志(向上 - >向右 - >向上 - >向左|向上>向上,与图片相同):
line# line-start line-end line-length
line0 from 384/463 to 384/433, length: 30.0
line1 from 384/433 to 409/447, length: 28.653097563788805
line2 from 409/447 to 409/417, length: 30.0
line3 from 409/417 to 383/431, length: 29.5296461204668
line4 from 383/431 to 383/401, length: 30.0
我遇到的问题是我想要/需要得到的线长度相等,但由于某种原因,旋转120°的线小于旋转-120°的线。
我的猜测是,不同的线长是舍入误差的结果,但我不知道如何开始修复它。
任何帮助或指针都会受到赞赏。
答案 0 :(得分:0)
我不确定我是否完全解决了它,但似乎Android中有两个不同的坐标点类:
整数坐标点和浮点坐标点。猜猜我用的是哪一个?对,整数一..使用我的点的浮点坐标摆脱了舍入错误的不准确性。
解决方案:使用float PointF而不是int Point。
https://developer.android.com/reference/android/graphics/Point.html
https://developer.android.com/reference/android/graphics/PointF.html