mcornell
mcornell

Reputation: 768

Generated Points going arwy

This is more of a fun experiment than an issue, I'm more curious as to what causes it.

I'm generating 2D points, stored as floats, in order to draw a collection of lines in pattern. When generating a new point, I make a copy of the last point, and move it in a certain direction based on a heading (x += cos(heading), y += sin(heading)). Heading is also stored as a float. I change the heading at a given rate (say, 90 degrees), which would imply that every new point is either parallel with the last point or at a right angle to it.

This works, but after many (1000s) of iterations, the outer edges of the pattern start to get slightly thicker, and then the newly plotted points start being moved at a slightly askew (but consistent) location. The entire pattern then starts to spin.

What I'm curious of is how something that shouldn't break breaks. My best guess is the heading (a float which is constantly being reduced) is losing definition when it reaches a certain size.

Essentially, the code (for producing the exact pattern below) looks like this

float distance = 25;
int theta = 90;
float heading = 0;

    public static void main(String[] args) {
    turnLeft();
    func();
    turnRight();
    func();
    turnRight();
    turnRight();
    func();
}

public void func() {
    drawForward();
    turnRight();
    drawForward();
    turnRight();
    turnRight();
    drawForward();
    turnRight();
    drawForward();
    drawForward();
}

    public void turnLeft() {
    heading += degToRad(theta);
}

public void turnRight() {
    heading -= degToRad(theta);
}

public float degToRad(float degrees) {
    return (float) (degrees * (Math.PI / 180.0));
}

public void drawForward() {
    FPoint newFp = new FPoint(array[array.length - 1]); //copy last point

    movePoint(newFp, (float) (distance * Math.cos(heading)),
            (float) (distance * Math.sin(heading)));

    appendPoint(newFp); //add point to end of array

}

public void movePoint(FPoint fp, float x, float y) {
    fp.x += x;
    fp.y += y;
}

Any thoughts on the matter would be greatly appreciated!

Upvotes: 1

Views: 67

Answers (1)

huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11910

See if you are at the exact same place when you move forward and backward. If you are not at the same location, then you subtract half of the difference from each forward movement.

This could be in relation with numerical stability and precision of the calculations. http://en.wikipedia.org/wiki/Numerical_stability .

Since you dont have a go backward , you just use forward + right + forward +right .... until you reach the starting place, then subtract if it is different then starting value. Then use this as an offset error value to subtract(but divide the error by number of movements first)(of course the would be like "FRFRFRFECC" )

This is called BFECC back and forth error correction compensation. Greatly reduces the error if it is about unavoidable errors.

I saw someone testing BFECC on Zalesak's Turning Disk to see if disk becomes corrupt after thousands of rotation iterations.

Upvotes: 1

Related Questions