{"id":853,"date":"2014-07-26T00:01:15","date_gmt":"2014-07-26T07:01:15","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=853"},"modified":"2014-07-19T07:48:46","modified_gmt":"2014-07-19T14:48:46","slug":"plotting-a-circle","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=853","title":{"rendered":"Plotting a Circle"},"content":{"rendered":"<p>Circles are easy to draw by hand, especially when you have a compass. On a computer, the Circle tool is common in graphics programs, although it&#8217;s really the Ellipse tool. And when programming, well, you&#8217;ll have to plot your own circle &#8212; especially when pulling a stunt like drawing graphics in text mode.<br \/>\n<!--more--><br \/>\nUnlike a simple wave, the C library holds no mathematical formula for plotting a circle. Instead, as with plotting a line, you have embrace the raw mathematical formula for a circle, which is:<\/p>\n<blockquote><p>x&sup2; + y&sup2; = r<\/p><\/blockquote>\n<p>Variables <em>x<\/em> and <em>y<\/em> are the circle&#8217;s coordinates. Variable <em>r<\/em> represents the radius or, more precisely, the square root of <em>r<\/em> is the radius. Figure 1 illustrates a basic circle plotted using the Grapher program on the Macintosh.<\/p>\n<div id=\"attachment_858\" style=\"width: 483px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-858\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure11.png\" alt=\"Figure 1. The circle formula.\" width=\"473\" height=\"439\" class=\"size-full wp-image-858\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure11.png 473w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure11-300x278.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure11-323x300.png 323w\" sizes=\"auto, (max-width: 473px) 100vw, 473px\" \/><p id=\"caption-attachment-858\" class=\"wp-caption-text\">Figure 1. The circle formula.<\/p><\/div>\n<p>If you&#8217;re going to plot coordinates from left to right on the Cartesian plane, you need to solve the circle equation for <em>y<\/em>. By my math memory, that would be:<\/p>\n<blockquote><p>y = &radic;<span style=\"text-decoration:overline\">r-x&sup2;<\/span><\/p><\/blockquote>\n<p>A mathematician would cringe at that solution, because it&#8217;s kinda wrong: If you plot that equation, you get only the top half of the circle. To get the bottom half you also have to take into account negative values of <em>y<\/em>. That&#8217;s okay, because all you need to plot a circle on a computer is the top half anyway, as the following code demonstrates.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n#define WIDTH 60\r\n#define HEIGHT 20\r\n#define X WIDTH\/2\r\n#define Y HEIGHT\/2\r\n#define XMAX WIDTH-X-1\r\n#define XMIN -(WIDTH-X)\r\n#define YMAX HEIGHT-Y\r\n#define YMIN -(HEIGHT-Y)+1\r\n\r\nchar grid[HEIGHT][WIDTH];\r\n\r\nint circle(int x, int y, int radius);\r\nint plot(int x, int y);\r\nvoid init_grid(void);\r\nvoid show_grid(void);\r\n\r\nint main()\r\n{\r\n    init_grid();\r\n    circle(0,0,8);\r\n    show_grid();\r\n\r\n    return(0);\r\n}\r\n\r\n<span class=\"comments\">\/* Plot a circle *\/<\/span>\r\nint circle(int x, int y, int radius)\r\n{\r\n    float xpos,ypos,radsqr,xsqr;\r\n\r\n    for(xpos=x-radius;xpos&lt;=x+radius;xpos+=0.1)\r\n    {\r\n        radsqr = pow(radius,2);\r\n        xsqr = pow(xpos,2);\r\n        ypos = sqrt(abs(radsqr - xsqr));\r\n        plot(rintf(xpos),rintf(ypos));\r\n        plot(rintf(xpos),rintf(-ypos));\r\n    }\r\n\r\n    return(1);\r\n}\r\n\r\n<span class=\"comments\">\/* Set \"pixel\" at specific coordinates *\/<\/span>\r\nint plot(int x, int y)\r\n{\r\n    if( x &gt; XMAX || x &lt; XMIN || y &gt; YMAX || y &lt; YMIN )\r\n        return(-1);\r\n\r\n    grid[Y-y][X+x] = '*';\r\n    return(1);\r\n}\r\n\r\n<span class=\"comments\">\/* Initialize grid *\/<\/span>\r\nvoid init_grid(void)\r\n{\r\n    int x,y;\r\n\r\n    for(y=0;y&lt;HEIGHT;y++)\r\n        for(x=0;x&lt;WIDTH;x++)\r\n            grid[y][x] = ' ';\r\n    <span class=\"comments\">\/* draw the axis *\/<\/span>\r\n    for(y=0;y&lt;HEIGHT;y++)\r\n        grid[y][X] = '|';\r\n    for(x=0;x&lt;WIDTH;x++)\r\n        grid[Y][x] = '-';\r\n    grid[Y][X] = '+';\r\n}\r\n\r\n<span class=\"comments\">\/* display grid *\/<\/span>\r\nvoid show_grid(void)\r\n{\r\n    int x,y;\r\n\r\n    for(y=0;y&lt;HEIGHT;y++)\r\n    {\r\n        for(x=0;x&lt;WIDTH;x++)\r\n            putchar(grid[y][x]);\r\n        putchar('\\n');\r\n    }\r\n}<\/pre>\n<p>The <em>circle()<\/em> function plots the circle by using the equation <em>y = &radic;<span style=\"text-decoration:overline\">r-x&sup2;<\/span><\/em>. Here&#8217;s how that breaks down, starting at Line 35:<\/p>\n<p><code>for(xpos=x-radius;xpos&lt;=x+radius;xpos+=0.1)<\/code><\/p>\n<p>The <em>for<\/em> loop marches along the <em>x<\/em> axis based on the <code>radius<\/code> argument. I use the full value of <code>radius<\/code> here, so I&#8217;m not exactly following the formula. That&#8217;s okay; the <em>circle()<\/em> function asks for a radius value and not the square root of that value. Also, the stepping value is 0.1, which ensures that something more-or-less resembling a circle is drawn.<\/p>\n<p>Lines 37 and 38 obtain the square of the <code>radius<\/code> and <code>xpos<\/code> variables by using the <em>pow()<\/em> function:<\/p>\n<p><code>radsqr = pow(radius,2);<br \/>\nxsqr = pow(xpos,2);<\/code><\/p>\n<p>I didn&#8217;t have to use the <code>radsqr<\/code> or <code>xsqr<\/code> variables; these could have been calculated on the next line, but doing so makes that statement rather messy. Also, you could use <code>radius*radius<\/code> instead of <code>pow(radius,2)<\/code>, but I prefer using the <em>pow()<\/em> function. Remember: Unlike other programming languages, C lacks a power operator.<\/p>\n<p>Line 39 calculates the <em>y<\/em> position by using the formula <em>y = &radic;<span style=\"text-decoration:overline\">r-x&sup2;<\/span><\/em>:<\/p>\n<p><code>ypos = sqrt(abs(radsqr - xsqr));<\/code><\/p>\n<p>The <em>abs()<\/em> function calculates the absolute value (positive numbers only) of <code>radsqr<\/code> minus <code>xsqr<\/code>. It&#8217;s necessary because that value could be negative, which would cause the <em>sqrt()<\/em> function to <em>squirt<\/em> all over the compiler. And because this formula returns only positive values (the top of the circle), the next two <em>plot()<\/em> statements (Lines 40 and 41) must plot both positive and negative values of <code>ypos<\/code>:<\/p>\n<p><code>plot(rintf(xpos),rintf(ypos));<br \/>\nplot(rintf(xpos),rintf(-ypos));<\/code><\/p>\n<p>And the circle is drawn. Sample output is shown in Figure 2. Yes, it&#8217;s an oval, which is due to the proportions of single characters in text mode graphics. (See if you can figure out how to compensate for that in the code!)<\/p>\n<div id=\"attachment_879\" style=\"width: 436px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-879\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0726-figure2.png\" alt=\"Figure 2. The circle is plotted by using text mode graphics.\" width=\"426\" height=\"321\" class=\"size-full wp-image-879\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0726-figure2.png 426w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0726-figure2-300x226.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0726-figure2-398x300.png 398w\" sizes=\"auto, (max-width: 426px) 100vw, 426px\" \/><p id=\"caption-attachment-879\" class=\"wp-caption-text\">Figure 2. The circle is plotted by using text mode graphics.<\/p><\/div>\n<p>The problem?<\/p>\n<p>Oh, yeah: Come on, you <em>knew<\/em> that there would be a problem!<\/p>\n<p>The current rendition of the <em>circle()<\/em> function assumes a focus of 0,0. If you want to plot the center of the circle at location 3,8, you need to modify the <em>circle()<\/em> function, specifically Lines 38, 40, and 41. Here&#8217;s the revised <em>circle()<\/em> function:<\/p>\n<pre><code>\r\n\/* Plot a circle *\/\r\nint circle(int x, int y, int radius)\r\n{\r\n    float xpos,ypos,radsqr,xsqr;\r\n\r\n    for(xpos=x-radius;xpos<=x+radius;xpos+=0.1)\r\n    {\r\n        radsqr = pow(radius,2);\r\n        xsqr = pow(xpos<span class=\"comments\">-x<\/span>,2);\r\n        ypos = sqrt(abs(radsqr - xsqr));\r\n        plot(rintf(xpos),rintf(ypos)<span class=\"comments\">+y<\/span>);\r\n        plot(rintf(xpos),rintf(-ypos)<span class=\"comments\">+y<\/span>);\r\n    }\r\n\r\n    return(1);\r\n}<\/code><\/pre>\n<p>Or you can <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0726b.c\">click here<\/a> to download the full source code file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s how to graph a circle in text mode. Crazy? Of course! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=853\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-853","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=853"}],"version-history":[{"count":13,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/853\/revisions"}],"predecessor-version":[{"id":881,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/853\/revisions\/881"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}