I am creating an android application for L-Systems and am getting differing line lengths after rotating my points with a rotation-matrix, which results in wonky fractals.
Example:
As you can see, the length of the rotated lines varies, the line with a rotation of 120° is shorter than the line with a rotation of 240°.
.
For rotating the points i am using these Android Matrix Operations based on this answer here. I am rotating the end-point of a line around its start-point.
Code:
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;
}
The corresponding points+rotations for the first five points based on the above code:
- 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)
Debug-Log for the first five lines (up -> right -> up -> left -> up, same as the picture):
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
The Problem im having is that i want/need the resulting lines to be of equal length, but for some reason the line rotated by 120° is smaller than a line rotated by -120°.
My guess is that the differing line lengths are a result of rounding errors, but i dont know how i would start fixing this.
Any help or pointer would be appreciated.