<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The Osmosian Order of Plain English Programmers Welcomes You]]></provider_name><provider_url><![CDATA[http://osmosianplainenglishprogramming.blog]]></provider_url><author_name><![CDATA[gerryrzeppa]]></author_name><author_url><![CDATA[https://osmosianplainenglishprogramming.blog/author/gerryrzeppa/]]></author_url><title><![CDATA[Bresenham&#8217;s Circle Drawing&nbsp;Algorithm]]></title><type><![CDATA[link]]></type><html><![CDATA[<header class="entry-header">
<h1 class="entry-title"></h1>
</header>
<div class="entry-content">
<p><strong>A Quick Comparison</strong></p>
<p>There are lots of great articles about Bresenham’s Circle Drawing Algorithm. <a href="https://www.geeksforgeeks.org/bresenhams-circle-drawing-algorithm/" target="_blank" rel="noopener">Here</a>, for instance. So I won’t describe the theory in this article. Instead, I’ll show a Plain English implementation of the same, and compare it with this C version from the aforementioned article:</p>
</div>
<div class="entry-content">
<div>
<div id="highlighter_68672" class="syntaxhighlighter  c">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="code">
<div class="container">
<div class="line number1 index0 alt2"><code class="c preprocessor">#include (some obscure library name) </code></div>
<div class="line number2 index1 alt1"><code class="c preprocessor">#include (some obscure library name)</code></div>
<div class="line number3 index2 alt2"><code class="c preprocessor">#include (some obscure library name)</code></div>
<div class="line number4 index3 alt1"><code class="c spaces"> </code></div>
<div class="line number5 index4 alt2"><code class="c comments">// Function to put pixels</code></div>
<div class="line number6 index5 alt1"><code class="c comments">// at subsequence points</code></div>
<div class="line number7 index6 alt2"><code class="c keyword bold">void</code><code class="c plain">drawCircle(</code><code class="c color1 bold">int</code><code class="c plain">xc, </code><code class="c color1 bold">int</code><code class="c plain">yc, </code><code class="c color1 bold">int</code><code class="c plain">x, </code><code class="c color1 bold">int</code><code class="c plain">y)</code></div>
<div class="line number8 index7 alt1"><code class="c plain">{</code></div>
<div class="line number9 index8 alt2"><code class="c spaces">    </code><code class="c plain">putpixel(xc+x, yc+y, RED);</code></div>
<div class="line number10 index9 alt1"><code class="c spaces">    </code><code class="c plain">putpixel(xc-x, yc+y, RED);</code></div>
<div class="line number11 index10 alt2"><code class="c spaces">    </code><code class="c plain">putpixel(xc+x, yc-y, RED);</code></div>
<div class="line number12 index11 alt1"><code class="c spaces">    </code><code class="c plain">putpixel(xc-x, yc-y, RED);</code></div>
<div class="line number13 index12 alt2"><code class="c spaces">    </code><code class="c plain">putpixel(xc+y, yc+x, RED);</code></div>
<div class="line number14 index13 alt1"><code class="c spaces">    </code><code class="c plain">putpixel(xc-y, yc+x, RED);</code></div>
<div class="line number15 index14 alt2"><code class="c spaces">    </code><code class="c plain">putpixel(xc+y, yc-x, RED);</code></div>
<div class="line number16 index15 alt1"><code class="c spaces">    </code><code class="c plain">putpixel(xc-y, yc-x, RED);</code></div>
<div class="line number17 index16 alt2"><code class="c plain">}</code></div>
<div class="line number18 index17 alt1"><code class="c spaces"> </code></div>
<div class="line number19 index18 alt2"><code class="c comments">// Function for circle-generation</code></div>
<div class="line number20 index19 alt1"><code class="c comments">// using Bresenham's algorithm</code></div>
<div class="line number21 index20 alt2"><code class="c keyword bold">void</code><code class="c plain">circleBres(</code><code class="c color1 bold">int</code><code class="c plain">xc, </code><code class="c color1 bold">int</code><code class="c plain">yc, </code><code class="c color1 bold">int</code><code class="c plain">r)</code></div>
<div class="line number22 index21 alt1"><code class="c plain">{</code></div>
<div class="line number23 index22 alt2"><code class="c spaces">    </code><code class="c color1 bold">int</code><code class="c plain">x = 0, y = r;</code></div>
<div class="line number24 index23 alt1"><code class="c spaces">    </code><code class="c color1 bold">int</code><code class="c plain">d = 3 - 2 * r;</code></div>
<div class="line number25 index24 alt2"><code class="c spaces">    </code><code class="c keyword bold">while</code><code class="c plain">(y &gt;= x)</code></div>
<div class="line number26 index25 alt1"><code class="c spaces">    </code><code class="c plain">{</code></div>
<div class="line number27 index26 alt2"><code class="c spaces">        </code><code class="c comments">// for each pixel we will</code></div>
<div class="line number28 index27 alt1"><code class="c spaces">        </code><code class="c comments">// draw all eight pixels</code></div>
<div class="line number29 index28 alt2"><code class="c spaces">        </code><code class="c plain">drawCircle(xc, yc, x, y);</code></div>
<div class="line number30 index29 alt1"><code class="c spaces">        </code><code class="c plain">x++;</code></div>
<div class="line number31 index30 alt2"><code class="c spaces"> </code></div>
<div class="line number32 index31 alt1"><code class="c spaces">        </code><code class="c comments">// check for decision parameter</code></div>
<div class="line number33 index32 alt2"><code class="c spaces">        </code><code class="c comments">// and correspondingly </code></div>
<div class="line number34 index33 alt1"><code class="c spaces">        </code><code class="c comments">// update d, x, y</code></div>
<div class="line number35 index34 alt2"><code class="c spaces">        </code><code class="c keyword bold">if</code><code class="c plain">(d &gt; 0)</code></div>
<div class="line number36 index35 alt1"><code class="c spaces">        </code><code class="c plain">{</code></div>
<div class="line number37 index36 alt2"><code class="c spaces">            </code><code class="c plain">y--; </code></div>
<div class="line number38 index37 alt1"><code class="c spaces">            </code><code class="c plain">d = d + 4 * (x - y) + 10;</code></div>
<div class="line number39 index38 alt2"><code class="c spaces">        </code><code class="c plain">}</code></div>
<div class="line number40 index39 alt1"><code class="c spaces">        </code><code class="c keyword bold">else</code></div>
<div class="line number41 index40 alt2"><code class="c spaces">            </code><code class="c plain">d = d + 4 * x + 6;</code></div>
<div class="line number42 index41 alt1"><code class="c spaces">        </code><code class="c plain">drawCircle(xc, yc, x, y);</code></div>
<div class="line number43 index42 alt2"><code class="c spaces">        </code><code class="c plain">delay(50);</code></div>
<div class="line number44 index43 alt1"><code class="c spaces">    </code><code class="c plain">}</code></div>
<div class="line number45 index44 alt2"><code class="c plain">}</code></div>
<div class="line number46 index45 alt1"><code class="c spaces"> </code></div>
<div class="line number47 index46 alt2"><code class="c spaces"> </code></div>
<div class="line number48 index47 alt1"><code class="c comments">// driver function</code></div>
<div class="line number49 index48 alt2"><code class="c color1 bold">int</code><code class="c plain">main()</code></div>
<div class="line number50 index49 alt1"><code class="c plain">{</code></div>
<div class="line number51 index50 alt2"><code class="c spaces">    </code><code class="c color1 bold">int</code><code class="c plain">xc = 50, yc = 50, r2 = 30;</code></div>
<div class="line number52 index51 alt1"><code class="c spaces">    </code><code class="c color1 bold">int</code><code class="c plain">gd = DETECT, gm;</code></div>
<div class="line number53 index52 alt2"><code class="c spaces">    </code><code class="c plain">initgraph(&amp;gd, &amp;gm, </code><code class="c string">""</code><code class="c plain">);  </code><code class="c comments">// initialize graph</code></div>
<div class="line number54 index53 alt1"><code class="c spaces">    </code><code class="c plain">circleBres(xc, yc, r);    </code><code class="c comments">// function call</code></div>
<div class="line number55 index54 alt2"><code class="c spaces">    </code><code class="c keyword bold">return</code><code class="c plain">0;</code></div>
<div class="line number56 index55 alt1"><code class="c plain">}</code></div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Now here’s the Plain English version:</p>
<p><span style="color:#00ccff;">To run:</span><br />
<span style="color:#00ccff;"> Start up.</span><br />
<span style="color:#00ccff;"> Clear the screen.</span><br />
<span style="color:#00ccff;"> Draw a circle using the screen&#8217;s center and 3 inches.</span><br />
<span style="color:#00ccff;"> Refresh the screen.</span><br />
<span style="color:#00ccff;"> Wait for the escape key.</span><br />
<span style="color:#00ccff;"> Shut down.</span></p>
<p><span style="color:#00ccff;">To draw a circle with a center spot and a radius:</span><br />
<span style="color:#00ccff;"> Put the radius and 0 into a spot.</span><br />
<span style="color:#00ccff;"> Put the radius times -2 plus 1 pixel with 1 pixel into a change pair.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> If the spot&#8217;s y is greater than the spot&#8217;s x, break.</span><br />
<span style="color:#00ccff;"> Plot eight spots given the center and the spot.</span><br />
<span style="color:#00ccff;"> Add 1 pixel to the spot&#8217;s y.</span><br />
<span style="color:#00ccff;"> Add the change&#8217;s y to some error twips.</span><br />
<span style="color:#00ccff;"> Add 2 pixels to the change&#8217;s y.</span><br />
<span style="color:#00ccff;"> If the error times 2 plus the change&#8217;s x is less than or equal to 0, repeat.</span><br />
<span style="color:#00ccff;"> Subtract 1 pixel from the spot&#8217;s x.</span><br />
<span style="color:#00ccff;"> Add the change&#8217;s x to the error.</span><br />
<span style="color:#00ccff;"> Add 2 pixels to the change&#8217;s x.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p><span style="color:#00ccff;">To plot eight spots given a center spot and a spot:</span><br />
<span style="color:#00ccff;"> Privatize the spot.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Plot the center plus the spot.</span><br />
<span style="color:#00ccff;"> Negate the spot&#8217;s x. Plot the center plus the spot.</span><br />
<span style="color:#00ccff;"> Negate the spot&#8217;s y. Plot the center plus the spot.</span><br />
<span style="color:#00ccff;"> Negate the spot&#8217;s x. Plot the center plus the spot.</span><br />
<span style="color:#00ccff;"> Negate the spot&#8217;s y.</span><br />
<span style="color:#00ccff;"> Flip the spot. If a counter is past 1, break.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p>Lines of code: Plain English, 33; C, 56.<br />
Almost 70% more in the C version.</p>
<p>Characters: Plain English, 1048; C, 1209.<br />
About 15% more in the C version.</p>
<p>Now please don’t get me wrong. C is a great language if you don’t mind memorizing (and/or looking up) how to type things like:</p>
<pre><code>initgraph(&amp;gd, &amp;gm, "");</code></pre>
<p>It just seems kind of <em>foreign </em>and <em>unnatural </em>to me. After all, my native tongue is English, and when I’m thinking about a program, I typically think in English “pseudo-code.” The cool thing about Plain English is that the pseudo-code I had in mind actually runs.</p>
<p>Bear with me, please. This next part is really interesting.</p>
<p><strong>Mathemagician Pie</strong></p>
<p>As you may know from a previous article of mine, I joined the Mathemagician’s Club in my youth. This is my membership card:</p>
<p><img loading="lazy" class="alignnone size-full wp-image-318984" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/degree-small.png" alt="" width="384" height="302" /></p>
<p>But I quickly became an outcast for taking Kronecker’s side in the Kronecker/Cantor kerfuffle. Which is why, decades later, Plain English is still an integer-only language. Baby steps toward a more <em>rational </em>and <em>natural </em>mathematics. And with the algorithms above, we now have what we need to take another baby step.</p>
<p>Pi, in the mathemagician’s world, is defined as the circumference of a circle, c, divided by the circle’s diameter, d, thus:</p>
<pre><code>Pi = c/d</code></pre>
<p>It is a nasty, transcendental (ie, unreal) number that can’t be fully calculated by anyone or anything. But using the routines above, we <em>are </em>able to calculate precise values for c/d, because we’re dealing, not with mathemagical abstractions, but with real-life pixels that we can actually see and count (using whole numbers).</p>
<p>The question is, How should we measure the circumference of a circle? I know of two, obvious, whole-number methods: (1) we can count the pixels we draw, or (2) we can calculate the sum of the Minkowski distances between those pixels. These are the figures we get for circles of increasing diameter using each method:</p>
<p><img loading="lazy" class="alignnone size-full wp-image-329604" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/pixel-count-vs-minkowski-circumference.jpg" alt="" width="524" height="525" /></p>
<p>If we figure the circumference using a pixel count circumference, we get a rational value for Pi that quickly converges to 2 and 5/6. On the other hand, if we calculate the circumference by tracing a Minkowski path through those pixels, we get a rational value for Pi that is very close to 4. This is exactly what we’d expect. The Pixel Count Pi is a little less than the mathemagician’s value, and the Minkowski Pi is a little more. But both are rational numbers based on real-world pixels.</p>
<p>A “pixelated” circle, I hope you can see, is <em>not </em>an approximation of a mathemagician’s “ideal” circle; that’s putting the cart before the horse. It’s the mathemagician’s <em>imaginary </em>circle that approximates the real-life pixelated circle. Which, as we’ve just seen, can be efficiently drawn, and measured, and analyzed, in Plain English, using whole numbers only.</p>
<p>Call me crazy, but don’t call me late for dinner!</p>
</div>
]]></html><thumbnail_url><![CDATA[https://i1.wp.com/cdncontribute.geeksforgeeks.org/wp-content/uploads/degree-small.png?fit=440%2C330&ssl=1]]></thumbnail_url><thumbnail_width><![CDATA[384]]></thumbnail_width><thumbnail_height><![CDATA[302]]></thumbnail_height></oembed>