User Tools

Site Tools


haas:spring2015:common:gdpoly

GD Polygons

Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gd.h>
 
// color values
//
#define BLACK       0
#define GRAY        1
#define VIOLET      2
#define INDIGO      3
#define BLUE        4
#define GREEN       5
#define YELLOW      6
#define ORANGE      7
#define RED         8
#define WHITE       9
#define DARKGREEN   10
 
int main(int argc, char **argv)
{
    FILE                *out;       // output file pointer
    char                *outfile;   // output file name
    gdImagePtr          img;        // GD Image Construct
    gdPoint             points[3];  // Points for a Polygon
    unsigned int        color[11];  // color array
    unsigned short int  wide, high, // useful image and
                        x, y;       // coordinate variables
 
    if (argc == 2)
    {
        outfile = *(argv+1);
    }
    else
    {
        outfile = (char *) malloc (sizeof(char) * 10);
        strcpy(outfile, "image.png");
    }
 
    fprintf(stdout, "Using '%s' as output filename\n", outfile);
 
    wide = 800;
    high = 600;
 
    img = gdImageCreateTrueColor(wide, high);
 
    // My GD color definitions
    //
    color[BLACK]             = gdImageColorAllocate(img,   0,   0,   0);
    color[BLUE]              = gdImageColorAllocate(img,   0,   0, 255);
    color[GREEN]             = gdImageColorAllocate(img,   0, 255,   0);
    color[DARKGREEN]         = gdImageColorAllocate(img,  51, 107,   0);
    color[RED]               = gdImageColorAllocate(img, 255,   0,   0);
    color[GRAY]              = gdImageColorAllocate(img, 204, 204, 204);
    color[WHITE]             = gdImageColorAllocate(img, 255, 255, 255);
 
    /* Draw a triangle. */
    points[0x00].x = (wide / 2);
    points[0x00].y = 0;
    points[0x01].x = wide;
    points[0x01].y = high;
    points[0x02].x = 0;
    points[0x02].y = high;
 
    /* Paint it in white */
    gdImageFilledPolygon(img, points, 3, color[GREEN]);
 
    /* Outline it in red; must be done second */
    gdImagePolygon(img, points, 3, color[RED]);
 
    // Output the data
    //
    out = fopen(outfile, "wb");
    gdImagePngEx(img, out, -1);
 
    // Close things up
    //
    fclose(out);
    gdImageDestroy(img);
 
    return(0);
}

More Polygons

If we made another gdPoint array (points2), what would that following code produce a likeness of?

    points2[0x00].x = (wide / 2); 
    points2[0x00].y = (high / 2) - 100;
    points2[0x01].x = (wide / 2) + 200;
    points2[0x01].y = (high / 2) + 100;
    points2[0x02].x = (wide / 2) - 200;
    points2[0x02].y = (high / 2) + 0;
    points2[0x03].x = (wide / 2) + 200;
    points2[0x03].y = (high / 2) + 0;
    points2[0x04].x = (wide / 2) - 200;
    points2[0x04].y = (high / 2) + 100;
    points2[0x05].x = (wide / 2) + 0;
    points2[0x05].y = (high / 2) - 100;

Combining Shapes

Write C programs that creates likenesses of the following (don't worry about shading- just see if you can use your skills to recreate these images, and to make additional images of your liking):

3734.jpg

red_white_blue_star.jpg

triforce.jpg

haas/spring2015/common/gdpoly.txt · Last modified: 2013/03/27 12:20 by 127.0.0.1