Bug 876 : Divergent Trigonometric calculations
Last modified: 2008-08-15 14:07




Status:
RESOLVED
Resolution:
INVALID -
Priority:
P2
Severity:
normal

 

Reporter:
Patrick Gunderson
Assigned To:
fry

Attachment Type Created Size Actions
Illustration of divergent sin and cos calculations image/png 2008-08-15 14:01 631.17 KB

Description:   Opened: 2008-08-15 13:59
1. Rev 0135
2. OS X 10.4 and Win XP Intel Core 2 Duo(both)
3.

float centerX = 500;
float centerY = 500;

float radius0 = 200;
float radius1 = 150;
float radius2 = 100;

float drawrate0 = 0.30;
float drawrate1 = 0.1500;
float drawrate2 = 0.1000;

float pos0 = 0;
float pos1 = 0;
float pos2 = 0;

float x = 0;
float y = 0;

float px = 0;
float py = 0;

float x0 = 0;
float y0 = 0;

float x1 = 0;
float y1 = 0;

float x2 = 0;
float y2 = 0;

void setup(){
size(1200, 1000);
background(0);
frameRate(120);
noLoop();
draw();
}

void draw(){
for (int i = 0; i < 15000; i++){
increment();
moveX();
moveY();
stroke(255, 255, 255, 16);
line(px, py, x, y);
//point(x, y);
}
}

void increment(){
pos0 += drawrate0;
pos1 += drawrate1;
pos2 += drawrate2;
}

void moveX(){
x0 = centerX + radius0 * cos(pos0);
x1 = radius1 * cos(pos1);
x2 = radius2 * cos(pos2);
px = x;
x = x0 + x1 + x2;
}

void moveY(){
y0 = centerY + radius0 * sin(pos0);
y1 = radius1 * sin(pos1);
y2 = radius2 * sin(pos2);
py = y;
y = y0 + y1 + y2;
}


4. As the position values are incremented (thousands of times) it becomes
apparent that the sin and cos calculations aren't completely correct. The
results diverge, rather than remaining consistent and linear. This makes
for very pretty patterns, but means that the calculation is inaccurate.

I have a feeling this is related to the use of a float as a basis for the
calculation, but doubles don't appear to be allowed.
Additional Comment #1 From Patrick Gunderson 2008-08-15 14:01
edit]
Illustration of divergent sin and cos calculations
Additional Comment #2 From fry 2008-08-15 14:07
Doubles are allowed, however there are no (Processing) API functions that
support them, however you're free to use Java functions (Math.cos() et al)
to manipulate them instead.

Or, if you want an accurate means of incrementing in fixed size steps, use
ints to keep track of the step count, and then multiply later.