Showing posts with label Local and Global function in C. Show all posts
Showing posts with label Local and Global function in C. Show all posts

Saturday, December 19, 2015

Draw Triangle Wave using C Program

Program #34


/*
//////////////////////////////////////
// **** DRAW TRIANGLE WAVE ****** //
//////////////////////////////////////
This Program will Indefinitely Draw Triangle (Sample Sin) 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;
}

/* 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);
    }
  }
}

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

/* 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);
}







 

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);
}






 

Saturday, December 12, 2015

Simple Animation - Drawing Border using C Program


Program #21

Description:
To draw Border in the Screen using dots.  All dots are in multi-colors. Once the border drawing completed.  System will prints success message.


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

/* global prototype declaration */
void setTextColor();

int main(){
  int leftPos=1, rightPos=25;
  int botPos=1, topPos=80;

 /* prototype declaration */
  void drawLeftBorder(int);
  void drawRightBorder(int);
  void drawTopBorder(int);
  void drawBottomBorder(int);
  void initialMessage();
  void finalMessage();
 
/* Clear Screen */
  clrscr();

  /* Initial Message */
  initialMessage();

  for(;!kbhit();){

     /* take a random color */
     setTextColor();

     if (botPos == 1 && topPos == 80 && leftPos <= 25 && rightPos >= 1){
       drawLeftBorder(leftPos++);
       drawRightBorder(rightPos--);
     }
     if (leftPos == 25 && rightPos == 1 && topPos >= 1 && botPos <= 80){
       drawBottomBorder(botPos++);
       drawTopBorder(topPos--);
     }

     if(leftPos == 25 && rightPos == 1 && botPos == 80 && topPos == 1){
       finalMessage();
       getch();

       /* End the Program Execution */
       return 0;
     }
  }
  getch();
  return 0;
}

/*  Draw Left Border */
void drawLeftBorder(int leftPos){
 gotoxy(1,leftPos);
 cprintf("*",leftPos);
 delay(100);
}

/*  Draw Right Border */
void drawRightBorder(int rightPos){
 gotoxy(80,rightPos);
 cprintf("*",rightPos);
 delay(100);
}

/*  Draw Top Border */
void drawTopBorder(int topPos){
 gotoxy(topPos,1);
 cprintf("*",topPos);
 delay(100);
}

/*  Draw Bottom Border */
void drawBottomBorder(int bottomPos){
 gotoxy(bottomPos, 25);
 cprintf("*",bottomPos);
 delay(100);
}

void finalMessage(){
  int i;
  gotoxy(30,12);
  setTextColor();
  cprintf("Border Drawing Completed!");
  gotoxy(30,13);
  for(i=1;i<=25;i++){
    printf("_");
    delay(100);
  }
}

void initialMessage(){
  int i;
  gotoxy(30,13);
  setTextColor();
  cprintf("Press any Key to EXIT!");
  gotoxy(30,14);
  for(i=1;i<=22;i++){
    printf("-");
    delay(100);
  }
}

void setTextColor(){
  int a;
  a = random(15);
  if (a == 0) {
    /* Restrict BLACK color */
    setTextColor();
  } else {
    textcolor(a);
  }
}



 

Popular Posts