0% found this document useful (0 votes)
36 views35 pages

computerGraphicReport (1) 1 (1) 000000000000000000

The document discusses computer graphics programs submitted by a student for their 5th semester. It includes the source code and output for programs to draw basic shapes like lines, circles and complex shapes like a house and car using graphics functions. It also includes algorithms for drawing lines, flood fill, boundary fill, scaling, rotation and translation.
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)
36 views35 pages

computerGraphicReport (1) 1 (1) 000000000000000000

The document discusses computer graphics programs submitted by a student for their 5th semester. It includes the source code and output for programs to draw basic shapes like lines, circles and complex shapes like a house and car using graphics functions. It also includes algorithms for drawing lines, flood fill, boundary fill, scaling, rotation and translation.
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/ 35

Vedas College

(Affiliated to Tribhuvan University)


Jawalakhel, Lalitpur, Nepal

Report of Computer Graphics


Department of Bachelor in Computer Application
Faculty of Humanities and social science
Tribhuvan University (TU)
Kirtipur, Nepal

Submitted By
Name: Keshav Prasad Subedi
Class Roll number: 10
Semester: 5th

Submitted to:
Er. Sujan Sharma

lecturer

….…………………

Signature
Table of Contents

Lab Programs Remarks Date


WAP to creating line

WAP to create man object


1
WAP to create house shape

Program for creating simple car shape

To implement DDA algorithm for drawing a line


segment between two given end points A(x1, x2)
and B(x2, y2).
WAP to implement bresenham’s line draw
algorithm.
2
Program for 4-connected flood fill.

Program for 4-connected Boundary fill.

Program of Scaling

Program for Rotation

Program for translation


3

Program for X-shear

Program for Y-shear

4 Program implements a simple analog clock using


OpenGL (GLUT library) in C
WAP for creating line

Source Code:
#include<stdio.h>
#include<graphics.h>
void main(){
int gd=DETECT ,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
line(100,100,150,150);
line(250,250,250,200);
line(180,180,225,180);
outtextxy(200,200,"keshab_10");
getch();
closegraph();
}

Output:

Note:
Initgraph: The initgraph function is not a standard function in the C programming language.
Instead, it is typically associated with graphics programming using the Borland Graphics
Interface (BGI) library in Turbo C, a popular compiler and integrated development
environment (IDE) for DOS-based systems.
Line: The line function is not a standard library function. However, I assume you are
referring to drawing a line on the screen, especially in the context of graphics programming.
If you are using a graphics library like the BGI (Borland Graphics Interface) in Turbo C, you
can use the line function provided by that library.

WAP to create man object


Sources Code:
#include <stdio.h>
#include <graphics.h>
void main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
circle(200, 100, 20);
line(200, 120, 200, 200);
line(200, 140, 150, 160);
line(200, 140, 250, 160);
line(200, 200, 170, 250);
line(200, 200, 230, 250);
outtextxy(250, 140, "keshab_!0");
getch();
closegraph();
}

Output:

Note:
Circle: the circle function is part of the graphics library, particularly in the context of Turbo C's BGI
(Borland Graphics Interface) library. The circle function is used to draw a circle on the screen.
Here's the general syntax of the circle function:
circle(int x, int y, int radius);
WAP to create create House shape
Sources Code:
#include <stdio.h>
#include <graphics.h>

void main() {
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

rectangle(150, 250, 450, 450);


rectangle(250, 300, 350, 400);
line(300, 250, 300, 300);
line(400, 250, 400, 300);
line(350, 300, 350, 250);
line(250, 350, 350, 350);

line(150, 250, 300, 150);


line(450, 250, 300, 150);

outtextxy(200, 460, "keshab_10");

getch();
closegraph();
}

