Draw a circle - programming

Question posted in Computer Software on 05 2010
Rate question difficulty level 1 Votes
Draw a circle on the screen given the radius and center point as well as a function called DrawPoint which accepts a point to be drawn on the screen. The function should not use a square root function or floating point data types (integer types only).
 
 
11 Answers
 
The candidate should come up with something similar to the Midpoint circle algorithm also commonly referred to as Bresenham's Circle algorithm because of the similarities to his line algorithm.
Some other good questions regard reading code and detmining whether it not it will compile. If it will compile what will be the output, if it won't why not.
These questions shouldn't be a quiz of Library functions, just simple syntax. No complicated long lines of multiple functions crammed into a single line or anything mis-leading

05/22/2010
 
 
The answer here is commonly referred to as Bresenham's Circle algorithm due to the similarities with his Line algorithm, but is actually called the Midpoint Circle algorithm.
I previously gave this answer in email to Shirley and wanted to elaborate further.
This can be difficult to come up with in an interview without memorizing, so to work toward it (which is what the interviewer wants to see) try this.
Really the interviewer is looking for you to move from x^2 + y^2 = R^2 to x^2 + y^2 - R^2 = 0.
You should calculate R^2 only once at function init. The toughest part is to get the y from without simply solving for y^2 then taking the square root. (sadly I drew blank on this in the interview and needless to say, I did not impress that interviewer).
You also don't want to calculate every point in the circle or draw the same point twice, so start by calling DrawPoint(center.x + r, center.y), DrawPoint(center.x - r, center.y), DrawPoint(center.x, center.y + r), and DrawPoint(center.x, center.y -r).
Then you only have to calculate the points for 1/8th of the circle.
First translate to 0,0.
Then iterate from the x value 1-r //this is 0-r+1 since we already calculated the first point
and iterate up to -r/2 //this is the 1/8th point halfway between 0 and 0-r
so that looks like this
for (int x = 1-r; x <= -r/2; x++)
//call the function that you wrote to solve for y from x^2 and your previously calculated rSquared value
//now remember that we are working from 0,0 so we have to shift our answers back to center and draw the Points that are easily mirrored from your answers
DrawPoint(new Point(x, y) + center)
DrawPoint(new Point(-x,y) + center)
DrawPoint(new Point(x, -y) + center)
DrawPoint(new Point(-x, -y) + center)
DrawPoint(new Point(x, y) + center)
DrawPoint(new Point(center - new Point(x,0))
DrawPoint(new Point(center + new Point(x,0))
DrawPoint(new Point(center - new Point(0, y))
DrawPoint(new Point(center + new Point(0, y))

or something like this, wife is rushing me, g2g.
I would also do this without all the new Point stuff to prevent local storage creation by just using the x and y values and calculating inside the parens or another efficient method for doing the same.

Have fun with the solving for y, it was really easy when I looked at it again, so i won't spoil it here, but once you look at Bresenham's you'll see what I mean

05/22/2010
 
 
I knew I should've spent a little more time looking at that. sorry, the 8 points you want to draw are:
(x,y) + center
(y,x) + center
(-x,y) + center
(-y,x) + center
(x,-y) + center
(y, -x) + center
(-x,-y) + center and
(-y,-x) + center

05/24/2010
 
 
Thanks for such an informative article about programming here. This problem is really important to every human being in this world so such articles like that can really open our eyes and show how the reality goes there. Thanks one more time for posting this one here and keep the job in the same success in the future too. David R. from viagra

04/20/2012
 
 
Wow, very impressive publication here. Moreover as I see, this question is very actual for many people indeed. Personally I totally agree with author opinion about this subject and I think that it would be really interesting to create such discussion with other this website visitors.
no fax payday loans

04/21/2012
 
 
Awesome information. I am really surprised with this topic. Keep up the good work and post more here to read.

consolidate guaranteed no fax payday loans

04/22/2012
 
 

online with instant approval with no faxing payday loans


Really i am impressed from this post. The person who created this post is a genius and knows how to keep the readers connected. Thank you for sharing this with us. I found it informative and interesting. Looking forward for more updates.

04/30/2012
 
 
I wonder how you got so good. This is really a fascinating blog, lots of stuff that I can Get into. One thing I just want to say is that your Blog is so perfect! cheap calling cards

05/11/2012
 
 
Your content is very useful. Thank you so much for providing plenty of useful content. I have bookmarked your site and will be without doubt coming back. Once again, I appreciate all your work and also providing a lot vital tricks for your readers. Football Predictions | Asian handicap

05/11/2012
 
 
I suggest this site to my friends so it could be useful for them also. Great effort Orange International Calls | free calls to South Africa

05/11/2012
 
 
Coach Outlet is an American leather goods company famous for its ladies handbags and purses. Coach Factory Brand may be creating Coach Bags styles which are absolutely multicolored and fun. Here, we talk about Coach Outlet Online comes in different colors and sizes, which means that you can go in for Coach Outlet Online one that suits you the most. Made up of Coach Factory Outlet various rare materials of Coach Tote Bags, Our Coach Outlet has a variety of collections of Cheap Coach Shoulder Bags.

05/17/2012
 
 
Add an answer*
 
Your name
Email
 
Location: United States
developer programming

add a question

arrow_blue


Now hiring!
---------------------------
---------------------------
---------------------------
PwC 
---------------------------
---------------------------
---------------------------
---------------------------