0

Is there any function I can create in order to draw a filled circle using the base drawing functions:

  • putpixel(x, y);
  • lineto(x, y);

?


Note that it must use trigonometric functions (videlicet sin, cos etc..) so I can easily turn it into a hexagon and even further - a square or another type of polygon. enter image description here


And finally, not necessarily but always appreciated if it isn't very slow in drawing (one loop should be enough I presume) and even more not necessarily, but always even more appreciated if it isn't too complex and/or consisting of more lines than needed for a lightweight life.

Imobilis
  • 1,475
  • 8
  • 29
  • 1
    Searching in google for such function without success. Found only for either non-filled circle, library-specific or badly implemented functionresulting with strange ellipse. – Imobilis Aug 03 '15 at 20:30
  • 1
    @MartinJames Inaccurate. Neither the followed by and the approved answer lie on trigonometry. – Imobilis Aug 03 '15 at 20:33
  • 1
    Really? I Googled for 'C draw a filled circle' and got: 'About 35,900,000 results', topped by several previous SO Q&A. What did you Google for? – Martin James Aug 03 '15 at 20:33
  • 1
    Similarly to what you did. If you have found C, standard drawing functions, filled circle, using trigonometric functions and you can prove that I can easily disfigure it to the relative primitive shape, why don't you help me/us by providing a reference to it. – Imobilis Aug 03 '15 at 20:33
  • 1
    Draw a circle with sin/cos, incrementally reduce the radius, rinse, repeat. – Martin James Aug 03 '15 at 20:36
  • 1
    The duplicate being marked doesn't answer the question! What's wrong with S/O these days, I don't remember it being like that before !! – Imobilis Aug 03 '15 at 20:38
  • Okay then. From now on, I will know that S/O is useful only to a users, provided a supreme question. – Imobilis Aug 03 '15 at 20:49
  • I was wrong. Pythagorean theorem, is geometry, but not trig, aprarrently:( – Martin James Aug 03 '15 at 20:51
  • Yes.. and the best way for one to differ it and answer to the question out of curiosity "why do I want it to lie on trigonometric functions" is to focus on what I want to do with that circle, which is now bolded in the question. – Imobilis Aug 03 '15 at 20:53
  • I guess that one way would be to fill X equilateral triangles with a common vertex so as to cover all 360 degs round it. In the limit of one pixel, the result would be circular. – Martin James Aug 03 '15 at 20:55
  • Use your sin/cos to identify the vertices, then fill with, say, http://stackoverflow.com/questions/11139724/i-need-a-pixel-perfect-triangle-fill-algorithm-to-avoid-aliasing-artifacts – Martin James Aug 03 '15 at 20:57
  • I withdrew my duplicate close vote on the grounds that I screwed up. – Martin James Aug 03 '15 at 21:01
  • My question might be a bit looking too specific, individual and intense for that I don't know the name of the shape-changing examination but is actually a pretty common thing, because I remember years ago when I played with such function I could easily turn it to square or square-alike figure only by changing its params. – Imobilis Aug 03 '15 at 21:05
  • The shape changing param is just the vertex count of the polygon. Small vertex counts like 3, 4, and 6 result in easily recognized shapes like triangles, squares, and hexagons. A large vertex count, like 20, simply looks like a circle. – user3386109 Aug 03 '15 at 21:25
  • you are right, it sounds like "n-polygon" still have to figure out how come I so easily succeeded to change it. – Imobilis Aug 03 '15 at 21:40

1 Answers1

2

The fastest way to draw a filled circle with lineto is as follows

for each value of y that lies within the circle
{
    compute the corresponding x values
    draw a line between (x1,y) and (x2,y)
}

enter image description here

To convert the circle to a polygon you can compute the vertices of the polygon based on the radius of the circles and angle to each vertex. For example, with a hexagon the angles are 0, 60, 120, 180, 240, and 300 degrees. With a square the angles are 45, 135, 225, and 315. Once you have the polygon vertices, you can compute the x1 and x2 values for each y value based on the equations of the lines between the vertices.

enter image description here

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • It is fine for me if it is pseudo-code, however, it will be awesome if you can show how can I configure it to unround angle so it starts looking like hexagon and decays into a square, because that method doesn't seem to provide any easy configuration for it, but was noted on the question. – Imobilis Aug 03 '15 at 20:51
  • Surely, drawing the locus of a polygon is fairly easy, given no. of sides and.. err.. something else to say how big it is:) – Martin James Aug 03 '15 at 21:06
  • I am not that good in geometry and trigonometry :( The concept is relatively clear to me but the way.. – Imobilis Aug 03 '15 at 21:10
  • @Malina Not sure if your last comment was in response to MartinJames comment or my edit? – user3386109 Aug 03 '15 at 21:20
  • it was to your edit and in general. – Imobilis Aug 03 '15 at 21:24
  • 2
    @Malina The code for this is just a whole bunch of nitpicking details based on some medium level mathematics. Being comfortable with the math is the first step. Specifically, you need to be able to compute points based on center, radius, and angle. And you need to be able to determine the equation of a line based on two points. The rest is left as an exercise for the reader. – user3386109 Aug 03 '15 at 21:34
  • I feel like I will never be able to create a function to draw n-polygon with adjustable n :-( – Imobilis Aug 03 '15 at 23:01