繁体   English   中英

在Android中使用矩阵旋转点时,为什么会得到不同的线长?我该如何解决?

[英]Why do i get differing line lengths when rotating points with matrices in Android and how do i fix it?

我正在为L-Systems创建一个android应用程序,并使用旋转矩阵旋转点后得到了不同的线长,这导致了不规则的分形。

例:

分形未对齐

如您所见,旋转线的长度变化,旋转120°的线比旋转240°的线短。

为了旋转点,我在这里根据此答案使用这些Android Matrix Operations 我正在围绕线的起点旋转线的终点。

码:

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°的线。

我的猜测是,不同的行长是舍入错误的结果,但是我不知道如何开始解决这个问题。

任何帮助或指针,将不胜感激。

我不确定我是否能完全解决它,但是看来Android中有两个不同的坐标点类:

整数坐标点和浮点坐标点。 猜猜我用了哪一个? 是的,整数1。对我的点使用浮点坐标消除了舍入误差的不准确性。

解决方案 :使用float PointF代替int Point。

https://developer.android.com/reference/android/graphics/Point.html

https://developer.android.com/reference/android/graphics/PointF.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM