User Tools

Site Tools


haas:spring2017:cprog:projects:cos0

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
haas:spring2017:cprog:projects:cos0 [2017/03/11 22:31] – [Program] wedgehaas:spring2017:cprog:projects:cos0 [2017/03/20 14:45] (current) – [How to view your image] wedge
Line 22: Line 22:
 Now that we've gotten through all of them, it is time to start applying those concepts so that deeper and further familiarity can be gained. This project is one such attempt at that. Now that we've gotten through all of them, it is time to start applying those concepts so that deeper and further familiarity can be gained. This project is one such attempt at that.
  
-We all know what a circle is, and in various math classes we've even explored various methods for doing things with circles (they have a radius, a circumference, a diameter), but how do would we enable the computer to draw a circle?+We all know what a circle is, and in various math classes we've even explored various methods for doing things with circles (they have a radius, a circumference, a diameter), but how would we enable the computer to draw a circle?
  
 One piece of that, obviously, is the math. One piece of that, obviously, is the math.
Line 62: Line 62:
 <code c> <code c>
 /* /*
- PROGRAM.c  - skeleton code to use the GD graphics library + cos0.c   - skeleton code to use the GD graphics library 
-              to create an image.+                        to create an image (circle of squares).
  *  *
- * To compile:  gcc -o PROGRAM PROGRAM.c -lgd + * To compile: type 'make' to compile the program 
- * To execute:  ./PROGRAM +         or: gcc --std=c99 -Wall -o cos0 cos0.c -lgd 
-         OR:  ./PROGRAM cos0.png  (creates in current directory)+ * To execute: ./cos0 ARGS
  *  *
-  Functions:  http://www.boutell.com/gd/manual2.0.33.html+ Documentation:  http://www.boutell.com/gd/manual2.0.33.html 
 +                 https://libgd.github.io/manuals/2.1.1/index/Functions.html
  */  */
 #include <stdio.h> #include <stdio.h>
 #include <stdlib.h> #include <stdlib.h>
 #include <string.h> #include <string.h>
 +#include <sys/stat.h>
 #include <gd.h> #include <gd.h>
  
Line 86: Line 88:
 int main(int argc, char **argv) int main(int argc, char **argv)
 { {
-    char                       *outfile;        // name out of the output file +        ////////////////////////////////////////////////////////////////// 
-    FILE                       *out;            // output file pointer +        // 
-    gdImagePtr                  img;            // GD Image Construct +        // Declare variables 
-    unsigned int                color[5];       // color array +        // 
-    unsigned short int          wide, high;     // image attributes+        char               *outfile;    // name out of the output file 
 +        FILE               *out;        // output file pointer 
 +        gdImagePtr          img;        // GD Image Construct 
 +        unsigned int        color[5];   // color array 
 +        unsigned short int  wide, high; // image attributes
  
-    if (argc == 2) +        ////////////////////////////////////////////////////////////////// 
-    +        // 
-        outfile = *(argv+1); +        // Check arguments 
-        fprintf(stdout, "Using '%s' as output filename\n", outfile); +        // 
-    +        if (argc     == 2) 
-    else +        
-    +                outfile   = *(argv+1); 
-        outfile = (char *) malloc (sizeof(char) * 64); +                fprintf(stdout, "Using '%s' as output filename\n", outfile); 
-        strcpy(outfile, "/home/USERNAME/public_html/cos0.png"); +        
-    }+        else 
 +        
 +                outfile   = (char *) malloc (sizeof(char) * 64); 
 +                sprintf(outfile, "/home/%s/public_html/cos0.png", getenv("USER")); 
 +        }
  
-    // image dimensions +        ////////////////////////////////////////////////////////////////// 
-    // +        // 
-    wide = 800+        // image dimensions 
-    high = 600;+        // 
 +        wide          600
 +        high          800;
  
-    // Create new image of specified wide-ness and high-ness +        ////////////////////////////////////////////////////////////////// 
-    // +        // 
-    img = gdImageCreate(wide, high);+        // Create new image of specified wide-ness and high-ness 
 +        // 
 +        img           = gdImageCreate(wide, high);
  
-    // My GD color definitions +        ////////////////////////////////////////////////////////////////// 
-    // +        // 
-    color[BLACK]     = gdImageColorAllocate(img, 0x00, 0x00, 0x00); +        // Some GD color definitions 
-    color[BLUE]      = gdImageColorAllocate(img, 0x00, 0x00, 0xFF); +        // 
-    color[GREEN]     = gdImageColorAllocate(img, 0x00, 0xFF, 0x00); +        color[BLACK]  = gdImageColorAllocate(img, 0x00, 0x00, 0x00); 
-    color[RED]       = gdImageColorAllocate(img, 0xFF, 0x00, 0x00); +        color[BLUE]       = gdImageColorAllocate(img, 0x00, 0x00, 0xFF); 
-    color[WHITE]     = gdImageColorAllocate(img, 0xFF, 0xFF, 0xFF);+        color[GREEN]  = gdImageColorAllocate(img, 0x00, 0xFF, 0x00); 
 +        color[RED]    = gdImageColorAllocate(img, 0xFF, 0x00, 0x00); 
 +        color[WHITE]  = gdImageColorAllocate(img, 0xFF, 0xFF, 0xFF);
  
