computer graphics – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 10 Mar 2024 17:04:43 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg computer graphics – Programmerbay https://programmerbay.com 32 32 C Program to draw Bezier Curve using 4 control points https://programmerbay.com/c-program-to-draw-bezier-curve-using-4-control-points/ https://programmerbay.com/c-program-to-draw-bezier-curve-using-4-control-points/#respond Sat, 24 Feb 2024 16:41:31 +0000 https://www.programmerbay.com/?p=2143 Bezier Curve is one of the Curve representation which uses control points to draw a curve. It is always determined on the number of control points that require to draw it. It follows Bernstein polynomial as the basis function.

Must Read [  What is Bezier Curve ?  ]

C Program to draw a Bezier curve

Program:

#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int x[4],y[4],i;
double put_x,put_y,t;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** Bezier Curve ***********");
printf("\n Please enter x and y coordinates ");
for(i=0;i<4;i++)                 
{
scanf("%d%d",&x[i],&y[i]);
putpixel(x[i],y[i],3);                // Control Points
}

for(t=0.0;t<=1.0;t=t+0.001)             // t always lies between 0 and 1
{
put_x = pow(1-t,3)*x[0] + 3*t*pow(1-t,2)*x[1] + 3*t*t*(1-t)*x[2] + pow(t,3)*x[3]; // Formula to draw curve
put_y =  pow(1-t,3)*y[0] + 3*t*pow(1-t,2)*y[1] + 3*t*t*(1-t)*y[2] + pow(t,3)*y[3];
putpixel(put_x,put_y, WHITE);            // putting pixel 
}
getch();
closegraph();
}

 

Output:

bezier curve

 

]]>
https://programmerbay.com/c-program-to-draw-bezier-curve-using-4-control-points/feed/ 0
C Program to Wish Happy New Year https://programmerbay.com/c-program-to-wish-happy-new-year/ https://programmerbay.com/c-program-to-wish-happy-new-year/#respond Thu, 15 Feb 2024 08:35:33 +0000 https://www.programmerbay.com/?p=2284 The program uses C graphics header file capabilities to create the illusion of fireworks and show a Happy New Year message. We hope you achieve all your goals this year.

C Program to wish Happy New Year 2025

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void shoot();
void shootagain();
void area();
void explode(int,int,int);
void main()
{
int gm,gd=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
shoot();
getch();
closegraph();
}
void shoot()
{
int i=0;
int x=0,y=480,x1=15,y1=460;
while(i<350)
{
area();
line(x+i,y-i,x1+i,y1-i);
delay(50);
i=i+10;
cleardevice();
}
explode(x+350,y-350,5);
shootagain();
}


void shootagain()
{
int i=0;
int x=600,y=480,x1=585,y1=460;
while(i<250)
{
setcolor(15);
area();
line(x-i,y-i,x1-i,y1-i);
delay(30);
i=i+10;
cleardevice();
}
explode(x-300,y-300,5);
}

void explode(int x,int y,int r)
{
int k,j,interval=0;
for(k=0;k<2;k++)
{
interval=interval+50;
for(j=0;j<interval;j++)
{
area();
setcolor(BLACK);
setcolor(rand()/15);
// horizontal left and right
circle(x+j,y,r+k);
circle(x-j,y,r+k);
//vertical up and down
circle(x,y+j,r+k);
circle(x,y-j,r+k);
//slighten down
circle(x+j,y+j,r+k);
circle(x-j,y-j,r+k);
//slighten up
circle(x-j,y+j,r+k);
circle(x+j,y-j,r+k);
delay(30);
cleardevice();
}
}
}
void area()
{
//Area
setcolor(15);
line(0,350,600,350);
rectangle(0,350,100,150);
rectangle(40,350,60,300);
rectangle(10,170,30,200);
rectangle(70,170,90,200);
rectangle(10,230,30,260);
rectangle(70,230,90,260);

rectangle(100,350,180,250);
line(100,250,140,180);
line(180,250,140,180);
rectangle(110,280,130,300);
rectangle(150,280,170,300);
rectangle(130,350,160,330);

rectangle(180,350,350,300);
rectangle(190,310,220,330);
rectangle(340,310,310,330);

rectangle(370,350,440,150);
rectangle(385,350,405,300);
rectangle(380,170,400,200);
rectangle(410,170,430,200);
rectangle(380,230,400,260);
rectangle(410,230,430,260);
settextstyle(BOLD_FONT,HORIZ_DIR,1);
outtextxy(110,50,"HAPPY NEW YEAR");
}

 

Output:

