0% found this document useful (0 votes)
21 views29 pages

CGR Practicals Output Full

Computer Graphics msbte practicals

Uploaded by

rajkirdath369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views29 pages

CGR Practicals Output Full

Computer Graphics msbte practicals

Uploaded by

rajkirdath369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Exp 2

#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
main()
{
float x,y,x1,y1, x2,y2, dx, dy, length;
int i,gd=DETECT,gm;
clrscr();
printf("\n Enter the coordinates of first point:");
printf("\n Enter x1");
scanf("%f",&x1);
printf("\n Enter y1 :");
scanf("%f",&y1);
printf("\n Enter the coordinates of second point :");
printf("\n Enter x2 :");
scanf("%f",&x2);
printf("\n Enter y2:");
scanf("%f",&y2);
initgraph(&gd, &gm," ");
dx= abs (x2-x1);
dy= abs (y2-y1);
if (dx >= dy)
{
length = dx;
}
else
{
length = dy;
}
dx = (x2-x1)/length;
dy =(y2-y1)/length;
x =x1+0.5;
y =y1+0.5;
i = 1;
while(i<=length)
{
putpixel(x,y,WHITE);
x=x+dx;
y=y+dy;
i=i+1;
}
getch();
closegraph();
}
Exp3:-
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx =x1-x0;
dy =y1-y0;
x=x0;
y = y0;
p=2 * dy - dx ;
while(x<x1)
{
if(p >= 0)
{ putpixel (x, y, 7);
y = y + 1;
p=p+2 * dy -2^ * dx;
}
else
{
putpixel(x, y, 7);
p=p+2*dy;
}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, y1);
drawline(x0, y0, x1, y1);
return 0;
}
Exp 4:-

