This is a CG lab program in C using graphics.h library to animate a caterpillar eating a leaf. As it eats, the caterpillar (or worm) should grow proportionally in size. The leaf should also be reduced in size. The program is given below:
The ouput is:
Search This Blog
DDA Line Drawing Program in C : Computer Graphics Lab Program
Digital Differential Analyzer Algorithm or DDA algorithm is the simplest line drawing algorithm. The algorithm is taught in computer graphics subjects. The algorithm is implemented in CG labs (computer graphics labs). Borland Graphics Interface (BGI) in C is commonly used for implementing computer graphics algorithms in computer graphics labs. Here i am adding c program to draw any line with any slope using the DDA line drawing algorithm. The DDA line drawing program is as follows:
In this program, the dda line drawing algorithm is implemented as a function named dda which takes the co-ordinates of the two end points as parameters. In the main program, i have written a code which tests the DDA algorithm by drawing lines of almost all slopes.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void dda(int x1,int y1,int x2,int y2)
{
float xincr,yincr,x,y,dx,dy,s,i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
s=abs(dx);
else
s=abs(dy);
xincr=dx/s;
yincr=dy/s;
x=x1;
y=y1;
for(i=0;i<s;i++)
{
putpixel(x,y,2);
x+=xincr;
y+=yincr;
}
}
void main()
{
int gd=DETECT,gm,theta=0;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI\\");
while(!kbhit())
{
theta=(theta+1)%361;
dda(300,150,300+50*sin(3.14f*theta/180),150+50*cos(3.14f*theta/180));
delay(50);
cleardevice();} }
In this program, the dda line drawing algorithm is implemented as a function named dda which takes the co-ordinates of the two end points as parameters. In the main program, i have written a code which tests the DDA algorithm by drawing lines of almost all slopes.
Blooming Flower and Bee Animation C Program - Computer Graphics Lab Experiment
One of my graphics lab questions was to write a c program to animate a blooming flower and a bee (or a beetle) that comes to the flower, sit on it and fly away. The program uses graphics.h header file.
Output:
Program
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int i,xmax,ymax;
int startx,starty;
int stopped=0;
void drawFlower(int i)
{
setcolor(GREEN);
line(xmax/2,ymax/2,xmax/2,ymax/2+100);
setcolor(BROWN);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(xmax/2,ymax/2,10+1.8*i,5+.6*i);
fillellipse(xmax/2,ymax/2,5+.6*i,10+1.8*i);
setcolor(BROWN);
setfillstyle(SOLID_FILL,RED);
fillellipse(xmax/2,ymax/2,5+.6*i,5+.6*i);
}
void drawBee(int i)
{
startx+=2;
starty+=7*sin(i);
setcolor(BROWN);
setfillstyle(SOLID_FILL,BLUE);
fillellipse(startx,starty,12,5);
setcolor(BLUE);
fillellipse(startx,starty,5,10*(i%2));
fillellipse(startx+8,starty-3,1,1);
fillellipse(startx+8,starty+3,1,1);
line(startx+9,starty-2,startx+11,starty-5);
line(startx+9,starty+2,startx+11,starty+5);
}
void main()
{
int max,may,i,t;
int gm=DETECT,gd;
initgraph(&gm,&gd," ");
xmax=getmaxx();
ymax=getmaxy();
startx=xmax/2-100;
starty=ymax/2;
setbkcolor(WHITE);
getch();
for(i=0;i<20;i++)
{
delay(120);
cleardevice();
drawFlower(i);
}
t=i;
for(i=0;startx+i<xmax-60;i++)
{
delay(90);
cleardevice();
drawFlower(t);
drawBee(i);
if(stopped==0&&abs(startx-xmax/2)<10&&abs(starty-ymax/2)<15)
{
delay(2500);
stopped=1;
}
}
getch();
}
C Program to get a Substring from a String from Given Index
In this post we will see a c program to get a substring from a given main string from specified index to a specified index. For example, consider given a string mainstring which is equal to "cSourceCodes.blogsppot.com". Calling the function subString(mainstring,7,11) will return "Codes".
Arguments (parameters):
First - the original string
Second - The starting index (position) of substring (counting starts from zero)
Third - The end index of substring
Both the start index and end index are inclusive.
Arguments (parameters):
First - the original string
Second - The starting index (position) of substring (counting starts from zero)
Third - The end index of substring
Both the start index and end index are inclusive.
#include<stdio.h>
#include<string.h>
char * subString(char *MainString,int fromIndex,int toIndex)
{
int Mainlength,j;
char *subStr;
Mainlength=strlen(MainString);
if(fromIndex<0||fromIndex>=Mainlength||toIndex<fromIndex||toIndex>=Mainlength)
{
printf("\nError in args: subString fn");
return(NULL);
}
subStr=(char *)malloc(1000*sizeof(char));
for(j=0;j<=toIndex-fromIndex;j++)
*(subStr+j)=*(MainString+fromIndex+j);
*(subStr+j)='\0';
return(subStr);
}
void main()
{
char mainstring[]="cSourceCodes.blogsppot.com";
printf("%s",subString(mainstring,7,11));
getch();
}
Computer Graphics Program in C to Simulate Motion of Simple Pendulum
In this post, we see a simple C program to simulate motion of a simple pendulum using graphics.h. Actually, this was a problem given in my CG lab (Computer Graphics lab). The program uses graphics.h header file. If you are using linux gcc, you have to include libgraph library. The header file graphics.h is already included in turboc or turboc++ compilers.
If you want corresponding java program, get it here: Java Program to Simulate Motion of Simple Pendulum.
The output of the program is as follows:
If you want corresponding java program, get it here: Java Program to Simulate Motion of Simple Pendulum.
The output of the program is as follows:
Program:
#include<graphics.h>
#include<stdio.h>
#include<math.h>
#define PI 3.14
int gd=DETECT,gm;
int pivotx,pivoty;
double thetamax,theta;;
double len=260;
int x,y,ymax,xmax;
int bobradius=30;
int xsign=-1,ysign=1;
double omega;
void paint()
{
cleardevice();
setcolor(RED);
fillellipse(pivotx,pivoty,8,8);
line(pivotx,pivoty, x,y);
fillellipse(x,y,bobradius,bobradius);
}
void main()
{
double decr;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI\\");
thetamax=60*PI/180;
pivotx=getmaxx()/2;
pivoty=30;
ymax=(int) (pivoty+len*cos(thetamax));
xmax=(int) (pivotx+len*sin(thetamax));
x=xmax;
y=ymax;
theta=thetamax;
while(1)
{
if(x>=pivotx+abs(len*sin(thetamax)))
{
xsign=-1;
ysign*=-1;
x=xmax-1;
delay(40);
}
else if(x<=pivotx-abs(len*sin(thetamax)))
{
ysign*=-1;
xsign=1;
x=(int) (pivotx-abs(len*sin(thetamax))+2);
delay(40);
}
else if(y>=pivoty+len)
{
ysign*=-1;
}
omega=y/60*PI/180;
decr=xsign*omega;
theta=theta+decr;
x=(int) (pivotx+len*sin(theta));
y=(int) (pivoty+len*cos(theta));
paint();
delay(40);
}
}
Body Mass Index Calculator Using C Program - C Program to Calculate BMI
Body Mass Index Calculation is based on your weight and height. Body Mass Index (BMI) = weight/(height*height) where weight is in kilograms and height in meters. The following c program takes weight and height as input and displays BMI and grades into underweight,Normal, Overweight or obesity.
C Program to Display Armstrong Numbers
C Program to Check Armstrong Number
C Program to Reverse a Number
C Program for Quick Sort
C Program to Search an element in array (Linear Search)
Leap Year C Program
C Program to Find Sum of Digits of a Number
C Program to Check Perfect Number
C Program to Find Transpose of Matrix
C Program to Understand Bitwise Operators
#include<stdio.h>;
void main()
{
float w,h,bmi;
printf("Enter weight in kgs and height in meters");
scanf("%f%f",&w,&h);
bmi=w/(h*h);
printf("bmi: %f",bmi);
bmi<18.5?printf("Underweight"):(bmi<25)?printf("Normal weight"):(bmi<30)?printf("Overweight"):printf("Obesity");
}
Calculate your BMI here
| Weight (in kg): | |
| Height (in meters): | |
Related Posts:
C Program to Display Prime Numbers Less than Given NumberC Program to Display Armstrong Numbers
C Program to Check Armstrong Number
C Program to Reverse a Number
C Program for Quick Sort
C Program to Search an element in array (Linear Search)
Leap Year C Program
C Program to Find Sum of Digits of a Number
C Program to Check Perfect Number
C Program to Find Transpose of Matrix
C Program to Understand Bitwise Operators
Four Different Methods or Tricks to Swap Two Numbers Without Using a Third Variable
In this post, we will see four different methods in c to swap two variables without using a third variable. This type of questions are often asked in technical interviews. Here we will see four different C programs to swap two numbers without a third temporary variable. Using a third variable is not an overhead. But still this kinda questions are asked in technical interviews for programming jobs to test your knowledge in the language.
In the third c code, we swap the numbers using bitwise XOR operation. The bitwise operator ^
is used in the program. The binary representation of the numbers, the bitwise xor operation on them and the result are shown in the c program as comments. The program is as follows:
In the fourth method to swap two numbers without a temporary variable, we swap the numbers using a combination of addition, subtraction and bitwise NOT (inversion) operation. The bitwise operator ~
is used in the program. The binary representation of the numbers, the bitwise NOT operation on them and the result are shown in the c program as comments. The program is as follows:
Trick 1
In the first c program to swap two numbers without a third variable, we swap the values of the two variables in 3 steps which are simple addition and subtractions.
#include<stdio.h>
void main(){
int a=5,b=10;
a=b+a; //now a =15
b=a-b; //now b= 5
a=a-b; // now a=10 (done)
printf("a= %d\nb= %d",a,b);
}
Trick 2
In the second c program to swap two numbers without a third variable, we swap them in a single statement. The statement contains an assignment, addition and subtraction.
#include<stdio.h>
void main(){
int a=10,b=30;
a=a+b-(b=a);
//10+30-(10) a becomes 30
// b is assigned with 10 in the same line of code
printf("a= %d\nb= %d",a,b);
}
Trick 3
is used in the program. The binary representation of the numbers, the bitwise xor operation on them and the result are shown in the c program as comments. The program is as follows:
#include<stdio.h>
void main(){
int a=8,b=12;
/* a: 8 : 01000
b:12 : 01100
*/
a=a^b;
/* 01000 ^
01100
a= 00100 (4)
*/
b=a^b;
/* 00100 ^
01100
b= 01000 (8)
*/
a=b^a;
/* 01000 ^
00100
a= 01100 (12)
*/
printf("a= %d\nb= %d",a,b);
}
Trick 4
is used in the program. The binary representation of the numbers, the bitwise NOT operation on them and the result are shown in the c program as comments. The program is as follows:
#include<stdio.h>
void main(){
int a=14,b=4;
/* a: 14 : 1110
b: 4 : 0100
~a: : 0001
~b: : 1011
*/
a=b-~a-1;
/* b: 0100 -
~a: 0001
0011 -
1: 0001
0010
now a= 0010
*/
b=a+~b+1;
/* a: 0010 +
~b: 1011
1: 0001
now b: 1110 (14)
~b: 0001
*/
a=a+~b+1;
/* a: 0010 +
~b: 0001
1: 0001
now a: 0100 (4)
*/
printf("a= %d\nb= %d",a,b);
}
Subscribe to:
Posts (Atom)