Rounding error, but not at sqrt(). Math.h functions return more precision than you know what to do with. Your error happens when you store a floating point result in an integer, which is done via truncation. Here are the two lines from the first case.
| CODE |
posInfo.y1=Y+radius-newYScale; posInfo.y2=Y+radius+newYScale; |
Lets look at a point where the thing just begins to round. So say, radius=5.0, newYScale=4.9; Lets just set Y=0 for simplicity. So Y+radius-newYScale = 0.1, which is truncated to 0 when stored in posInfo.y1. Y+radius+newYScale = 9.9, which is truncated ot 9 when stored in posInfo.y2. (Edit: your posInfo.y1/y2 might actually be defined as float, but at any rate, this truncation is done at some point. Perhaps within the engine. Sorry, didn't catch that possibility right away. It all works the same as if these values are int, so the fix should be the same.)
What does that mean? Your bars normally run from 0 to 10. As soon as it starts rounding, instead of running from 1 to 9, it runs from 0 to 9. When radius is big, you simply don't notice this error. When radius is small, this becomes apparent.
Simplest thing you can do is replace truncation by rounding. Define a new function.
| CODE |
int round2int(float f) { int a;
a=(int)f; if(f-a>0.5)return a+1; if(f-a<-0.5)return a-1; return a; } |
Now replace the lines that compute Y coordinates accordingly.
| CODE |
posInfo.y1=round2int(Y+radius-newYScale); posInfo.y2=round2int(Y+radius+newYScale); |
The resulting shape might still be a bit off, but it should at list not look so flattened. See if it's good 'nough.