We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Weird rendering behaivour
Page Index Toggle Pages: 1
Weird rendering behaivour (Read 924 times)
Weird rendering behaivour
Nov 30th, 2009, 5:17am
 
I do some animated drawing (lines and text) with floating point values.
In certain circumstances (which I haven't sorted out yet but it depends on the value of the coordinates) the lines and the text, which have exact the same coordinates, aren't aligned as they where expected to be. The difference is one pixel only, so it looks to me like there is an internal problem with floating point coordinates.
Maybe I should mention that I'm drawing to an off screen and then render it to the screen via img().

Is this a known behavior and are there any workarounds?

Thanks.
Re: Weird rendering behaivour
Reply #1 - Nov 30th, 2009, 5:51am
 
It is hard to answer with such vague description, so I will give a generic answer...
Float point arithmetic in Processing, ie. in Java, in any language actually (it is more a computer number modeling issue) has limited precision.
You can have a better precision by using doubles, BTW, but sometime you might have issues in using them in Processing.

If you do lot of operations with floats, you will have a little error on each operation, but these errors can cumulate, being bigger and bigger, and can reach one unit.
Well, at least, it can be a possible source of your problems...

You have also some issues in representing some decimal numbers: try running these lines:
float x = 0.0001;
println(x * 1000);

You might have a surprise!

You can compensate by rounding some values, etc. Depends on your computations.
Re: Weird rendering behaivour
Reply #2 - Nov 30th, 2009, 6:31am
 
Thanks PhiLho.

The problem occurs at runtime and I haven't found out yet how exactly to reproduce it. This code could maybe clarify why I'm thinking it could be a Processing issue.

Code:

// generic code

float y = 0.0f;

for (;;) {
 background(0);
 line(100.0f, y, 200.0f, y);
 text("Hello!", 100.0f, y);
 y += 0.2f;
}


As mentioned in the first post I'd expect the line and the text to be aligned, or have at least the same vertical distance on each pass.
Re: Weird rendering behaivour
Reply #3 - Nov 30th, 2009, 7:02am
 
Well, perhaps my eyes aren't sharp enough, but I can't really see a gap when I transform your pseudo-code into real Processing code:
Code:
float y = 0.0;

void setup()
{
size(200, 900);
PFont f = createFont("Arial", 24);
textFont(f);
}

void draw()
{
background(255);
stroke(#FF0000);
line(0.0, y, 200.0, y);
stroke(0);
fill(0);
text("Hello! " + y, 10.0, y);
y += 0.2;
}

Anyway, what I wrote is still true: drawing text is a complex operation, which can suffer from rounding errors.
Perhaps it depends on graphics mode too.
Re: Weird rendering behaivour
Reply #4 - Dec 3rd, 2009, 6:13am
 
In this example the text is rendered once with a scale factor of 1 then with a scale factor of 2. The result is that in the first case the text and the line are always aligned and in the second one they aren't.
Code:
float y = 0.0f;
PFont font;

void setup() {
 size(500, 300);
 font = createFont("Arial", 22);;
 textFont(font);
}

void draw() {
 background(0);
 stroke(255);
 strokeWeight(1);
 fill(255);

 scale(1.0f);
 line(100.0f, y, 200.0f, y);
 text("Hello!", 100.0f, y);
 
 scale(2.0f);
 line(100.0f, y, 200.0f, y);
 text("Hello!", 100.0f, y);

 y += 0.2f;
}
Re: Weird rendering behaivour
Reply #5 - Dec 3rd, 2009, 6:56am
 
i'm seeing the text and the line move down the screen together, no gap between them. the large text does bobble a bit, probably due to rounding errors as PhiLho says, but there's never a gap.

winxp / processing 1.0.7. i think this might be an operating system thing.
Re: Weird rendering behaivour
Reply #6 - Dec 3rd, 2009, 7:40am
 
I've tried it with those configurations:
- Windows Vista and Processing 1.01/1.09 (hardware acceleration)
- Windows 2000 and Processing 1.07 (no hardware acceleration)

The result was the same: the scaled up text wobbles.
And the text combined with the scaling is just one example. There are other combinations (without text or scaling and even without floating point values being involved) that result in a wobbling animation.

As I pass the same value to two different functions/methods and the result is a difference in the rendering, this seems to be an internal problem.
Re: Weird rendering behaivour
Reply #7 - Dec 3rd, 2009, 7:59am
 
Using a different renderer (P2D) than the default JAVA2D could be a solution but then the rendering is rather poor and it's missing the background  transparency option...
Code:
float y = 0.0f;
PFont font;

void setup() {
 size(500, 300, P2D);
 font = createFont("Arial", 22);;
 textFont(font);
 noSmooth();
}

void draw() {
 background(0);
 stroke(255);
 strokeWeight(1);
 fill(255);

 scale(1.0f);
 line(100.0f, y, 200.0f, y);
 text("Hello P2D!", 100.0f, y);
 
 scale(2.0f);
 line(100.0f, y, 200.0f, y);
 text("Hello P2D!", 100.0f, y);

 y += 0.2f;
}
Page Index Toggle Pages: 1