This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
haas:spring2017:cprog:projects:cos0 [2016/03/07 17:30] – external edit 127.0.0.1 | haas: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, | + | 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, |
One piece of that, obviously, is the math. | One piece of that, obviously, is the math. | ||
Line 35: | Line 35: | ||
* uses the gd image library (libgd) | * uses the gd image library (libgd) | ||
* reads the following information from the command-line (lacking this information should result in an error): | * reads the following information from the command-line (lacking this information should result in an error): | ||
- | * *(argv+1): image width (in pixels, ranging from 800-1280) | + | * *(argv+1): image width (in pixels, ranging from 600-1024) |
- | * *(argv+2): image height (in pixels, ranging from 600-1024) | + | * *(argv+2): image height (in pixels, ranging from 800-1280) |
* *(argv+3): circle radius (in pixels, should not exceed half of the smaller of width, height) | * *(argv+3): circle radius (in pixels, should not exceed half of the smaller of width, height) | ||
* *(argv+4): center x of circle (a value of 0 should result in auto-centering of x with respect to width) | * *(argv+4): center x of circle (a value of 0 should result in auto-centering of x with respect to width) | ||
* *(argv+5): center y of circle (a value of 0 should result in auto-centering of y with respect to height) | * *(argv+5): center y of circle (a value of 0 should result in auto-centering of y with respect to height) | ||
* *(argv+6), if present, will be the name of the output image file produced. If it is absent, assume the default name **cos0.png**. | * *(argv+6), if present, will be the name of the output image file produced. If it is absent, assume the default name **cos0.png**. | ||
- | * image width, height is at least 800x600 | + | * image width, height is at least 600x800 |
* if the information isn't supplied, display an error and exit with non-zero status | * if the information isn't supplied, display an error and exit with non-zero status | ||
* image resolution must not be square (i.e. **NOT** 800x800, 1000x1000, etc.)-- if it is, display an error and exit with non-zero status | * image resolution must not be square (i.e. **NOT** 800x800, 1000x1000, etc.)-- if it is, display an error and exit with non-zero status | ||
* image has a black background | * image has a black background | ||
- | * image has an outer border of a color other than black and the circle (unless circle is multiple colors, then the border could be one of the contained colors). | + | * image has an outer border of a color other than background |
* draws a circle out of squares | * draws a circle out of squares | ||
* circle must be at least 1 color (that isn't the same as the background), | * circle must be at least 1 color (that isn't the same as the background), | ||
Line 51: | Line 51: | ||
* multiple circles (concentric? | * multiple circles (concentric? | ||
* spirals | * spirals | ||
- | * squares should be at least 10x10 (pixels) | + | * squares should be at least 12x12 (pixels) |
- | Now, gd has functions for drawing circles. You are **NOT** to use them for this project. As I said, this is a circle of squares. As such, you will be making use of the **gdImageFilledRectangle()** function. | + | Now, gd has functions for drawing circles. You are **NOT** to use them for this project. As I said, this is a circle |
====getting started==== | ====getting started==== | ||
Line 62: | Line 62: | ||
<code c> | <code c> | ||
/* | /* | ||
- | | + | |
- | | + | |
* | * | ||
- | * To compile: | + | * To compile: |
- | * To execute: | + | |
- | | + | * To execute: ./ |
* | * | ||
- | | + | |
+ | | ||
*/ | */ | ||
#include < | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
+ | #include < | ||
#include < | #include < | ||
Line 86: | Line 88: | ||
int main(int argc, char **argv) | int main(int argc, char **argv) | ||
{ | { | ||
- | | + | ////////////////////////////////////////////////////////////////// |
- | FILE | + | // |
- | gdImagePtr | + | // Declare variables |
- | unsigned int color[5]; | + | // |
- | unsigned short int wide, high; | + | |
+ | FILE | ||
+ | gdImagePtr | ||
+ | unsigned int color[5]; | ||
+ | unsigned short int wide, high; // image attributes | ||
- | | + | ////////////////////////////////////////////////////////////////// |
- | { | + | // |
- | outfile = *(argv+1); | + | // Check arguments |
- | fprintf(stdout, | + | // |
- | } | + | |
- | else | + | { |
- | { | + | outfile |
- | outfile = (char *) malloc (sizeof(char) * 64); | + | fprintf(stdout, |
- | | + | } |
- | } | + | else |
+ | { | ||
+ | outfile | ||
+ | | ||
+ | } | ||
- | | + | ////////////////////////////////////////////////////////////////// |
- | // | + | // |
- | wide = 800; | + | |
- | high = 600; | + | // |
+ | wide = 600; | ||
+ | high = 800; | ||
- | | + | ////////////////////////////////////////////////////////////////// |
- | // | + | // |
- | img = gdImageCreate(wide, | + | |
+ | // | ||
+ | img | ||
- | | + | |
- | // | + | // |
- | color[BLACK] | + | // Some GD color definitions |
- | color[BLUE] | + | // |
- | color[GREEN] | + | color[BLACK] |
- | color[RED] | + | color[BLUE] |
- | color[WHITE] | + | color[GREEN] |
+ | color[RED] | ||
+ | color[WHITE] | ||
- | | + | ////////////////////////////////////////////////////////////////// |
- | // | + | // |
- | gdImageFilledRectangle(img, | + | |
+ | // | ||
+ | gdImageFilledRectangle(img, | ||
/ | / | ||
Line 130: | Line 148: | ||
| | ||
- | | + | ////////////////////////////////////////////////////////////////// |
- | // | + | // |
- | if((out = fopen(outfile, | + | |
- | { | + | // |
- | fprintf(stderr, | + | out |
- | exit(1); | + | if(out |
- | } | + | { |
+ | fprintf(stderr, | ||
+ | exit(1); | ||
+ | } | ||
- | | + | |
- | // | + | // |
- | | + | // Set sane file permissions on image file |
+ | // | ||
+ | | ||
- | | + | //////////////////////////////////////////////////////////////// |
- | // | + | // |
- | | + | // Send image to file |
- | | + | // |
+ | gdImagePngEx(img, out, -1); | ||
- | | + | ////////////////////////////////////////////////////////////////// |
+ | // | ||
+ | // Close things up | ||
+ | // | ||
+ | fclose(out); | ||
+ | gdImageDestroy(img); | ||
+ | |||
+ | | ||
} | } | ||
</ | </ | ||
+ | |||
+ | =====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/ | ||
+ | |||
+ | To view your image in a web browser, simply point it at the following URL: | ||
+ | |||
+ | * < | ||
+ | |||
+ | ... where " | ||
+ | |||
+ | 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 " | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | make: Entering directory '/ | ||
+ | ‘/ | ||
+ | ‘/ | ||
+ | make: Leaving directory '/ | ||
+ | lab46: | ||
+ | lab46: | ||
+ | Makefile | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | 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: | ||
+ | |||
+ | < | ||
+ | 78: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *:cos0:no negative compiler messages [4/4] | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *:cos0:can adjust output file name with optional last argument [4/4] | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | *: | ||
+ | </ |