Basic Concept
This article deals with a dynamic C drawing program that adapts according to user input to produce a triangle, a rectangle, a square or a circle. It uses building block specified by the user to construct these shapes. Concepts used here include prototypes, functions, mathematical logic control, do while loops, for loops, and nested for loops.
First part of the program that draws Rectangles, and circles uses for loops to print building characters in a grid of order defined by the user.
The second part that prints triangles has two parts; first part prints the triangle, and second part that controls spacing of the columns to achieve symmetry for a upright triangle.
Third and the most important part of the program draws a circle, the logic of which is described in a detailed manner Here. (Drawing a circle in C without using graphics library)
The main function of the program includes deciding the function according to user input and providing construction character argument for our own defined drawing functions.
#include <stdio.h> #include <stdlib.h> void drCircle(char con); void drRect(char con); void drTriangle(char con); main() { char sh,con; int ex; printf(" \n\n \t *** PROGRAM THAT DRAWS SHAPES *** \n\n" ); // User Instructions Go Here printf("\t S : Square \n\t R : Rectangle \n\t T : Triangle \n\t C : Circle \n\n"); start1: // Choice of Building Blocks printf("\tChoose Construction Character? [e.g. * . + etc] : "); fflush(stdin); scanf("%c",&con); do{ printf("\tPlease choose your desired shape[S/R/T/C] : "); fflush(stdin); scanf("%c",&sh); // This one tests choice of shapes if (sh == 'S' || sh == 'R' || sh == 's' || sh == 'r') drRect(con); else if(sh == 'T' || sh == 't') drTriangle(con); else if (sh == 'C' || sh == 'c') drCircle(con); else{ printf("\tInvalid Parameter! Try Again.\n\n"); ex = -1; } } while (ex == -1); //Error Handler printf("\n\n\n\tDraw Again? [Y/N] : "); // Multiple Instances fflush(stdin); con = getchar(); if (con == 'y' || con == 'Y') goto start1; } void drRect(char con){ //Rectangle & Square Drawing int b, l, lx, bx; printf("\tEnter Dimensions [HEIGHT X WIDTH] : "); scanf("%d x %d",&b,&l); printf("\n\n"); for (bx = 1; bx <= b; bx++) { printf("\n\t\t\t%c",con); for (lx = 1; lx <= l; lx++) printf(" %c",con); } } void drTriangle(char con){ // Triangle Drawing int b,c,bx,lx; printf("\tFor the time being, this program only prints symmetric triangles.\n"); printf("\tEnter height of triangle : "); scanf("%d",&b); printf("\n\n"); for (bx = b; bx>=1; bx--) { printf("\n\t\t\t"); for (c = (b - bx); c < b ; c++) //Spacing for Symmetry printf(" "); printf("%c",con); for (lx = bx; lx < b; lx++) printf(" %c",con); } } void drCircle(char con){ // Cirlce Drawing int r, x, y; printf("\tPlease Enter Radius : "); scanf("%d", &r); for (y = r; y >=- r; y--){ //Quadrantal Coordinates Controller printf("\n\t"); for (x = -r; x <= r; x++){ if (x*x + y*y < r*r) // Limits the grid Using Eq of Circle printf(" %c",con); else printf(" "); } } }
You'll get following output on running the program above.
You might have noted the error controller installed through a do...while loop.
No comments:
Post a Comment