Output:
Outtextxy: The outtextxy function is associated with the graphics programming library,
particularly in the context of Turbo C's BGI (Borland Graphics Interface) library. This
function is used to display text on the screen at a specified position.
Program for creating simple car shape
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
int main(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
cleardevice();
line(150, 100, 242, 100);
ellipse(242, 105, 0, 90, 10, 5);
line(150, 100, 120, 150);
line(252, 105, 280, 150);
line(100, 150, 320, 150);
line(100, 150, 100, 200);
line(320, 150, 320, 200);
line(100, 200, 110, 200);
line(320, 200, 310, 200);
arc(130, 200, 0, 180, 20);
arc(290, 200, 0, 180, 20);
line( 270, 200, 150, 200);
circle(130, 200, 17);
outtextxy(300,200,"Keshab_10");
circle(290, 200, 17);
getch();
return 0;
Output:
WAP to implement bresenham’s line draw algorithm.
Sources Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

int main() {
int p,x,y,x1,y1,x2,y2,dx,dy;
int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

printf("Enter value of x1: ");


scanf("%d",&x1);
printf("Enter value of y1: ");
scanf("%d",&y1);
printf("Enter value of x2: ");
scanf("%d",&x2);
printf("Enter value of y2: ");
scanf("%d",&y2);

x=x1;
y=y1;

dx=x2-x1;
dy=y2-y1;

putpixel(x,y,2);

p=(2*dy-dx);

while(x<=x2){
if(p<0){
x=x+1;
p=p+2*dy;
}
else{
x=x+1;
y=y+1;
p=p+(2*dy)-(2*dx);
}
putpixel(x,y,7);
}

outtextxy(200,200,"Keshab_10");

getch();
closegraph();
return 0;
}

Output:

Note:
Putpixel: The putpixel function is another graphics-related function associated with the BGI (Borland
Graphics Interface) library in Turbo C. It is used to set the color of a pixel at a specified position on
the screen.
Here's the general syntax of the putpixel function:

void putpixel(int x, int y, int color);


To implement DDA algorithm for drawing a line segment between
two given end points A(x1, x2) and B(x2, y2).
Source Code:
#include<stdio.h>
#include<graphics.h>
#include<math.h>

float round(float a);

int main() {
int gd=DETECT, gm;
int x1, y1, x2, y2, steps, k;
float xincr, yincr, x, y, dx, dy;

printf("Enter x1, y1: ");


scanf("%d %d", &x1, &y1);

printf("Enter x2, y2: ");


scanf("%d %d", &x2, &y2);

dx = x2 - x1;
dy = y2 - y1;

if(abs(dx) > abs(dy))


steps = abs(dx);
else
steps = abs(dy);

xincr = dx / (float)steps;
yincr = dy / (float)steps;

initgraph(&gd, &gm, "c:\\turboc3\\BGI");


x = x1;
y = y1;

for(k = 1; k <= steps; k++) {


y += yincr;
putpixel(round(x), round(y), WHITE);
x += xincr;
}
outtextxy(200, 200, "DDA");
outtextxy(200, 220, "Keshab_10");
outtextxy(x1 + 5, y1 - 5, "(x1,y1)");
outtextxy(x2 + 5, y2 + 5, "(x2,y2)")
getch();
closegraph();
return 0;
}

float round(float a) {
int b = a + 0.5;
return b;
}
Output:
Program for 4-connected flood fill.

Source Code:
#include <stdio.h>
#include <graphics.h>
#include <dos.h>

void flood(int, int, int, int);

int main() {
int gd, gm = DETECT;

detectgraph(&gd, &gm);
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

rectangle(100, 100, 150, 150);


outtextxy(160, 120, "Original");

flood(110, 110, 7, 0);


outtextxy(160, 140, "Flood Fill");

getch();
return 0;
}

void flood(int x, int y, int fill_col, int old_col) {


if (getpixel(x, y) == old_col) {
delay(10);
putpixel(x, y, fill_col);

flood(x + 1, y, fill_col, old_col);


flood(x - 1, y, fill_col, old_col);
flood(x, y + 1, fill_col, old_col);
flood(x, y - 1, fill_col, old_col);
}
}

Output:
Program for 4-connected Boundary fill.
Source Code:
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
void boundary_fill(int x,int y, int fcolor, int bcolor){
if((getpixel(x,y)!=bcolor)&&(getpixel(x,y)!=fcolor)){
delay(1);
putpixel(x,y,fcolor);
boundary_fill(x+1,y,fcolor,bcolor);
boundary_fill(x-1,y,fcolor,bcolor);
boundary_fill(x,y+1,fcolor,bcolor);
boundary_fill(x+1,y-1,fcolor,bcolor);
}
}
int main(){
int x, y,fcolor,bcolor;
int gd=DETECT, gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the seed point (x,y) for a circle (200,200,45):");
scanf("%d%d",&x,&y);
printf("Enter Boundary Color:");
scanf("%d",&bcolor);
printf("Enter new color");
scanf("%d",&fcolor);
outtextxy(100,150,"Keshav Subedi");
circle(200,200,45);
boundary_fill(x,y,fcolor,bcolor);
getch();
return 0;
}
Program for scaling
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

int main() {
int gd = DETECT, gm;
float x1, y1, x2, y2, sx, sy, x3, y3, x4, y4;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

printf("\nEnter the starting point (x1, y1): ");


scanf("%f %f", &x1, &y1);

printf("\nEnter the ending point (x2, y2): ");


scanf("%f %f", &x2, &y2);

printf("\nEnter scaling factors (sx, sy): ");


scanf("%f %f", &sx, &sy);

setcolor(7);
line(x1, y1, x2, y2);
outtextxy(x2 + 5, y2, "subedikeshab");
x3 = x1 * sx;
y3 = y1 * sy;
x4 = x2 * sx;
y4 = y2 * sy;

setcolor(15);
line(x3, y3, x4, y4);
outtextxy(x4 + 5, y4, "image");

getch();
closegraph();

return 0;
}

Output:
Program for Rotation
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

int main() {
int gd = DETECT, gm;
float x1, y1, x2, y2, x3, y3, x4, y4, a, t;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

printf("\nEnter coordinates of starting point (x1, y1): ");


scanf("%f%f", &x1, &y1);

printf("\nEnter coordinates of ending point (x2, y2): ");


scanf("%f%f", &x2, &y2);

printf("\nEnter angle for rotation (degree): ");


scanf("%f", &a);

setcolor(7);
line(x1, y1, x2, y2);
outtextxy(x2 + 5, y2, "Object");

t = a * (3.14 / 180);

x3 = (x1 * cos(t)) - (y1 * sin(t));


y3 = (x1 * sin(t)) + (y1 * cos(t));
x4 = (x2 * cos(t)) - (y2 * sin(t));
y4 = (x2 * sin(t)) + (y2 * cos(t));

setcolor(15);
line(x3, y3, x4, y4);
outtextxy(x4 + 5, y4, "Image");

outtextxy(200, 200, "subedi_keshab");

getch();
return 0;
}

Output:
Program for translation
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

int main() {
int gd = DETECT, gm;
int x1, y1, x2, y2, tx, ty, x3, y3, x4, y4;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

printf("\nEnter the starting point of line segment (x1, y1):");


scanf("%d %d", &x1, &y1);

printf("\nEnter the ending point of line segment (x2, y2):");


scanf("%d %d", &x2, &y2);

printf("\nEnter translation vector (tx, ty):");


scanf("%d %d", &tx, &ty);

setcolor(7);
line(x1, y1, x2, y2);
outtextxy(x2 + 5, y2, "Object");

x3 = x1 + tx;
y3 = y1 + ty;
x4 = x2 + tx;
y4 = y2 + ty;

setcolor(15);
line(x3, y3, x4, y4);
outtextxy(x4 + 5, y4, "Image");
outtextxy(100, 100, "subedikeshab");

getch();
return 0;
}

Output:
Program for X-shear
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>

int main() {
int gd = DETECT, gm;
float shx, shy;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

printf("Enter shear factor shx along x-axis: ");


scanf("%f", &shx);

line(100, 0, 200, 0);


line(200, 0, 200, 200);
line(200, 200, 100, 200);
line(100, 200, 100, 0);
printf("X-shear");

setcolor(12);
line((100 + (0 * shx)), 0, (200 + (0 * shx)), 0);
line((200 + (0 * shx)), 0, (200 + (200 * shx)), 200);
line((200 + (200 * shx)), 200, (100 + (200 * shx)), 200);
line((100 + (200 * shx)), 200, (100 + (0 * shx)), 0);

outtextxy(300, 300, "subedikeshab");

getch();
return 0;
}
Output:
Program for Y-shear

#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>

int main() {
int gd = DETECT, gm;
float shx, shy;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

printf("Enter shear factor shy along y-axis: ");


scanf("%f", &shy);

line(100, 10, 200, 10);


line(200, 10, 200, 200);
line(200, 200, 100, 200);
line(100, 200, 100, 10);
printf("Y-shear");

setcolor(12);
line(100, 10 + (shy * 100), 200, 10 + (shy * 200));
line(200, 10 + (shy * 200), 200, 200 + (shy * 200));
line(200, 200 + (shy * 200), 100, 200 + (shy * 100));
line(100, 200 + (shy * 100), 100, 10 + (shy * 100));

outtextxy(300, 300, "subedi_keshab");

getch();
return 0;
}
Output:
Program implements a simple analog clock using OpenGL (GLUT
library) in C
/
Source Code:
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <time.h>
#include <sys/timeb.h>
#include <string.h>

#define HOUR_HAND_LENGTH 50.0


#define MINUTE_HAND_LENGTH 70.7106781187
#define SECOND_HAND_LENGTH 78.1024967591
#define CLOCK_RADIUS 80.0
#define PI 3.14159
#define CLOCK_VOL 100.00
#define ANGLE_ONE_MIN PI / 30.0

typedef struct Point {


double x, y;
} point;

double secondAngle = 0, minuteAngle = 0, hourAngle = 0;

void init(void) {
// Initialize OpenGL
gluOrtho2D(-100, 100, -100, 100);
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glShadeModel(GL_SMOOTH);
glLoadIdentity();
}

void drawPoint(point p) {
// Draw a point
glBegin(GL_POINTS);
glVertex2f(p.x, p.y);
glEnd();
}

void drawLine(double x, double y, double angle) {


// Draw a line
glVertex2f(x, x);
glVertex2f(y * cos(angle), y * sin(angle));
}

void addDate() {
// Add date to clock display
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char* s = asctime(timeinfo);
char y[20];
int count_space = 0, i;
for (i = 0; i < strlen(s); ++i) {
if (s[i] == ' ') {
count_space++;
y[i] = ',';
} else {
y[i] = s[i];
}
if (count_space == 3)
break;
}
y[i] = '\0';
glColor3f(1.0, 1.0, 0.0);
glRasterPos2f(0, 90);
for (i = 0; y[i] != '\0'; ++i) {
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, y[i]);
}
}

