问题是,我没有得到一个合适的圈子。例如,如果我输入坐标:9,8 和半径:8 ...我只得到很少的点。有人可以指导我如何获得一个完整的圆,这段代码有什么问题?我们不能使用任何内置函数..
3 回答
This is incorrect
if(sqrt(pow(i,2)+ pow(j,2))== radius)
It's very rare that these two values will be exactly equal. Instead you should make a test to see if the two numbers are roughly equal. Like this for instance
if (fabs(sqrt(pow(i,2)+ pow(j,2)) - radius) <= 0.001)
This tests if sqrt(pow(i,2)+ pow(j,2))
and radius
are within 0.001 of each other. You might need to change the value of 0.001 to something else. It depends on your co-ordinate system.
You should use Midpoint circle algorithm.
Nice thing about it that it uses only integer arithmetic - so it is both fast and exact.
what is wrong in this code?
Here you are:
if (sqrt(pow(i, 2) + pow(j, 2)) == radius)
You should not compare floating-point numbers using ==
, it won't work as expected. You should check instead if it's close to the radius:
if (sqrt(pow(i, 2) + pow(j, 2)) >= radius * 0.95)
for example.