{"id":831,"date":"2014-07-19T00:01:05","date_gmt":"2014-07-19T07:01:05","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=831"},"modified":"2014-07-26T07:26:13","modified_gmt":"2014-07-26T14:26:13","slug":"graphing-curves-in-text-mode","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=831","title":{"rendered":"Graphing Curves in Text Mode"},"content":{"rendered":"<p>When it comes to plotting curves in the C language, you need to dust off your trigonometry. It&#8217;s not a scary thing: The C library offers a host of mathematical functions, many of which can help you plot a curve here or there. In fact, writing a function to draw a curve is a lot easier than plotting a straight line.<br \/>\n<!--more--><br \/>\nAs an example, consider a sine wave, such as the one shown in Figure 1.<\/p>\n<div id=\"attachment_833\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-833\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure1.png\" alt=\"Figure 1. A sine wave, plotting y=sin(x).\" width=\"400\" height=\"247\" class=\"size-full wp-image-833\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure1.png 400w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure1-300x185.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><p id=\"caption-attachment-833\" class=\"wp-caption-text\">Figure 1. A sine wave, plotting <em>y=sin(x)<\/em>.<\/p><\/div>\n<p>The sine wave is a basic curve, something people recognize, although not everyone may know that it&#8217;s called a <em>sine wave<\/em>.<\/p>\n<p>In Figure 1, you see a plot of the function <em>y=sin(x)<\/em>. Because <em>sin()<\/em> is also a C library function (declared in the <code>math.h<\/code> header), you can whip up a tiny little program that spews out all the coordinates necessary to plot that curve:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    float x,y;\r\n\r\n    for(x=-3.14159;x&lt;=3.14159;x+=1.0)\r\n    {\r\n        y = sin(x);\r\n        printf(\"%.2f\\t%.2f\\n\",x,y);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop at Line 8 steps through <code>x<\/code> values from negative &pi; through positive &pi;.<\/p>\n<p>The value of <code>y<\/code> is calculated as the sine of <code>x<\/code> at Line 10.<\/p>\n<p>In Line 11, the <em>x<\/em> and <em>y<\/em> &#8220;coordinates&#8221; are output, the coordinates you could use to plot the curve.<\/p>\n<p>Here&#8217;s the sample output:<\/p>\n<pre><code>-3.14\t-0.00\r\n-2.14\t-0.84\r\n-1.14\t-0.91\r\n-0.14\t-0.14\r\n0.86\t0.76\r\n1.86\t0.96\r\n2.86\t0.28<\/code><\/pre>\n<p>Obviously the line wouldn&#8217;t be very detailed given those coordinates; seven points doesn&#8217;t make a very pretty curve.<\/p>\n<p>The solution is to increase the stepping value in the <em>for<\/em> loop. Modify Line 8 to read:<\/p>\n<p><code>for(x=-3.14159;x&lt;=3.14159;x+=<span class=\"comments\">0.1<\/span>)<\/code><\/p>\n<p>The results are far more detailed when the stepping value is 0.1 instead of 1.0.<\/p>\n<p>To plot the curve using text mode graphics, you need to do two additional things.<\/p>\n<p>First, you need to increase the wave&#8217;s amplitude and wave length. The current numbers are accurate, but they won&#8217;t graph well in text mode. So you need to make the wave proportionally larger.<\/p>\n<p>Second, you need to convert the wave&#8217;s coordinates from floating point to integer; the <em>plot()<\/em> function accepts <em>int<\/em> values. For that conversion, I&#8217;ll use the <em>rintf()<\/em> function.<\/p>\n<blockquote><p>The <em>rintf()<\/em> function is a rounding function. It takes a floating point value and rounds it up or down to the nearest integer. <em>rintf()<\/em> requires the <code>math.h<\/code> header file.<\/p><\/blockquote>\n<p>Below you see the code I&#8217;ve concocted to plot and display a sine wave. This code uses the basic graphic functions from earlier Lessons, but it omits the line-plotting functions.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.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 plot(int x, int y);\r\nvoid init_grid(void);\r\nvoid show_grid(void);\r\n\r\nint main()\r\n{\r\n    float x,y;\r\n\r\n    init_grid();\r\n    for(x=-3.14159;x&lt;=3.14159;x+=0.1)\r\n    {\r\n        y = sin(x);\r\n        plot(rintf(x*10),rintf(y*8));\r\n    }\r\n    show_grid();\r\n\r\n    return(0);\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 key to drawing the curve is the <em>plot()<\/em> function. The code between Lines 24 through 28 calculates the points by using the <em>for<\/em> loop example shown earlier in this Lesson.<\/p>\n<p>Figure 2 shows the output.<\/p>\n<div id=\"attachment_837\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-837\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure2.png\" alt=\"Figure 2. A sine wave drawn by using text mode graphics.\" width=\"585\" height=\"414\" class=\"size-full wp-image-837\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure2.png 585w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure2-300x212.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure2-423x300.png 423w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><p id=\"caption-attachment-837\" class=\"wp-caption-text\">Figure 2. A sine wave drawn by using text mode graphics.<\/p><\/div>\n<p>Pretty!<\/p>\n<p>You could replace the <em>sin()<\/em> function in Line 26 with the <em>cos()<\/em> function to behold  a cosine wave. And if you&#8217;re too lazy for that, just look at Figure 3.<\/p>\n<div id=\"attachment_847\" style=\"width: 448px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-847\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure3.png\" alt=\"Figure 3. The plot of a cosine wave.\" width=\"438\" height=\"328\" class=\"size-full wp-image-847\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure3.png 438w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure3-300x224.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0719-figure3-400x300.png 400w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><p id=\"caption-attachment-847\" class=\"wp-caption-text\">Figure 3. The plot of a cosine wave.<\/p><\/div>\n<p>Of course, these curves are easy because the C library is doing the math. What&#8217;s a wee bit more difficult to plot is a circle. That code is presented in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=853\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Graphics aren&#8217;t all composed of straight lines. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=831\">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-831","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\/831","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=831"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/831\/revisions"}],"predecessor-version":[{"id":896,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/831\/revisions\/896"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}