-    // Paint the background black +        ////////////////////////////////////////////////////////////////// 
-    // +        // 
-    gdImageFilledRectangle(img, 0, 0, wide-1, high-1, color[BLACK]);+        // Paint the background black 
 +        // 
 +        gdImageFilledRectangle(img, 0, 0, wide-1, high-1, color[BLACK]);
  
     /****************************************************************     /****************************************************************
Line 130: Line 148:
      ****************************************************************/      ****************************************************************/
  
-    // Open the file +        ////////////////////////////////////////////////////////////////// 
-    // +        // 
-    if((out = fopen(outfile, "wb")== NULL) +        // Open the file 
-    +        // 
-        fprintf(stderr, "Error opening '%s'\n", outfile); +        out           = fopen(outfile, "wb")
-        exit(1); +        if(out       == NULL) 
-    }+        
 +                fprintf(stderr, "Error opening '%s'\n", outfile); 
 +                exit(1); 
 +        }
  
-    // Send image to file +        ////////////////////////////////////////////////////////////////// 
-    // +        // 
-    gdImagePngEx(img, out-1);+        // Set sane file permissions on image file 
 +        // 
 +        chmod (outfile0644);
  
-    // Close things up +        ////////////////////////////////////////////////////////////////// 
-    // +        // 
-    fclose(out); +        // Send image to file 
-    gdImageDestroy(img);+        // 
 +        gdImagePngEx(img, out, -1);
  
-    return(0);+        ////////////////////////////////////////////////////////////////// 
 +        // 
 +        // Close things up 
 +        // 
 +        fclose(out); 
 +        gdImageDestroy(img); 
 + 
 +        return(0);
 } }
 </code> </code>
 +
 +=====How to view your image=====
 +When you have a functioning project and want to view the images generated, you need to make sure they are deployed into your **public_html/** directory.
 +
 +To view your image in a web browser, simply point it at the following URL:
 +
 +  * <nowiki>https://lab46.g7n.org/~USERNAME/IMAGE.png</nowiki>
 +
 +... where "USERNAME" is your lab46 username (in lowercase), and "IMAGE" is the name of your png file (likely also in lowercase, such as 'cos0').
 +
 +Note that the tilde just prior to your username in the URL is critical and necessary.
 +
 +=====Grabit Integration=====
 +I rigged up a skeleton file in a **grabit** project to kickstart your efforts.
 +
 +To "grab" it:
 +
 +<cli>
 +lab46:~/src/cprog$ grabit cprog cos0
 +make: Entering directory '/var/public/spring2017/cprog/cos0'
 +‘/var/public/spring2017/cprog/cos0/Makefile’ -> ‘/home/USERNAME/src/cprog/cos0/Makefile’
 +‘/var/public/spring2017/cprog/cos0/cos0.c’ -> ‘/home/USERNAME/src/cprog/cos0/cos0.c’
 +make: Leaving directory '/var/public/spring2017/cprog/cos0'
 +lab46:~/src/cprog$ cd cos0
 +lab46:~/src/cprog/pos0$ ls
 +Makefile  cos0.c
 +lab46:~/src/cprog/pos0$ 
 +</cli>
 +
 +And, of course, your basic compile and clean-up operations:
 +
 +  * **make**: compile everything
 +  * **make debug**: compile everything with debug support
 +  * **make clean**: remove all binaries
 +
 +Just another "nice thing" we deserve.
 +
 +NOTE: You do NOT want to do this on a populated **cos0** project directory-- it will overwrite files. Only do this on an empty directory.
  
 =====Submission===== =====Submission=====
Line 177: Line 246:
  
 You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches. You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.
 +
 +What I'll be looking for:
 +
 +<code>
 +78:cos0:final tally of results (78/78)
 +*:cos0:submit project [4/4]
 +*:cos0:level of adequate modification in cos0.c [4/4]
 +*:cos0:level of adequate commenting in cos0.c [4/4]
 +*:cos0:level of adequate indentation in cos0.c [4/4]
 +*:cos0:cos0.c generates circle via algorithm [8/8]
 +*:cos0:no negative compiler messages [4/4]
 +*:cos0:circle of squares is achieved [8/8]
 +*:cos0:deployed to public_html [4/4]
 +*:cos0:checks for and implements error conditions [6/6]
 +*:cos0:image width and height cannot be the same [4/4]
 +*:cos0:cos0.c commit and pushed to lab46 repo [4/4]
 +*:cos0:makes use of gd image library [4/4]
 +*:cos0:reads image details in via arguments [4/4]
 +*:cos0:can adjust output file name with optional last argument [4/4]
 +*:cos0:image border meets specifications [4/4]
 +*:cos0:image background meets specifications [4/4]
 +*:cos0:circle color meets specifications [4/4]
 +*:cos0:circle size meets specifications [4/4]
 +</code>
haas/spring2017/cprog/projects/cos0.1489271502.txt.gz · Last modified: 2017/03/11 22:31 by wedge