happynewyear

]]>
https://programmerbay.com/c-program-to-wish-happy-new-year/feed/ 0
C Program for 2D Translation https://programmerbay.com/c-program-for-2d-translation/ https://programmerbay.com/c-program-for-2d-translation/#respond Fri, 19 Aug 2022 09:55:19 +0000 https://www.programmerbay.com/?p=1837 2D Translation can be defined as a way of shifting an object from one point to another in a straight path. Here is the C program for demonstrating 2D translation

Must Read: What is 2D Translation?

Program to show the translation of a line

Program:

#include<conio.h>
#include<graphics.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
// declaring two array
// Translation vector already initialized
int l[2][2],v[2]={10,15},i=0,j;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter the initial and final coordinates of a line ");

// Getting input from user, having 2D array where 1st row represents initial point
// And Second row represents final coordinate
while(i<2)
{
printf("x%d and y%d = ",i,i);
j=0;
scanf("%d",&l[i][j]);
scanf("%d",&l[i][j+1]);
i++;
}
// Line before translation
line(l[0][0],l[0][1],l[1][0],l[1][1]);
setcolor(BLUE);
// Line after translation
line(l[0][0]+v[0],l[0][1]+v[1],l[1][0]+v[0],l[1][1]+v[1]); // Adding Translation vector in it to change the position
getch();
closegraph();
}

 

Output:

translation using array

 

 

Related post with Example:

2D Translation: Positioning of an Object

]]>
https://programmerbay.com/c-program-for-2d-translation/feed/ 0
C Program to Draw Chess Board https://programmerbay.com/program-to-draw-a-chess-board-in-c/ https://programmerbay.com/program-to-draw-a-chess-board-in-c/#respond Fri, 12 Aug 2022 07:47:11 +0000 https://www.programmerbay.com/?p=2108 C supports a special header file named graphics.h that provides various functions through which one can draw different shapes such as line, circle, triangle, and more. And with the combination of these functions, we can create objects like a hut, joker, cap, and more.

The program draws a Chess board with the help of graphic libraries. It uses rectangle(), setcolor(), floodfill and setfillstyle() functions to draw this object.

C program to Draw Chess Board

 Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
int gr=DETECT,gm;
int row,col,x=50,y=50,flag=0;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\t*********** CHESS BOARD **************\n");
for(row=0;row<8;row++)
{

for(col=1;col<=8;col++){
if(flag==0){
setcolor(YELLOW);
setfillstyle(SOLID_FILL,BLACK);
rectangle(x,y,x+50,y+50);
floodfill(x+1,y+1,YELLOW);
flag=1;
}
else
{
setcolor(YELLOW);
setfillstyle(SOLID_FILL,WHITE);
rectangle(x,y,x+50,y+50);
floodfill(x+1,y+1,YELLOW);
flag=0;
}
x=x+50;
}
if(flag==0)
flag=1;
else
flag=0;
delay(100);
x=50;
y=50+y;
}
getch();
closegraph();
}

 

Output:

chessboard

]]>
https://programmerbay.com/program-to-draw-a-chess-board-in-c/feed/ 0
C Program to Show a Man Walking in Rain https://programmerbay.com/program-for-man-walking-in-rain/ https://programmerbay.com/program-for-man-walking-in-rain/#respond Thu, 11 Aug 2022 17:21:02 +0000 https://www.programmerbay.com/?p=2101 Using translation transformation,  an illusion of moving object can be achieved.  Here’s the simple code to show a man is walking in the rain.

C program for a man walking in the rain

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main(){
int gr=DETECT,gm;
int i,x,y,j;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");

// man
for(j=1;j<600;j=j+5)
{
line(0,400,800,400);
circle(30+j,280,20); //head
line(30+j,300,30+j,350); //body
line(30+j,330,70+j,330); //hand
if(j%2==0){
line(30+j,350,25+j,400); //left leg
line(30+j,350,10+j,400); // right
}
else{
line(30+j,350,35+j,400); //transition
delay(20);
}
//umbrela
line(70+j,250,70+j,330);
pieslice(70+j,250,180,0,80);
// rain
for(i=0;i<300;i++)
{
x=random(800);
y=random(800);
outtextxy(x,y,"/");
}
delay(170);
cleardevice();
}
getch();
closegraph();
}

 

