×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

not hard though

1. write an integer function sqrt() (round to closest integer)
You do not need a accurate sqrt function. to calculate y = sqrt(r * r - x * x), you can loop y from r - x to r and compare y * y and r * r - x * x (of course it's better to save r * r - x * x into a variable) and get the closest y.

2.
for (x = 0; x <= r; ++x)
{
  y = sqrt(r * r - x * x);
  draw points (x, y), (x, -y), (-x, y), (-x, -y)
}

3. there might be some disconnected points when x is close to r. to improve this case, you can save the previous y to y1. if (y1 - y > 1), loop from y1 to y and for each step calculate x1 and draw the 4 points.
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 一道常见的编程面试题求解, 多谢
    画圆:
    Write a routine to draw a circle given a center coordiante (x,y) and a radius (r) without making use of any floating point computations.
    • not hard though
      1. write an integer function sqrt() (round to closest integer)
      You do not need a accurate sqrt function. to calculate y = sqrt(r * r - x * x), you can loop y from r - x to r and compare y * y and r * r - x * x (of course it's better to save r * r - x * x into a variable) and get the closest y.

      2.
      for (x = 0; x <= r; ++x)
      {
        y = sqrt(r * r - x * x);
        draw points (x, y), (x, -y), (-x, y), (-x, -y)
      }

      3. there might be some disconnected points when x is close to r. to improve this case, you can save the previous y to y1. if (y1 - y > 1), loop from y1 to y and for each step calculate x1 and draw the 4 points.
      • 好像我用过的所有计算机语言里sqrt都是floating 函数
        • for sure here we can not use the predefined function sqrt().
    • 把屏幕上的每一点都计算(x-x0)^2+(y-y0)^2, 如果值等于r^2画点,如果不是不画。
      • You will only draw couple pixels.
    • Not verified yet.
      for (i = r; i >= 0; i--)
      {
      for (j = r - i; j <= r; j++)
      {
      if ((i * i + j * j <= r * r) && ( i * i + (j + 1) * (j + 1) >= r * r ))
      {
      drawpoint( x - i, y + j);
      drawpoint( x + i, y - j);
      drawpoint( x - i, y + j);
      drawpoint( x + i, y - j);
      }
      }
      }
      • This is a very good answer.
    • You guys made me laugh. It is a very basic computer graphics concept. Even a non IT guy like me know the answer.
      • hehe!!
        graphic.drawOval(x,y,r);
      • 谁叫我今天心情好呢。写CODE我就不会,讲讲原理吧。先画第一象限的1/4。
        start from (x+r, y)

        start from this point to draw the 1/4 circle, the first step should be up,

        draw (x+r, y+1), decide this pixel is outside of the circle or inside, by
        r*r + 1*1 is bigger than r*r

        so the next step is left, draw (x+r-1, 1) decide it is in or out again, if it is in, go up, if it is out, go left until you reach (x, y+r)

        now you got the 1/4 circle. Do the same thing four times, you now get your full circle.