shape1

shape1.c  [download]

/* 
 * First example: shapes
 * 1. Bad old way, in plain C
 *
 * RunStuff:: gcc THISFILE
 *
 * Picture looks roughly like this:
 *
 *   *************************          *****
 *   *                       *          *   *
 *   *                       *          *   *
 *   *                       *          *   *
 *   *                       *          *   *
 *   *        r1             *          *r2 *
 *   *                       *          *   *
 *   *                       *          *   *
 *   *                       *          *   *
 *   *************************          *   *
 *                                      *   *
 *                                      *   *
 *            *  *                      *   *
 *          *      *                    *   *
 *         *        *                   *   *
 *         *   c1   *                   *   *
 *          *      *                    *   *
 *            *  *                      *   *
 *                                      *****
 */

#include <stdio.h>

/*
 * Data definitions
 */
int r1x, r1y, r1wid, r1ht;	//*1 Data definitions
int r2x, r2y, r2wid, r2ht;	//*1
int c1x, c1y, c1rad;	//*1

/*
 * Procedure definitions
 */
void draw_rect (int x, int y, int wid, int ht) {	//*2 Procedure definitions
	printf ("Drawing rectangle at %d, %d, %d, %d\n", x, y, wid, ht);	//*2
}

void draw_circle (int x, int y, int rad) {	//*2
	printf ("Drawing circle at %d, %d, radius = %d\n", x, y, rad);
}

float area_rect (int x, int y, int wid, int ht) {	//*2
	return (float) (wid * ht);
}

float area_circle (int x, int y, int rad) {	//*2
	return (float) (rad * rad * 3.14159);
}

/*
 * Main program
 */
int main () {
	/*
	 * Set the locations of the 3 objects
	 */
	r1x = 10; r1y = 10;	//*3 Set the data
	r1wid = 50; r1ht = 50;	//*3

	r2x = 80; r2y = 10;	//*3
	r2wid = 10; r2ht = 100;	//*3

	c1x = 35; c1y = 90;	//*3
	c1rad = 10;	//*3

	/*
	 * Draw them
	 */
	draw_rect (r1x, r1y, r1wid, r1ht);	//*4 Call the procedures
	draw_rect (r2x, r2y, r2wid, r2ht);	//*4
	draw_circle (c1x, c1y, c1rad);	//*4

	/*
	 * Compute and print total area
	 */
	printf ("Total area = %f\n",
		area_rect (r1x, r1y, r1wid, r1ht)	//*4
		+ area_rect (r2x, r2y, r2wid, r2ht)	//*4
		+ area_circle (c1x, c1y, c1rad));	//*4

	return 0;
}