The document contains implementations of various line and shape drawing algorithms, including DDA and Bresenham's line, circle, and ellipse algorithms, along with methods for drawing a house. Additionally, it discusses clipping algorithms such as Cohen-Sutherland and Sutherland-Hodgman. Each program is accompanied by C/C++ source code demonstrating the specific algorithm's implementation.
Program 1 demonstrates the implementation of the DDA line drawing algorithm in OpenGL, utilizing source code to set up the drawing context and define pixel positions based on linear interpolation.
Program 2 presents the Bresenham's line algorithm, showcasing efficient pixel selection for line drawing using integer calculations to determine the pixel positions.
Program 3 implements Bresenham's circle algorithm, using mathematical computations to plot circle points based on radius and center coordinates.
Program 4 details the midpoint ellipse drawing algorithm, calculating pixel locations based on the ellipse's axes, iterating until the entire ellipse is rendered.
Program 5 describes how to draw a house in OpenGL, layering polygons for walls, windows, and roofs while managing colors for visual differentiation.
Program 1 of Section B explains the Cohen-Sutherland algorithm for clipping lines against a rectangular clipping window, using code to determine visibility and adjust endpoints.
Program 2 details the Sutherland-Hodgeman polygon clipping algorithm, processing polygon edges against defined clipping boundaries to render the final visible polygon.
Program 3 illustrates window to viewport transformation, demonstrating how to map coordinates from a defined window to a target viewport using scaling factors.
Program 4 showcases various 2D transformations (Translation, Scaling, Rotation) using user-defined points to manipulate and redraw shapes based on transformation criteria.
Program 5 focuses on rotating a triangle around a pivot point, demonstrating geometric transformations through calculated rotations using user-provided angle and reference coordinates.
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
glutInitWindowPosition(50, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("House Section OpenGL");
init();
glutDisplayFunc(buildHouse);
glutMainLoop();
return 0;
}
Output:
Hay listen carefully, don’t draw this home with your own color, just use pencil.
16.
SECTION-B:::::::::::::::::
PROGRAM-1::::::::::::::
Program Title:
Write aprogram to implement Cohen Sutherland Line Clipping algorithm.
Source Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.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);
int main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
Output:
PROGRAM-3::::::::::::::
Program Title:
Write aprogram to implement Window to Viewport transformation.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<math.h>
int main()
{
int xwmin,ywmin,xwmax,ywmax,xv1,yv1;
int xvmin,xvmax,yvmin,yvmax,xw,yw,xv,yv;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
printf("Enter the window coordinates xwmin,xwmax,ywmin,ywmaxn");
scanf("%dt%dt%dt%d",&xwmin,&xwmax,&ywmin,&ywmax);
line(xwmin-25,xwmin-25,xwmin-25,ywmax+50);
line(xwmin-40,ywmax+25,xwmax+50,ywmax+25);
outtextxy(xwmin+5,ywmax+5,"Window");
line(xwmin,ywmin,xwmin,ywmax);
line(xwmin,ywmax,xwmax,ywmax);
Output:
PROGRAM-4::::::::::::::
Program Title:
Write aprogram to implement the following 2D transformations:-
a) Translation
b) Scaling
c) Rotation
Source Code:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
int main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
32.
float t;
initgraph(&gd,&gm,"c:tcbg:");
printf("t Programfor basic transactions");
printf("nt Enter the points of triangle");
setcolor(1);
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);
getch();
printf("n 1.Transactionn 2.Rotationn 3.Scallingn 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("n Enter the translation factor");
scanf("%d%d",&xt,&yt);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
33.
getch();
case 2:
printf("n Enterthe angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 3:
printf("n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
Output:
PROGRAM-5::::::::::::::
Program Title:
Write aprogram to draw a triangle and rotate about the pivot point.
Source Code:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
int main()
{
int gd=DETECT,gm;
int i,xmid,ymid,x1,y1,x2,y2,x3,y3,x,y,dy,dx,p,gap=50,temp,xr,yr;
int x1dash,y1dash,x2dash,y2dash,x3dash,y3dash;
float m;
double theta;
char str[5];
//clrscr();
initgraph(&gd,&gm,"..bgi");
printf("Enter first co-ords of the trianglen");
36.
scanf("%d %d",&x1,&y1);
printf("Enter secondco-ords of the trianglen");
scanf("%d %d",&x2,&y2);
printf("Enter third co-ords of the trianglen");
scanf("%d %d",&x3,&y3);
xmid= getmaxx()/2;
ymid= getmaxy()/2;
line(5,ymid,getmaxx()-5,ymid);
line(xmid+3,5,xmid+3,getmaxy()-5);
for( i= xmid+gap;i<getmaxx()-5;i=i+gap)
{
outtextxy(i,ymid-3,"|");
//itoa(i-xmid,str,10);
outtextxy(i,ymid+3,str);
}
for( i= ymid-gap;i>5;i=i-gap)
{
outtextxy(xmid,i,"-");
//itoa(ymid-i,str,10);
outtextxy(xmid+5,i,str);
}
for( i= xmid-gap;i>5;i=i-gap)
{
outtextxy(i,ymid-3,"|");
//itoa(-(xmid-i),str,10);
outtextxy(i-6,ymid+3,str);
}
for( i= ymid+gap;i<getmaxy()-5;i=i+gap)
{
outtextxy(xmid,i,"-");
//itoa(-(i-ymid),str,10);
outtextxy(xmid+8,i,str);
}
line(x1+xmid,ymid-y1,x2+xmid,ymid-y2);
line(x2+xmid,ymid-y2,x3+xmid,ymid-y3);
line(x3+xmid,ymid-y3,x1+xmid,ymid-y1);
printf("Enter the degree to rotaten");
37.
scanf("%lf",&theta);
theta= ((float) theta*3.14f )/(float)180; // converting theta to radian
printf("Enter the arbitrary point to rotaten");
scanf("%d%d",&xr,&yr);
x1dash=xr+(x1-xr)*cos(theta)-(y1-yr)*sin(theta);
x2dash=xr+(x2-xr)*cos(theta)-(y2-yr)*sin(theta);
x3dash=xr+(x3-xr)*cos(theta)-(y3-yr)*sin(theta);
y1dash=yr+(x1-xr)*sin(theta)+(y1-yr)*cos(theta);
y2dash=yr+(x2-xr)*sin(theta)+(y2-yr)*cos(theta);
y3dash=yr+(x3-xr)*sin(theta)+(y3-yr)*cos(theta);
line(x1dash+xmid,ymid-y1dash,x2dash+xmid,ymid-y2dash);
line(x2dash+xmid,ymid-y2dash,x3dash+xmid,ymid-y3dash);
line(x3dash+xmid,ymid-y3dash,x1dash+xmid,ymid-y1dash);
getch();
closegraph();
}
Input: