computerGraphicReport (1) 1 (1) 000000000000000000
computerGraphicReport (1) 1 (1) 000000000000000000
Submitted By
Name: Keshav Prasad Subedi
Class Roll number: 10
Semester: 5th
Submitted to:
Er. Sujan Sharma
lecturer
….…………………
Signature
Table of Contents
Program of Scaling
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.
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;
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");
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:
int main() {
int gd=DETECT, gm;
int x1, y1, x2, y2, steps, k;
float xincr, yincr, x, y, dx, dy;
dx = x2 - x1;
dy = y2 - y1;
xincr = dx / (float)steps;
yincr = dy / (float)steps;
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>
int main() {
int gd, gm = DETECT;
detectgraph(&gd, &gm);
initgraph(&gd, &gm, "C:\\TurboC3\\BGI");
getch();
return 0;
}
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;
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;
setcolor(7);
line(x1, y1, x2, y2);
outtextxy(x2 + 5, y2, "Object");
t = a * (3.14 / 180);
setcolor(15);
line(x3, y3, x4, y4);
outtextxy(x4 + 5, y4, "Image");
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;
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;
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);
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;
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));
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>
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 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 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);
}
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.