{"id":771,"date":"2014-07-05T00:01:23","date_gmt":"2014-07-05T07:01:23","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=771"},"modified":"2014-07-12T07:32:14","modified_gmt":"2014-07-12T14:32:14","slug":"graphing-in-text-mode-part-ii-lines","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=771","title":{"rendered":"Graphing in Text Mode, Part II: Lines"},"content":{"rendered":"<p>So far in my text mode graphics demonstration, I&#8217;ve shown you code that presents a graphical grid, plus the <em>plot()<\/em> function that &#8220;draws&#8221; pixels in the grid. These are the basic building blocks you need to do graphics in text mode. The next step is to craft functions that create basic geometric shapes. The most basic of those is the <em>line<\/em>.<br \/>\n<!--more--><br \/>\nA line is defined by two points. It connects the two points in the shortest distance possible in two dimensions.<\/p>\n<p>The traditional computer graphics <em>line()<\/em> function takes at least four arguments: Position <em>x1<\/em>, <em>y1<\/em> for one point on the line, and position <em>x2<\/em>, <em>y2<\/em> for the other point. Additional arguments may set the line&#8217;s width, color, style, and so on, but basically two points are specified and the line is plotted betwixt them.<\/p>\n<p>The formula for plotting a line goes back to that High School algebra class you slept through:<\/p>\n<blockquote><p>y = mx + b<\/p><\/blockquote>\n<p>Where <em>y<\/em> is a function of the slope, <em>m<\/em>, by the corresponding <em>x<\/em> position. Value <em>b<\/em> is the vertical axis intercept.<\/p>\n<p>The most difficult part of converting the line equation into C code is the slope calculation. You&#8217;ll also discover &#8212; especially if you were coding this function on your own &#8212; that some conditions exist where the slope becomes undefined. I&#8217;ll burn that bridge when I come to it.<\/p>\n<p>To calculate the slope, you need to know the change between the <em>y<\/em> coordinates and <em>x<\/em> coordinates. That is, what&#8217;s the difference between <em>y1<\/em> and <em>y2<\/em> as well as <em>x1<\/em> and <em>x2<\/em>. The slope is the ratio of that change in <em>y<\/em> over the change in <em>x<\/em>.<\/p>\n<p>In Figure 1, you see two coordinates plotted in a Cartesian grid, (-4,3) and (3,-5). The equivalent <em>line()<\/em> function is also shown.<\/p>\n<div id=\"attachment_779\" style=\"width: 303px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-779\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0705-figure1.png\" alt=\"Figure 1. A line is plotted.\" width=\"293\" height=\"329\" class=\"size-full wp-image-779\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0705-figure1.png 293w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0705-figure1-267x300.png 267w\" sizes=\"auto, (max-width: 293px) 100vw, 293px\" \/><p id=\"caption-attachment-779\" class=\"wp-caption-text\">Figure 1. A line is plotted.<\/p><\/div>\n<p>To calculate the slope, or the difference between the starting and ending points on the Y axis, you take the ratio of the differences with respect to the <em>y<\/em> and <em>x<\/em> coordinates, as shown in Figure 2.<\/p>\n<div id=\"attachment_797\" style=\"width: 337px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-797\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/0705-figure2.png\" alt=\"Figure 2. The slope calculation is made.\" width=\"327\" height=\"371\" class=\"size-full wp-image-797\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/0705-figure2.png 327w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/06\/0705-figure2-264x300.png 264w\" sizes=\"auto, (max-width: 327px) 100vw, 327px\" \/><p id=\"caption-attachment-797\" class=\"wp-caption-text\">Figure 2. The slope calculation is made.<\/p><\/div>\n<p>From Figure 2, you see that the slope is <sup>-8<\/sup>\/<sub>7<\/sub>, or -1.14285714. That&#8217;s a negative slope, which means the line falls from left to right. A positive slope means the line rises from left to right.<\/p>\n<p>To code this concept, create a loop that crawls from position <em>x1<\/em> to <em>x2<\/em>, plotting values from <em>y1<\/em> to <em>y2<\/em> by modifying the <em>y<\/em> values with the slope. This solution is presented 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 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    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\">\/* 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\">\/* 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 basic skeleton for this code was presented in last week&#8217;s Lesson. The <em>line()<\/em> function, dwelling between Lines 29 and 52, is what&#8217;s new.<\/p>\n<p>Lines 35 through 37 calculate the <code>slope<\/code>. The change in <em>y<\/em> and <em>x<\/em> are calculated using absolute coordinates: Remember that the grid can have both positive and negative values.<\/p>\n<p>In Line 41, <code>y<\/code> is initialized as a floating point value. It could be an <em>int<\/em>, but text mode lines are crude. Therefore, the <em>float<\/em> calculation is used for more drawing precision.<\/p>\n<p>The <em>while<\/em> loop at Line 42 plots the line. Lines 46 and 48 handle the situation when the starting coordinates might not always be the leftmost coordinate.<\/p>\n<p>Figure 3 displays the result as shown on my terminal.<\/p>\n<div id=\"attachment_782\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-782\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0705-figure3.png\" alt=\"Figure 3. A lovely text mode graphical line.\" width=\"585\" height=\"414\" class=\"size-full wp-image-782\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0705-figure3.png 585w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0705-figure3-300x212.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/07\/0705-figure3-423x300.png 423w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><p id=\"caption-attachment-782\" class=\"wp-caption-text\">Figure 3. A lovely text mode graphical line.<\/p><\/div>\n<p>Feel free to plug in other values for the <em>line()<\/em> function in this code. See whether you can discover which two conditions throw the function. If not, I&#8217;ll be covering them <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=810\">next Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once the <em>plot()<\/em> function is crafted, writing basic (text) graphics routines is the next step. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=771\">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-771","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\/771","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=771"}],"version-history":[{"count":13,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/771\/revisions"}],"predecessor-version":[{"id":856,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/771\/revisions\/856"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}