#include <stdio.h>
#include <graphics.h>
#include<conio.h>
#include<dos.h>
void drawCircle(int xc, int yc, int r) {
int x = 0, y = r;
int d = 3 - 2 * r;
while (x <= y)
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
putpixel(xc + y, yc + x, WHITE);
putpixel(xc - y, yc + x, WHITE);
putpixel(xc + y, yc - x, WHITE);
putpixel(xc - y, yc - x, WHITE);
if (d < 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
int main() {
int gd = DETECT, gm;
int xc, yc, r;
initgraph(&gd, &gm, "");
printf("Enter center coordinates (x y): ");
scanf("%d %d", &xc, &yc);
printf("Enter radius: ");
scanf("%d", &r);
drawCircle(xc, yc, r);
delay(5000);
closegraph();
return 0;
}
Exp 5:-

#include <graphics.h>
#include <stdio.h>
#include<conio.h>
#include<dos.h>
void floodFill(int x, int y, int oldColor, int newColor)
{
if(getpixel(x,y)==oldColor)
{
putpixel(x,y,newColor);
floodFill(x + 1, y, oldColor, newColor);
floodFill(x ,y + 1, oldColor, newColor);
floodFill(x - 1 , y , oldColor, newColor);
floodFill(x, y - 1, oldColor, newColor);
}
}
int main()
{
int gd = DETECT,radius, gm;
int x,y;
printf("Enter x and y positions for circle \n");
scanf("%d%d",&x,&y);
printf("Enter radius of circle \n");
scanf("%d",&radius);
initgraph(&gd,&gm," ");
circle(x,y,radius);
floodfill(x,y,0,15);
delay(5000);
closegraph();
return 0;
}
Exp 6 :-

#include <graphics.h>
#include <stdio.h>
#include <dos.h>
void boundaryFill(int x, int y, int f_color, int b_color) {
if(getpixel(x,y)!=b_color && getpixel(x,y)!= f_color)
{
// Fill current pixel
putpixel(x, y, f_color);
boundaryFill(x + 1, y, f_color, b_color);
boundaryFill(x,y + 1 , f_color, b_color);
boundaryFill(x - 1, y + 1, f_color, b_color);
boundaryFill(x, y - 1, f_color, b_color);
}
}
int main() {
int gd = DETECT, gm;
int n;
int x[10],y[10];
int fillX,fillY;
int f_color,b_color;
initgraph(&gd, &gm, "");
printf("Enter th number of vertices for the ploygon (max 10):");
scanf("%d",&n);
printf("Enter the coordinates of the polygon vertices :\n");
for(int i=0;i<n;i++)
{
printf("vertex %d (x y):",i+1);
scanf("%d%d",&x[i],&y[i]);
}
drawpoly(n,x);
printf("enter the fill point(x y):");
scanf("%d %d",&fillX,&fillY);
printf("Enter the fill color(0 - 15):");
scanf("%d",&f_color);
printf("Enter the boundary color(0 - 15):");
scanf("%d",&b_color);
boundaryFill(fillX,fillY,f_color,b_color);
delay(5000);
closegraph();
return 0;
}
Exp 7:-

#include <stdio.h>
#include <graphics.h>
#include<dos.h>
void translatePoint(int *x, int *y, int tx, int ty) {
*x = *x + tx;
*y = *y + ty;
}
void scalePoint(int *x, int *y, float sx, float sy) {
*x = *x * sx;
*y = *y * sy;
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x1 = 100, y1 = 100;
int x2 = 200, y2 = 100;
int x3 = 150, y3 = 50;
setcolor(RED);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
int tx = 50, ty = 50;
translatePoint(&x1, &y1, tx, ty);
translatePoint(&x2, &y2, tx, ty);
translatePoint(&x3, &y3, tx, ty);
setcolor(GREEN);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
float sx = 1.5, sy = 1.5;
scalePoint(&x1, &y1, sx, sy);
scalePoint(&x2, &y2, sx, sy);
scalePoint(&x3, &y3, sx, sy);
setcolor(BLUE);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
closegraph();
return 0;
}
Exp 8:-

#include <graphics.h>
#include <math.h>
#include <stdio.h>
#include<conio.h>
void main() {
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3;
int nx1, ny1, nx2, ny2, nx3, ny3;
float angle;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("Enter coordinates of triangle (x1 y1 x2 y2 x3 y3): ");
scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
printf("Enter rotation angle (in degrees): ");
scanf("%f", &angle);
angle = (angle * 3.14159) / 180;
nx1 = x1 * cos(angle) - y1 * sin(angle);
ny1 = x1 * sin(angle) + y1 * cos(angle);
nx2 = x2 * cos(angle) - y2 * sin(angle);
ny2 = x2 * sin(angle) + y2 * cos(angle);
nx3 = x3 * cos(angle) - y3 * sin(angle);
ny3 = x3 * sin(angle) + y3 * cos(angle);
setcolor(RED);
line(nx1, ny1, nx2, ny2);
line(nx2, ny2, nx3, ny3);
line(nx3, ny3, nx1, ny1);
getch();
closegraph();
}
Exp 9:-

#include <graphics.h>
#include <stdio.h>
#include<conio.h>
void main() {
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3;
int nx1, ny1, nx2, ny2, nx3, ny3;
int choice, shx, shy;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("Enter coordinates of triangle (x1 y1 x2 y2 x3 y3): ");
scanf("%d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3);
setcolor(WHITE);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
printf("\n1. Reflection about X-axis");
printf("\n2. Reflection about Y-axis");
printf("\n3. X-Shear");
printf("\n4. Y-Shear");
printf("\nEnter your choice (1-4): ");
scanf("%d", &choice);
switch(choice) {
case 1:
nx1 = x1;
ny1 = -y1;
nx2 = x2;
ny2 = -y2;
nx3 = x3;
ny3 = -y3;
break;
case 2:
nx1 = -x1;
ny1 = y1;
nx2 = -x2;
ny2 = y2;
nx3 = -x3;
ny3 = y3;
break;
case 3:
printf("Enter X-shear factor: ");
scanf("%d", &shx);
nx1 = x1 + shx * y1;
ny1 = y1;
nx2 = x2 + shx * y2;
ny2 = y2;
nx3 = x3 + shx * y3;
ny3 = y3;
break;
case 4: // Y-Shear
printf("Enter Y-shear factor: ");
scanf("%d", &shy);
nx1 = x1;
ny1 = y1 + shy * x1;
nx2 = x2;
ny2 = y2 + shy * x2;
nx3 = x3;
ny3 = y3 + shy * x3;
break;
}
setcolor(RED);
line(nx1, ny1, nx2, ny2);
line(nx2, ny2, nx3, ny3);
line(nx3, ny3, nx1, ny1);
getch();
closegraph();
}
Exp 10:-
#include <graphics.h>
#include <stdio.h>
#include< conio.h>
void main()
{
int gd = DETECT, gm;
int x1, y1, z1, x2, y2, z2, x3, y3, z3; // Original coordinates
int tx, ty, tz; // Translation factors
float sx, sy, sz; // Scaling factors
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("Enter coordinates of 3D triangle (x1 y1 z1 x2 y2 z2 x3 y3 z3): ");
scanf("%d %d %d %d %d %d %d %d %d", &x1, &y1, &z1, &x2, &y2, &z2, &x3,
&y3, &z3);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
printf("Enter translation factors (tx ty tz): ");
scanf("%d %d %d", &tx, &ty, &tz);
printf("Enter scaling factors (sx sy sz): ");
scanf("%f %f %f", &sx, &sy, &sz);
x1 = (x1 + tx) * sx;
y1 = (y1 + ty) * sy;
z1 = (z1 + tz) * sz;
x2 = (x2 + tx) * sx;
y2 = (y2 + ty) * sy;
z2 = (z2 + tz) * sz;
x3 = (x3 + tx) * sx;
y3 = (y3 + ty) * sy;
z3 = (z3 + tz) * sz;
setcolor(RED);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
closegraph();
}
Exp 11:-

#include <graphics.h>
#include <math.h>
#include <stdio.h>
void multiply(float a[4][4], float b[4][4], float result[4][4])
{
int i,j,k;
for( i = 0; i<4; i++)
{
for( j = 0; j<4; j++)
{
result[i][j] = 0;
for( k = 0; k < 4; k++)
{
result[i][j] += a[i][k] * b[k][j];
}
}
}
}
void main()
{
int gd = DETECT, gm;
float points[8][4] = {
{100,100,100,1}, {200,100,100,1},
{200,200,100,1}, {100,200,100,1},
{100,100,200,1}, {200,100,200,1},
{200,200,200,1}, {100,200,200,1}
};
float angle, x, y, z;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("Enter rotation angles (in degrees) for X, Y, Z axes: ");
scanf("%f %f %f", &x, &y, &z);
x = x * M_PI / 180;
y = y * M_PI / 180;
z = z * M_PI / 180;
float Rx[4][4] = {
{1, 0, 0, 0},
{0, cos(x), -sin(x), 0},
{0, sin(x), cos(x), 0},
{0, 0, 0, 1}
};
float Ry[4][4] = {
{cos(y), 0, sin(y), 0},
{0, 1, 0, 0},
{-sin(y), 0, cos(y), 0},
{0, 0, 0, 1}
};
float Rz[4][4] = {
{cos(z), -sin(z), 0, 0},
{sin(z), cos(z), 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}
};
float temp[4][4], final[4][4];
multiply(Rx, Ry, temp);
multiply(temp, Rz, final);
for(int i = 0; i < 4; i++) {
line(points[i][0], points[i][1], points[(i+1)%4][0], points[(i+1)%4][1]);
line(points[i+4][0], points[i+4][1], points[((i+1)%4)+4][0], points[((i+1)%4)+4][1]);
line(points[i][0], points[i][1], points[i+4][0], points[i+4][1]);
}
float rotated[8][4];
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 4; j++) {
rotated[i][j] = 0;
for(int k = 0; k < 4; k++) {
rotated[i][j] += points[i][k] * final[k][j];
}
}
}
setcolor(RED);
for(int i = 0; i < 4; i++) {
line(rotated[i][0], rotated[i][1], rotated[(i+1)%4][0], rotated[(i+1)%4][1]);
line(rotated[i+4][0], rotated[i+4][1], rotated[((i+1)%4)+4][0], rotated[((i+1)%4)+4][1]);
line(rotated[i][0], rotated[i][1], rotated[i+4][0], rotated[i+4][1]);
}
getch();
closegraph();
}
Exp 12:-

#include <graphics.h>
#include <stdio.h>
#include<conio.h>
#define INSIDE 0 // 0000
#define LEFT 1 // 0001
#define RIGHT 2 // 0010
#define BOTTOM 4 // 0100
#define TOP 8 // 1000
const int x_min = 100;
const int y_min = 100;
const int x_max = 400;
const int y_max = 300;
int computeCode(int x, int y) {
int code = INSIDE;
if (x < x_min)
code |= LEFT;
else if (x > x_max)
code |= RIGHT;
if (y < y_min)
code |= BOTTOM;
else if (y > y_max)
code |= TOP;
return code;
}
void cohenSutherlandClip(int x1, int y1, int x2, int y2) {
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
int accept = 0;
while (1) {
if (!(code1 | code2)) { // Both points inside
accept = 1;
break;
}
else if (code1 & code2) { // Both points outside
break;
}
else {
int code_out;
int x, y;
code_out = code1 ? code1 : code2;
if (code_out & TOP) {
x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1);
y = y_max;
}
else if (code_out & BOTTOM) {
x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
y = y_min;
}
else if (code_out & RIGHT) {
y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
x = x_max;
}
else if (code_out & LEFT) {
y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
x = x_min;
}
if (code_out == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept) {
setcolor(GREEN);
line(x1, y1, x2, y2);
}
}
void main() {
int gd = DETECT, gm;
int x1, y1, x2, y2;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
rectangle(x_min, y_min, x_max, y_max);
printf("Enter line coordinates (x1 y1 x2 y2): ");
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
setcolor(WHITE);
line(x1, y1, x2, y2);
cohenSutherlandClip(x1, y1, x2, y2);
getch();
closegraph();
}
Exp 13 :-

#include <graphics.h>
#include <stdio.h>
#include<conio.h>
#define INSIDE 0 // 0000
#define LEFT 1 // 0001
#define RIGHT 2 // 0010
#define BOTTOM 4 // 0100
#define TOP 8 // 1000
const int X_MIN = 100;
const int X_MAX = 500;
const int Y_MIN = 100;
const int Y_MAX = 400;
int computeCode(int x, int y) {
int code = INSIDE;
if (x < X_MIN)
code |= LEFT;
else if (x > X_MAX)
code |= RIGHT;
if (y < Y_MIN)
code |= BOTTOM;
else if (y > Y_MAX)
code |= TOP;
return code;
}
void midpointSubdivision(float x1, float y1, float x2, float y2) {
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
float xm, ym;
if ((code1 == 0) && (code2 == 0)) {
line(x1, y1, x2, y2);
return;
}
if (code1 & code2)
return;
xm = (x1 + x2) / 2;
ym = (y1 + y2) / 2;
midpointSubdivision(x1, y1, xm, ym);
midpointSubdivision(xm, ym, x2, y2);
}
void main() {
int gd = DETECT, gm;
float x1, y1, x2, y2;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
rectangle(X_MIN, Y_MIN, X_MAX, Y_MAX);
printf("Enter line coordinates (x1 y1 x2 y2): ");
scanf("%f %f %f %f", &x1, &y1, &x2, &y2);
setcolor(WHITE);
line(x1, y1, x2, y2);
setcolor(GREEN);
midpointSubdivision(x1, y1, x2, y2);
getch();
closegraph();
}
Exp 14:-

#include “stdio.h”
#include “conio.h”
#include “stdlib.h”
#include “dos.h”
#include “math.h”
#include “graphics.h”
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline (PT p1,PT p2);
PT setcode(PT p);
int visibility (PT p1,PT p2);
PT resetendpt (PT p1,PT p2);
main()
{
int gd=DETECT, gm,v;
PT p1,p2,ptemp;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
cleardevice();
printf(“ENTER END-POINT 1 (x,y): “);
scanf(“%d%d”,&p1.x,&p1.y);
printf(“\nENTER END-POINT 2 (x,y): “);
scanf(“%d%d”,&p2.x,&p2.y);
cleardevice();
drawwindow();
getch();
drawline(p1,p2);
getch();
cleardevice();
drawwindow();
midsub(p1,p2);
getch();
closegraph();
return(0);
}midsub(PT p1,PT p2)
{
PT mid;
int v;
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
{
case 0:
drawline(p1,p2);
break;
case 1:break;
case 2:
mid.x = p1.x + (p2.x-p1.x)/2;
mid.y = p1.y + (p2.y-p1.y)/2;
midsub(p1,mid);
mid.x = mid.x+1;
mid.y = mid.y+1;
midsub(mid,p2);
break;
}
}
void drawwindow()
{
setcolor(RED);
line(150,100,450,100);
line(450,100,450,400);
line(450,400,150,400);
line(150,400,150,100);
}

void drawline (PT p1,PT p2)


{
setcolor(15);
line(p1.x,p1.y,p2.x,p2.y);
}

PT setcode(PT p)
{
PT ptemp;
if(p.y<=100) ptemp.code[0]=’1′; else ptemp.code[0]=’0′; if(p.y>=400)
ptemp.code[1]=’1′;
else
ptemp.code[1]=’0′;
if (p.x>=450)
ptemp.code[2]=’1′;
else
ptemp.code[2]=’0′;
if (p.x<=150)
ptemp.code[3]=’1′;
else
ptemp.code[3]=’0′;
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}

int visibility (PT p1,PT p2)


{
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!=’0′)||(p2.code[i]!=’0′))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) &&(p1.code[i]==’1′))
flag=0;
}
if(flag==0)
return(1);
return(2);
}
Exp 15:-

#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include<conio.h>
void bezierCurve(int x[4], int y[4]) {
double t;
int xt, yt;
for(t = 0.0; t <= 1.0; t += 0.0005) {
// Using Bezier curve formula
xt = pow(1-t, 3)*x[0] + 3*t*pow(1-t, 2)*x[1] +
3*pow(t, 2)*(1-t)*x[2] + pow(t, 3)*x[3];

yt = pow(1-t, 3)*y[0] + 3*t*pow(1-t, 2)*y[1] +


3*pow(t, 2)*(1-t)*y[2] + pow(t, 3)*y[3];
putpixel(xt, yt, WHITE);
}
}
void main() {
int gd = DETECT, gm;
int x[4], y[4], i;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
printf("Enter the x and y coordinates of 4 control points:\n");
for(i = 0; i < 4; i++) {
printf("Point %d (x y): ", i+1);
scanf("%d %d", &x[i], &y[i]);
}
for(i = 0; i < 4; i++) {
putpixel(x[i], y[i], RED);
}
setcolor(BLUE);
for(i = 0; i < 3; i++) {
line(x[i], y[i], x[i+1], y[i+1]);
}
setcolor(WHITE);
bezierCurve(x, y);
getch();
closegraph();
}

You might also like