void drawMarks(void) {
// Draw clock marks
point sp, ep, p;
int count = 0;
double i = PI / 6.0;
sp.x = 0, sp.y = 0;
ep.x = 0, ep.y = CLOCK_RADIUS;
glPointSize(10.0);
drawPoint(sp);
for (i = 0; i <= 2 * PI; i += PI / 30.0) {
if (i == PI / 2.0) {
p.x = CLOCK_RADIUS;
} else if (i == 3 * PI / 2.0) {
p.x = -CLOCK_RADIUS;
} else {
p.x = ep.y * sin(i);
}
p.y = ep.y * cos(i);
if (count % 5 == 0) {
glPointSize(7.0);
drawPoint(p);
} else {
glPointSize(3.0);
drawPoint(p);
}
count++;
}
}

void drawClock(void) {
// Draw clock hands
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glLineWidth(1.0);
drawMarks();
addDate();
glLineWidth(5.0);
glColor3f(1.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
drawLine(0.0, HOUR_HAND_LENGTH, -hourAngle + PI / 2);
glEnd();
glLineWidth(3.0);
glColor3f(0.0f, 1.0f, 1.0f);
glBegin(GL_LINES);
drawLine(0.0, MINUTE_HAND_LENGTH, -minuteAngle + PI / 2);
glEnd();
glLineWidth(1.0);
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
drawLine(0.0, SECOND_HAND_LENGTH, -secondAngle + PI / 2);
glEnd();
glFlush();
glutSwapBuffers();
}

void reshape(int w, int h) {


// Maintain aspect ratio when window is resized
double aspectRatio;
if (h == 0) {
h = 1;
}
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (double)w / (double)h;
if (w <= h)
glOrtho(-CLOCK_VOL, CLOCK_VOL, -CLOCK_VOL / aspectRatio, CLOCK_VOL / aspectRatio,
1.0, -1.0);
else
glOrtho(-CLOCK_VOL * aspectRatio, CLOCK_VOL * aspectRatio, -CLOCK_VOL, CLOCK_VOL,
1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void redraw() {
// Update clock hands based on system time
struct timeb tb;
time_t tim = time(0);
struct tm* t;
t = localtime(&tim);
ftime(&tb);
secondAngle = (double)(t->tm_sec + (double)tb.millitm / 1000.0) / 30.0 * PI;
minuteAngle = (double)(t->tm_min) / 30.0 * PI + secondAngle / 60.0;
hourAngle = (double)(t->tm_hour > 12 ? t->tm_hour - 12 : t->tm_hour) / 6.0 * PI + minuteAngle /
12.0;
glutPostRedisplay();
glutTimerFunc(15, redraw, 1);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Clock");
init();
glutDisplayFunc(drawClock);
glutReshapeFunc(reshape);
glutTimerFunc(15, redraw, 1);
glutMainLoop();
return 0;
}

Output:
Explanation
This program creates a basic analog clock using OpenGL (GLUT library) in C. Here's a simplified
breakdown:

• Headers: Includes necessary C and OpenGL headers for graphics and time management.
• Constants: Defines lengths of clock hands and the clock radius.
• Structure: Defines a 2D point structure called Point.
• Function Prototypes: Declarations for functions used in the program.
• Initialization Function (init): Sets up OpenGL settings for projection and background color.
• Drawing Functions: Functions to draw points and lines.
• Date Function (addDate): Fetches current system time and adds the date to the clock display.
• Clock Structure Drawing Function (drawMarks): Draws clock marks like hour and minute
indicators.
• Display Function (drawClock): Clears the screen and draws clock hands using current time angles.
• Reshape Function: Maintains aspect ratio when the window is resized.
• Timer Function (redraw): Updates clock hand angles based on the current system time and triggers
redrawing.
• Main Function: Initializes GLUT, sets up the window, and registers callback functions for display,
reshape, and timer. Enters the GLUT main loop to handle events and update the clock continuously.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy