Showing posts with label Draw a box in C Program. Show all posts
Showing posts with label Draw a box in C Program. Show all posts

Friday, December 11, 2015

Simple Animation to Fill Box color using C Program



Program #18

Description:
Dynamically plotting dots on a specific area in the screen using multi-colors.  Once all positions plotting is done.  Program will come to an end.

#include<conio.h>
#include<stdlib.h>
#include<dos.h>

/* Global Array Declaration */
int Box[50][50];

int main(){
  int counter=0, rowValue=1, colValue=1;
  int boxStatus, colorValue;

  /* function prototype declaration */
  int getRow();
  int getColumn();
  int checkBoxCompletion();
 
/* Clear the Screen */
  clrscr();

 /* Iterate Indefinite loop */
  for(;!kbhit();){
    rowValue = getRow();
    colValue = getColumn();
    /* set Box position using Currrent Row,Column */
    if (Box[rowValue][colValue] != 1){
      Box[rowValue][colValue] = 1;
    } else {

 /* avoid overwritting plotted color
      get a new blank position.
      If you wish, just comment below "continue"
      and see the difference
      */
      continue;
    }

  /* Re-initialize counter as 0 once it reaches INT max value */
    if (counter == 32766) counter = 0;

  /* Increase the Counter */
    counter++;

 /* get random color */
    colorValue = getColor();

   /* set color using randow value */
    textcolor(colorValue);
    /* Move the cursor position */
    gotoxy(colValue, rowValue);

   /* do color print */
    cprintf("*");
   
/* set some delay in Milliseconds
    as much you decrease, that much fast system will
    fill the color + add mild-sound
    */
    sound(200*rowValue);
    delay(10);
   
/* after delay, mute the sound */
    nosound();
   
/* check box completion station after every dot plot */
    boxStatus = checkBoxCompletion();
    if (boxStatus == 1) {
      gotoxy(18, 18);

 /* print the success message using last dot color, Interesting!! */
      cprintf("Box Color Fill: Completed Successfully !!");
      getch();

  /* stop the execution */
      return 1;
    }
   }
   return 0;
}

int getRow(){
  int rowNum;

  /* getting a randow row */
   rowNum = random(25);
  if (rowNum<8 || rowNum>15){

 /* get next random */
    getRow();
  } else {
    return rowNum;
  }

  /* worst case, default return value */
  return 8;
}

int getColumn(){
  int colNum;

 /* getting a randow column */
   colNum = random(80);
   if (colNum<25 || colNum>50){

  /* get Next Column */
      getColumn();
   } else {
      return colNum;
   }
   /* worst case, default return value */
  return 25;
}

int getColor(){
   int color;

 /* getting a randow color */
   color = random(15);
   if (color == 0){

   /* 0 means BLACK color, it will spoil the Spot */
     getColor();
   } else {
     return color;
   }

  /* worst case, default return value */
   return 1;
}

int checkBoxCompletion(){
  int i,j;
  for (i=8;i<=15;i++){
    for (j=25; j<=50; j++){
      if(Box[i][j] != 1){

/* Color fill not yet completed */
 return 0;
      }
    }
  }
 
/* Color fill Finished */
  return 1;
}

 

Sunday, December 6, 2015

Draw a box in C Program

Program #13

Description:
Draw a box dynamically in the screen based on co-ordinates and box size.

#include <stdio.h>
#include <conio.h>
void main(){
 int x,y,boxSize,i,getY,getX;
 clrscr();

 printf(" Enter the co-ordinate to Draw Box (x,y) ;\n");
 printf(" x (column):");
 scanf("%d", &x);
 printf(" y (row):");
 scanf("%d", &y);


/* validate co-ordinates */
 if (x != y) {
   printf("Invalid Co-ordinates!!");
   getch();
   /* Stop the Execution */
   abort();
 }
 printf(" Enter size of the Box:\n");
 scanf("%d",&boxSize);

/* Validate possibility of Drawing Box */
 if ((x+boxSize*2) > 80 ){
   printf("Invalid X Co-ordinate!!");
   getch();
   /* Stop the Execution */
   abort();
 } else if ((y+boxSize) > 25){
   printf("Invalid Y Co-ordinate!!");
   getch();
   /* Stop the Execution */
   abort();
 }


/* Move the cursor (x,y) and Draw top line */
 gotoxy(x, y);
 for(i=1; i<=boxSize; i++){
    printf(" *");
 }


 /* Move the cursor (x,y) and Draw left line */
 getY = y+1;
 gotoxy(x,y);
 for(i=1; i<boxSize-1; i++){
    gotoxy(y, getY);
    printf("*");

    /* To move cursor to next row */
    getY++;
 }


 /* Move the cursor (x,y) and Draw bottom line */
 gotoxy(x, y+boxSize-1);
 for(i=1; i<=boxSize; i++){
    printf(" *");
 }


/* Move the cursor (x,y) and Draw right line */
 getY = y+1;
 getX = x+boxSize*2;
 for(i=1; i<boxSize-1; i++){
    gotoxy(getX, getY);
    printf("*");
    /* To move cursor to next row */
    getY++;
 }
 getch();
}


 

Popular Posts