Saturday, December 19, 2015

Draw Square Wave using C Program

Program #33

/*
//////////////////////////////////////////
// **** DRAW SQUARE WAVE ****** //
//////////////////////////////////////////
This Program will Indefinitely Draw Square Wave in Multi-Colors
Also Additional information will keeps on show the Current Drawing Position
*/
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"

/* Defined Constants */
#define WAVE_SPEED   100
#define DEFAULT_ROW   12
#define DEFAULT_COLUMN 1

/* Global Variable */
int column=5, row=12;

/* Global Function Prototypes */
void goStraight();
void goDown();
void goUp();
void drawCurrentPosition();
void drawSquareWave();
void drawUpperPortion();
void drawLowerPortion();
void setColor();

int main(){

  /* Local Function Prototypes */
  int checkColumn();

  /* Clear Screen */
  clrscr();

  /* Set Row and Column values */
  row    = DEFAULT_ROW;
  column = DEFAULT_COLUMN;

  /* Position the Cursor */
  gotoxy(column, row);

  /* Initiate Drawing Square Wave */
  drawSquareWave();
  return 1;
}

/* Initiate Indefinite Drawing */
void drawSquareWave(){
 for(; !kbhit(); ){
    goStraight();
    drawUpperPortion();
    goStraight();
    drawLowerPortion();
  }
}

/* Print Current Position */
void printCurrentPosition(int position){
 /* 1. Straight */
 if (position == 1){
  gotoxy(35, 20);
  printf("Draw Straight! ", column, row);
 }
 /* 2. Up */
 else if (position == 2){
  gotoxy(35, 20);
  printf("Draw Up!       ", column, row);
 }
 /* 3. Down */
 else if (position == 3) {
  gotoxy(35, 20);
  printf("Draw Down!     ", column, row);
 }
 /* 4. End of the Wave */
 else if (position == 4){
   gotoxy(30, 20);
   printf("Square Wave Completed !!");
 }
}

/* Check Column */
int checkColumn(){
  if ( (column+5) > 80) {
    printCurrentPosition(4);
    /* Clear the Screen and Continue the Indefinite Wave */
    clrscr();
    /* Reset the Square Wave Position once again */
    row    = DEFAULT_ROW;
    column = DEFAULT_COLUMN;
    gotoxy(column, row);
    /* Call Drawing Once again */
    drawSquareWave();
  }
  /* Ok, Valid Column */
  return 1;
}

/* Draw Upper Portion */
void drawUpperPortion(){
  goUp();
  goStraight();
  goDown();
}

/* Draw Lower Portion */
void drawLowerPortion(){
  goDown();
  goStraight();
  goUp();
}

/* Go Straight */
void goStraight(){
  int col, i, waveSpeed;
  /* Set Color */
  setColor();
  col = column;
  waveSpeed = WAVE_SPEED;
  if( checkColumn() ){
    for (i=col; i< (col+4); i++){
      /* increase column */
      column++;
      /* Print Position */
      printCurrentPosition(1);
      /* draw Wave */
      gotoxy(i, row);
      cprintf("*");
      /* Add Delay */
      delay(waveSpeed);
    }
  }
}

/* Go Up */
void goUp(){
  int rows, i, waveSpeed;
  /* Set Color */
  setColor();
  rows = row;
  waveSpeed = WAVE_SPEED;
  if( checkColumn() ){
    for (i=rows; i > (rows-4); i--){
      /* Print Position */
      printCurrentPosition(2);
      /* Draw Wave */
      gotoxy(column, i);
      cprintf("*");
      /* Add Delay */
      delay( waveSpeed );
      /* decrease row */
      row--;
    }
  }
}

/* Go Down */
void goDown(){
  int rows, i, waveSpeed;
  /* Set Color */
  setColor();
  rows = row;
  waveSpeed = WAVE_SPEED;
  if( checkColumn() ){
    for (i=rows; i< (rows+4); i++){
      /* Print Position */
      printCurrentPosition(3);
      /* Draw Wave */
      gotoxy(column, i);
      cprintf("*");
      /* Add Delay */
      delay(waveSpeed);
      /* increase row */
      row++;
    }
  }
}

/* getColor */
void setColor(){
 int color;
 int colorArray[14] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
 color = random(14);
 /* Restrict Zero - BLACK color */
 color = colorArray[color];
 textcolor(color);
}






 

No comments:

Post a Comment

Popular Posts