[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
What's wrong with abs? (draw a circle)
This is somewhat related to my posting "draw an ellipse". But now I'm just
trying to draw a circle.
I plot the function b2(x) where b2 is a function that returns a y-value that
makes x^2+y^2=1 (or very close to 1 any way).
It looks kind of alright when using the code below, (it's a bisection method
to find the correct y-value) and then change the starting value for y1 in the
code to
a) 1-x (to get a better starting value for the lower bound).
You get a problem in the second quadrant.
b) 1-abs(x) to fix the problem. Now it will be a total mess.
So, what is wrong?
I have not looked at the generated postscript, can it be that it is using the
postscript routins for "abs" and other functions, and that causes the
problem?
This is both fun and frustrating....
The code:
#include "epix.h"
#include <cmath>
using namespace ePiX;
double b2(double x){
double y1,y2,ym;
y1=0; /////// THIS LINE!!!
y2=1;
while (y2-y1>0.00001){
ym=(y1+y2)/2;
if (x*x + ym*ym >1)
y2=ym;
else
y1=ym;
}
return ym;
}
main(){
bounding_box(P(-1,-1),P(1,1));
unitlength("1in");
picture(P(1,1));
begin();
plot(b2,x_min,x_max,80);
end();
}
Gunnar.
--
The things that interest people most are usually none of their business.