{"id":810,"date":"2014-07-12T00:01:30","date_gmt":"2014-07-12T07:01:30","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=810"},"modified":"2014-07-19T07:33:33","modified_gmt":"2014-07-19T14:33:33","slug":"graphing-in-text-mode-part-iii-better-lines","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=810","title":{"rendered":"Graphing in Text Mode, Part III: Better Lines"},"content":{"rendered":"<p>After you create a point-plotting function, the next obvious graphical function to code is the line. Whether you&#8217;re graphing in text mode or using pixels, you&#8217;ll quickly find that two conditions exist where a line function just can&#8217;t draw a line.<br \/>\n<!--more--><br \/>\nThese two conditions are for the vertical and horizontal lines. Seems rather basic, right?<\/p>\n<p>I&#8217;ve modified the code from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=771\">last week&#8217;s Lesson<\/a> to draw a vertical line by changing Line 22 in the code to read:<\/p>\n<p><code>line(5,8,5,-8);<\/code><\/p>\n<p>I&#8217;d show you the output, but none exists; only an empty canvas grid is displayed.<\/p>\n<p>Likewise, a horizontal line won&#8217;t show up when you try this function:<\/p>\n<p><code>line(-10,8,10,8);<\/code><\/p>\n<p>Nada.<\/p>\n<p>The reason no line is drawn is because the slope calculation contains a zero: When the change in <em>y<\/em> is zero, the slope is <sup>0<\/sup>\/<sub><em>&Delta;x<\/em><\/sub>. And when the change in <em>x<\/em> is zero, the slope is <sup><em>&Delta;y<\/em><\/sup>\/<sub>0<\/sub>, which is undefined. (Honestly, I&#8217;m surprised the program doesn&#8217;t puke all over itself.)<\/p>\n<p>To properly code these two conditions, you must supply a pair of exceptions to the <em>line()<\/em> function. I call them the <em>horzline()<\/em> and <em>vertline()<\/em> functions, which you see in the following code.<\/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 line(int x1, int y1, int x2, int y2);\r\nint horzline(int x1, int x2, int y);\r\nint vertline(int x, int y1, int y2);\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    line(-4,3,3,-5);\r\n    line(-20,-5,20,-5);\r\n    line(-20,-9,20,9);\r\n    show_grid();\r\n\r\n    return(0);\r\n}\r\n\r\n<span class=\"comments\">\/* draw a line from point x1,y1 to x2,y2 *\/<\/span>\r\nint line(int x1, int y1, int x2, int y2)\r\n{\r\n    int x,deltaY,deltaX;\r\n    float slope,y;\r\n\r\n    <span class=\"comments\">\/* Calculate the slope *\/<\/span>\r\n    deltaY=(Y-y1)-(Y-y2);       <span class=\"comments\">\/* Calculate using absolute *\/<\/span>\r\n    deltaX=(X+x2)-(X+x1);       <span class=\"comments\">\/*  coordinates *\/<\/span>\r\n    slope=(float)deltaY\/(float)deltaX;\r\n\r\n    <span class=\"comments\">\/* check for certain exceptions *\/<\/span>\r\n    if( y1 == y2 )          <span class=\"comments\">\/* line is flat *\/<\/span>\r\n    {\r\n        horzline(x1,x2,y1);\r\n        return(1);\r\n    }\r\n    if( slope &gt; X)          <span class=\"comments\">\/* line is too steep *\/<\/span>\r\n    {\r\n        vertline(x1,y1,y2);\r\n        return(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* Plot the points on the line *\/<\/span>\r\n    x = x1;\r\n    y = (float)y1;\r\n    while( x!=x2 &amp;&amp; (int)y!=y2)\r\n    {\r\n        plot(x,(int)y);\r\n        y += slope;\r\n        if( x1 &lt; x2 )\r\n            x++;\r\n        if( x1 &gt; x2 )\r\n            x--;\r\n    }\r\n    return(1);\r\n}\r\n\r\n<span class=\"comments\">\/* Draw Horizontal Line *\/<\/span>\r\nint horzline(int x1, int x2, int y)\r\n{\r\n    int x;\r\n\r\n    for(x=x1;x&lt;=x2;x++)\r\n        plot(x,y);\r\n\r\n    return(1);\r\n}\r\n\r\n<span class=\"comments\">\/* Draw Vertical Line *\/<\/span>\r\nint vertline(int x, int y1, int y2)\r\n{\r\n    int y;\r\n\r\n    for(y=y1;y&lt;=y2;y++)\r\n        plot(x,y);\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    \/* draw the axis *\/\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>That&#8217;s a lot of code, but really it includes only a new version of the <em>line()<\/em> function plus the addition of the <em>horzline()<\/em> and <em>vertline()<\/em> functions, and their prototypes.<\/p>\n<p>The output is shown in Figure 1.<\/p>\n<div id=\"attachment_815\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-815\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0712-figure1.png\" alt=\"Figure 1. Three lines are drawn, including a horizontal line.\" width=\"585\" height=\"414\" class=\"size-full wp-image-815\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0712-figure1.png 585w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0712-figure1-300x212.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0712-figure1-423x300.png 423w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><p id=\"caption-attachment-815\" class=\"wp-caption-text\">Figure 1. Three lines are drawn, including a horizontal line.<\/p><\/div>\n<p>Line 44 checks to see whether the line is flat, or horizontal. If so, the <em>horzline()<\/em> function is run.<\/p>\n<p>Line 49 checks to see whether the line is too steep, or vertical. If so, the <em>vertline()<\/em> function is run.<\/p>\n<p>Both the <em>horzline()<\/em> and <em>vertline()<\/em> function draw a straight line. Each uses a <em>for<\/em> loop and the <em>plot()<\/em> function; no slope calculation is needed. <\/p>\n<p>Together, the <em>line()<\/em>, <em>vertline()<\/em>, and <em>horzline()<\/em> functions completes the line-plotting tools necessary for text-mode graphics. Another function you could craft would be the <em>box()<\/em> function, which draws a rectangle between two sets of coordinates, using the same arguments as the <em>line()<\/em> function. Feel free to code that one on your own.<\/p>\n<p>After lines come curves. In the <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=831\">next Lesson<\/a>, I&#8217;ll demonstrate a function that draws curves by using text mode graphics; all you need is the <em>plot()<\/em> function &#8212; and some trigonometry.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A <em>line()<\/em> function must anticipate two conditions where the line must be drawn manually. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=810\">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-810","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\/810","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=810"}],"version-history":[{"count":11,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/810\/revisions"}],"predecessor-version":[{"id":876,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/810\/revisions\/876"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}