-4

问题是,我没有得到一个合适的圈子。例如,如果我输入坐标:9,8 和半径:8 ...我只得到很少的点。有人可以指导我如何获得一个完整的圆,这段代码有什么问题?我们不能使用任何内置函数..

4

3 回答 3

1

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.

于 2012-11-20T17:11:40.887 回答
1

You should use Midpoint circle algorithm.

Nice thing about it that it uses only integer arithmetic - so it is both fast and exact.

于 2012-11-20T17:12:21.613 回答
0

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.

于 2012-11-20T17:11:48.730 回答