Output

]]>
https://programmerbay.com/program-for-man-walking-in-rain/feed/ 0
C Program to Show a Kite is Flying https://programmerbay.com/c-program-to-show-a-kite-is-flying/ https://programmerbay.com/c-program-to-show-a-kite-is-flying/#respond Wed, 03 Aug 2022 08:46:20 +0000 https://www.programmerbay.com/?p=1969 The program uses translation transformation to implement flying kite in C. In computer graphics, translation allows reposition of an object. We can create an illusion of flying kite by frequently changing the object’s position.

C Program to show a kite is flying

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gm,gd=DETECT;
int i= 0,j=0,rnd_x=0,rnd_y,stop_me=0;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
srand(time());
while(stop_me<=1000)
{

if(i>=180 &&j>=100 ) // controlling kite, so that it wouldn't disappear from screen
{
rnd_x=rand()%4 -3;
rnd_y=rand()%5 -4;
}
else{
rnd_x=rand()%3;
rnd_y=rand()%3;
}
line(200+i,200-j,250+i,250-j);
line(200+i,200-j,150+i,250-j);
line(150+i,250-j,200+i,350-j);
line(200+i,350-j,250+i,250-j);
line(200+i,200-j,200+i,350-j);
arc(200+i,275-j,25,155,50);
line(0,500,200+i,225-j);
i=i+rnd_x;
j=j+rnd_y;
stop_me=5+stop_me;
delay(100);
clearviewport(); // clearing image which would make illusion of flying kite

}
getch();
closegraph();

}

Output:

]]>
https://programmerbay.com/c-program-to-show-a-kite-is-flying/feed/ 0
C Program to Move a Car https://programmerbay.com/program-to-move-a-car-in-c/ https://programmerbay.com/program-to-move-a-car-in-c/#respond Wed, 03 Aug 2022 07:54:18 +0000 https://www.programmerbay.com/?p=1961 In this article, we’ll be implementing moving car program in computer graphics. Using translation transformation, we can draw and move an object to another coordinate. The below code is a moving car program written in C.

C Program to move a car in computer graphics

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gm,gd=DETECT;
int i= 0;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
while(i<=800)
{
line(0,300,800,300); // Path
//Body of the car
line(50+i,220,100+i,220);
line(50+i,220,30+i,250);
line(100+i,220,120+i,250);
rectangle(0+i,250,160+i,270);
// Tyres of the car
circle(30+i,285,12);
circle(130+i,285,12);
if(i>=800)
{
break;
}
i=i+2;
clearviewport(); // clearing image which would make illusion of moving car
}
getch();
closegraph();

}

Explanation: 

In the above code, we have drawn a car and a path on which it would be running. We used, a line method to create a path, 1 rectangle and 3 line methods to build the upper body of a car and two circle method for its tyres.

Using translation transformation, we were able to make an illusion of a moving car. And lastly, clearviewport() method used to clear a previously generated view.

Output:

movingcar

]]>
https://programmerbay.com/program-to-move-a-car-in-c/feed/ 0
C Program to Draw a Circle using Midpoint Circle Drawing Algorithm https://programmerbay.com/program-to-draw-a-circle-using-midpoint-circle-drawing-algorithm/ https://programmerbay.com/program-to-draw-a-circle-using-midpoint-circle-drawing-algorithm/#respond Fri, 29 Jul 2022 07:58:45 +0000 https://www.programmerbay.com/?p=2180 An algorithm that is used to find points required for plotting and converting a circle over display. Midpoint circle drawing algorithm snippet provided in this Article. If you are interested to see the algorithm ( Go here) .

C Program to draw a circle using the midpoint circle drawing algorithm

Program:

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int x,y,x_mid,y_mid,radius,dp;
int g_mode,g_driver=DETECT;
clrscr();
initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
printf("*********** MID POINT Circle drawing algorithm ********\n\n");
printf("\nenter the coordinates= ");
scanf("%d %d",&x_mid,&y_mid);
printf("\n now enter the radius =");
scanf("%d",&radius);
x=0;
y=radius;
dp=1-radius;
do
{
putpixel(x_mid+x,y_mid+y,YELLOW);
putpixel(x_mid+y,y_mid+x,YELLOW);
putpixel(x_mid-y,y_mid+x,YELLOW);
putpixel(x_mid-x,y_mid+y,YELLOW);
putpixel(x_mid-x,y_mid-y,YELLOW);
putpixel(x_mid-y,y_mid-x,YELLOW);
putpixel(x_mid+y,y_mid-x,YELLOW);
putpixel(x_mid+x,y_mid-y,YELLOW);
if(dp<0) {
dp+=(2*x)+1;
}
else{
y=y-1;
dp+=(2*x)-(2*y)+1;
}
x=x+1;
}while(y>x);
getch();
}

Output:

