{"id":761,"date":"2014-06-28T00:01:29","date_gmt":"2014-06-28T07:01:29","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=761"},"modified":"2014-07-05T08:22:19","modified_gmt":"2014-07-05T15:22:19","slug":"graphing-in-text-mode-part-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=761","title":{"rendered":"Graphing in Text Mode, Part I"},"content":{"rendered":"<p>In text mode graphics, a <em>char<\/em> buffer serves as the drawing board, allowing you to plot <em>x<\/em> and <em>y<\/em> coordinates. Before any plotting takes place, however, you need a proper canvas.<br \/>\n<!--more--><br \/>\nRather than create a simply <em>x<\/em>, <em>y<\/em> grid, I&#8217;m going to build a buffer based on Cartesian coordinates: Position 0,0 will be at the center of the grid. That location makes it easier to plot geometric shapes like circles and waves (which I&#8217;ll demonstrate in a future Lesson).<\/p>\n<p>The following code sets up such a Cartesian coordinate system:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.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\r\nint main()\r\n{\r\n    char grid[HEIGHT][WIDTH];\r\n    int x,y;\r\n\r\n<span class=\"comments\">\/* Initialize grid *\/<\/span>\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<span class=\"comments\">\/* display grid *\/<\/span>\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\r\n    return(0);\r\n}<\/pre>\n<p>Constants <code>X<\/code> and <code>Y<\/code> define the axes. A line is drawn horizontally and vertically, and a + (plus character) is inserted at the center. Figure 1 details the output.<\/p>\n<div id=\"attachment_763\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-763\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/TerminalScreenSnapz001.png\" alt=\"Figure 1. The canvas sports X and Y axes.\" width=\"585\" height=\"414\" class=\"size-full wp-image-763\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/TerminalScreenSnapz001.png 585w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/TerminalScreenSnapz001-300x212.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/TerminalScreenSnapz001-423x300.png 423w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><p id=\"caption-attachment-763\" class=\"wp-caption-text\">Figure 1. The canvas sports X and Y axes.<\/p><\/div>\n<p>A better way to write the code would be to have the initialize grid and display grid routines banished to their own functions. I&#8217;ve done that in the following code, which is merely an update to the first example:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.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\r\nchar grid[HEIGHT][WIDTH];\r\n\r\nvoid init_grid(void);\r\nvoid show_grid(void);\r\n\r\nint main()\r\n{\r\n    init_grid();\r\n    show_grid();\r\n\r\n    return(0);\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 code&#8217;s output is the same, but by creating the <em>init_grid()<\/em> and <em>show_grid()<\/em> functions, it makes it easier to read the code as well as to modify it in the future.<\/p>\n<p>The grid by itself is lonely. What it needs is a basic graphics function. Below, I&#8217;ve added the <em>plot()<\/em> function, which places a character (hard-coded as an asterisk) at a specific <em>x<\/em>, <em>y<\/em> position in the grid. Negative coordinates can be used, plus I added some bounds checking to ensure that nothing is plotted off the grid.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.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    init_grid();\r\n    plot(-3,-3);\r\n    plot(XMIN,YMIN);\r\n    plot(XMAX,YMIN);\r\n    plot(XMIN,YMAX);\r\n    plot(XMAX,YMAX);\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&tl;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>plot()<\/em> function starts at Line 32. First the coordinates are checked against the grid&#8217;s bounds (Line 34). Because constants are used, you can re-size the grid by changing the <code>WIDTH<\/code> and <code>HEIGHT<\/code> constants and not have to fuss with any further code modifications.<\/p>\n<p>The result of the bounds checking is that if the coordinate plotted is outside the grid, the function returns -1. Otherwise, the position in the grid is plotted at Line 37. Remember that negative and positive coordinates can be used, which you see when the code is run; the output is shown in Figure 2.<\/p>\n<div id=\"attachment_765\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-765\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/TerminalScreenSnapz002.png\" alt=\"Figure 2. A &quot;pixel&quot; is placed at all four corners of the grid, plus another at position -3, -3.\" width=\"585\" height=\"414\" class=\"size-full wp-image-765\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/TerminalScreenSnapz002.png 585w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/TerminalScreenSnapz002-300x212.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/TerminalScreenSnapz002-423x300.png 423w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><p id=\"caption-attachment-765\" class=\"wp-caption-text\">Figure 2. A &#8220;pixel&#8221; is placed at all four corners of the grid, plus another at position -3, -3.<\/p><\/div>\n<p>The &#8220;pixel&#8221; at -3, -3 isn&#8217;t exactly plotted equidistant, as it would be when using true Cartesian coordinates. That&#8217;s due to the width\/height ratio of a character in text mode. Still, it worked!<\/p>\n<p>The <em>plot()<\/em> function is the basis for all graphics output. By using it, you can concoct other functions to create text-mode graphics. I&#8217;ll show you more <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=771\">next Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first step to creating text mode graphics is to build a grid. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=761\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-761","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\/761","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=761"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/761\/revisions"}],"predecessor-version":[{"id":844,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/761\/revisions\/844"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}