我试图渲染椭圆在绘图仪由单片机供电,所以有低内存工作和整数算法是预先准备的。
我有这个方程式

我试着用C语言实现它,方法如下:
y = sqrt( (b*b) * ( 1 - ( (x*x) / (a*a) )));其中y、b、x和a是整数值,但结果却是错误的。
Q1是椭圆方程的正确实现吗?
Q2还有其他方法吗?
发布于 2015-11-22 10:03:23
- `(x0,y0)` is the ellipse center
- `a,b` are the semi-axises
- `t` is angular parameter `t=<0,2*M_PI>` 1. So form a loop where `t` goes the full circle with some small enough step
2. compute `(x,y)` per each step of t
3. interpolate/render line per each step (from last to new point)
由于您的x,y,x0,y0,a,b是整数,所以要么将它们转换为浮动/双值,要么为cos[],sin[]创建整数表,例如:
- `int tcos[360],tsin[360];`现在正在使用的tcos[i]=float(1000.0*cos(float(i)*M_PI/180.0));只是整数,如:
(i=0;i<360;i++) {x=x0+(a*tco(I))/1000;y=y0+(b*tsin(i))/1000;//.}
1. loop one axis with pixel step
2. compute the other axis coordinate
3. render both pixels/or fill the line例如,轴对齐的(0,0)中心椭圆:
对于(x=-a;x<=a;x++) {y= sqrt( (b*b) -( (x*x*b*b) / (a*a);//呈现像素:(x,+y)和(x,+y)或通过行连接它们
如果您需要整数sqrt,那么实现一个整数(而不是使用math.h),例如:
int位( DWORD p) { DWORD m=0x80000000;int b=32;for (;m;m>>=1,b-) if (p>=m)断开;返回b;} DWORD sqrt(const DWORD &x) {DWORD m,a;m=(位(X)>>1);// bits(x)仅返回MSB非零位的位置可以使用m=16;相反,如果(m) m=1<>=1) { a|=m;if (a*a>x) a^=m;}返回a;}
其中DWORD是无符号32位int数据类型。
发布于 2015-11-21 17:12:56
实现是正确的。我把y,b,x和a设为双值,这是一个错误的输出。
https://stackoverflow.com/questions/33845970
复制相似问题