circle drawing algorithm

]]>
https://programmerbay.com/program-to-draw-a-circle-using-midpoint-circle-drawing-algorithm/feed/ 0
C Program to Rotate a Line https://programmerbay.com/c-program-to-rotate-a-line/ https://programmerbay.com/c-program-to-rotate-a-line/#respond Wed, 27 Jul 2022 12:48:40 +0000 https://www.programmerbay.com/?p=1863 Rotation can be defined as moving an object in a circular path at a given angle theta.

If there is a positive angle, it would rotate in anticlockwise whereas if it appears to a negative angle, the object would rotate in clockwise. Here’s is C program to rotate a line in computer Graphics. The below program is rotation program in C.

Program to show rotation of a line in Computer Graphics

Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int pivot_x,pivot_y,x,y;
double degree,radian;
int rotated_point_x,rotated_point_y;
initgraph(&gd,&gm,"C://TURBOC3//BGI");
cleardevice();
printf("\t\t*********** ROTATION *********** \n");
printf("\n Enter an initial coordinates of the line = ");
scanf("%d %d",&pivot_x,&pivot_y);
printf("\n Enter a final coordinates of the line = ");
scanf("%d %d",&x,&y);
line(pivot_x,pivot_y,x,y);
printf("\n\n Now, Enter a degree = ");
scanf("%lf",&degree);
radian=degree*0.01745;
rotated_point_x=(int)(pivot_x +((x-pivot_x)*cos(radian)-(y-pivot_y)*sin(radian)));
rotated_point_y=(int)(pivot_y +((x-pivot_x)*sin(radian)+(y-pivot_y)*cos(radian)));
setcolor(RED);
line(pivot_x,pivot_y,rotated_point_x,rotated_point_y);
getch();
closegraph();
}

 

Output:

Rotation by 30

rotation 30

Rotation by 90

rotation 90

]]>
https://programmerbay.com/c-program-to-rotate-a-line/feed/ 0
Explain Cohen Sutherland Line Clipping Algorithm https://programmerbay.com/cohen-sutherland-line-clipping-algorithm/ https://programmerbay.com/cohen-sutherland-line-clipping-algorithm/#respond Mon, 25 Jul 2022 18:12:28 +0000 https://www.programmerbay.com/?p=2165 Line clipping is a clipping concept in which lines that lies outside the clipping window is removed from the clip region. As a result, only lines which is inside the view plane are visible. Cohen Sutherland Algorithm is one of the popular line clipping algorithm used for the purpose.

Must Read : What is Clipping in Computer Graphics

What is Cohen Sutherland Line Clipping?

Cohen Sutherland uses region code to clip a portion of the line which is not present in the visible region. It divides a region into 9 columns based on (X_MAX,Y_MAX) and (X_MIN,Y_MIN).

The central part is viewing region or window, all the lines which lie within this region are completely visible. A region code is always assigned to endpoints of the given line.

To check whether the line is visible or not.

Region Code

region code structure

A line can be drawn:

a) Inside the Window, if that is the case, then no clipping is required

b) Completely outside the Window, if that is the case, then no clipping is required because entire line isn’t in the window.

c) Partially inside or outside the window, if that is the case, then we need to find the intersection point and clipping would take place.

Algorithm of Cohen Sutherland Line Clipping

1) First, define a window or View plane. Get coordinates from the user of a line.

2) Initialize the region code for initial and end coordinates of a line to 0000.
3) Check whether the line lies within, partially or outside the window.

  •   Now, Assign the region code for both the initial and end coordinates.
  •  After Assigning, If both the endpoints give 0000, then the line is completely within the window.
  •  Else perform AND operation, if the result is not 0000, then the line is not inside the window and that line would not be considered for clipping.
  • Else the line is partially inside the window.

4) After confirming the line is partially inside the window, the next step is to find the intersection point at the window boundary. By using the following formula:

If the line passes through the top,
x=x+(W_ymax-y)/slope ;
y=W_ymax;
If the line passes through the bottom,
x=x+(W_ymin-y)/slope ;
y=W_ymin;
if the line passes through the left region,
y=y+(W_xmin-x)*slope,
x1=W_xmin;
if the line passes through the right region,
y1=y1+(W_xmax-x1)*slope ,
x1=W_xmax

5) Now, overwrite the endpoint with a new one and update it.
6) Repeat 4th step till your line doesn’t get clipped completely.

C Program for Clipping a line using Cohen Sutherland Algorithm

 

 

]]>
https://programmerbay.com/cohen-sutherland-line-clipping-algorithm/feed/ 0