CG4
CG4
CG4
: 4
Aim: To draw an ellipse using Bresenhams
Algorithm
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int h,k;
float a,b;
void printhead()
{
char sap[10] = {'5','0','0','0','4','1','1','9','8'};
glRasterPos2f(-80.0,80.0);
for (int i=0;i<9;i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10,sap[i]);
glRasterPos2f(-80.0,90.0);
for (int i=0;i<6;i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10,name[i]);
EXPERIMENT-7
Clipping (Line, Polygon)
2D Transformation:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
# define ROUND(a) ((int)(a+0.5))
# define absx(x) x+320
# define absy(y) 240-y
void DrawCordinates()
{
line(320,0,320,640);
line(0,240,640,240);
}
void main()
{
int gd=DETECT,gm;
int ang,sx,sy,c,k=0,x1,y1,sh,shx,shy,c1;
int poly[20],choice,i,tdx,tdy,shchoice;
int x,y;
initgraph(&gd,&gm,"C:\\TurboC3\\bgi");
DrawCordinates();
poly[0]=absx(20);
poly[1]=absy(20);
poly[2]=absx(70);
poly[3]=absy(20);
poly[4]=absx(70);
poly[5]=absy(70);
poly[6]=absx(20);
poly[7]=absy(70);
poly[8]=poly[0];
poly[9]=poly[1];
drawpoly(5,poly);
printf("This square will be used for 2D transaformations in this program\nPress
Any Key To Continue...");
closegraph();
do
{
printf("\n****************MENU***************\n");
printf("\n1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Exit");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
DrawCordinates();
drawpoly(5,poly);
printf("\nEnter translation vectors (tx,ty):");
scanf("%d%d",&tdx,&tdy);
for(i=0;i<8;i=i+2)
{
poly[i]=poly[i]+tdx;
poly[i+1]-=tdy;
}
poly[8]=poly[0];
poly[9]=poly[1];
setcolor(RED);
drawpoly(5,poly);
closegraph();
break;
case 2:
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
DrawCordinates();
drawpoly(5,poly);
printf("\nEnter rotation angle:");
scanf("%d",&ang);
for(i=0;i<8;i=i+2)
{
int tx= poly[i]-320;
poly[i]=320+((poly[i]-320)*cos(ang*0.01744)-(240-
poly[i+1])*sin(ang*0.01744));
poly[i+1]=240-((240-poly[i+1])*cos(ang*0.01744)+
(tx)*sin(ang*0.01744));
}
poly[8]=poly[0];
poly[9]=poly[1];
setcolor(RED);
drawpoly(5,poly);
closegraph();
break;
case 3:
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
DrawCordinates();
drawpoly(5,poly);
printf("\nEnter scaling vectors (tx,ty):");
scanf("%d%d",&sx,&sy);
for(i=2;i<8;i=i+2)
{
poly[i]=poly[i]*sx+poly[0]*(1-sx);
poly[i+1]=poly[i+1]*sy+poly[1]*(1-sy);
}
poly[8]=poly[0];
poly[9]=poly[1];
setcolor(RED);
drawpoly(5,poly);
closegraph();
break;
case 4:
initgraph(&gd,&gm,"C:\TurboC++\Disk\TurboC3\BGI");
DrawCordinates();
drawpoly(5,poly);
printf("\n1.Y-axis\n2.X-axis\n3.exit\n");
scanf("%d",&c1);
switch(c1)
{
case 1:
for(k=0;k<8;k=k+2)
{
x1=poly[k];
y1=poly[k+1];
poly[k]=320-(x1-320);
poly[k+1]=y1;
}
poly[8]=poly[0];
poly[9]=poly[1];
setcolor(RED);
drawpoly(5,poly);
break;
case 2:
for(k=0;k<8;k=k+2)
{
x1=poly[k];
y1=poly[k+1];
poly[k]=x1;
poly[k+1]=240-(y1-240);
}
poly[8]=poly[0];
poly[9]=poly[1];
setcolor(RED);
drawpoly(5,poly);
break;
case 3:
break;
}
closegraph();
break;
case 5:
exit(0);
}
}while(choice!=6);
}
3D Transformation
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void trans();
//void axis();
void scale();
void rotate();
int maxx,maxy,midx,midy;
/*void axis()
// getch();
// cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}*/
void main()
int ch;
int gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\bgi");
scanf("%d",&ch);
do
switch(ch)
{
case 1 : trans();
getch();
// closegraph();
break;
case 2 : scale();
getch();
// closegraph();
break;
case 3 : rotate();
getch();
// closegraph();
break;
case 4 : break;
scanf("%d",&ch);
} while(ch<4);
void trans()
int x,y,z,o,x1,x2,y1,y2;
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
//axis();
bar3d(midx+50,midy-100,midx+60,midy-90,10,1);
scanf("%d%d",&x,&y);
printf("After translation:");
bar3d(midx+x+50,midy-(y+100),midx+x+60,midy-(y+90),10,1);
void scale()
int x,y,z,o,x1,x2,y1,y2;
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
//axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("before translation\n");
printf("After scaling\n");
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);
void rotate()
int x,y,z,o,x1,x2,y1,y2;
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
//axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter rotating angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);
x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
// axis();
// bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
//axis();
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
//axis();
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
}
EXPERIMENT NO.: 8
Cohen Sutherland line clipping algorithm
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#define X_COORDINATE 0
#define Y_COORDINATE 1
#define Z_COORDINATE 2
#define b0 0
#define b1 1
#define b2 2
#define b3 3
printf ("Enter the first point of the the line segment to be clipped (x, y): ");
scanf ("%f %f", &givenLineSegmentP1[X_COORDINATE],
&givenLineSegmentP1[Y_COORDINATE]);
printf ("Enter the second point of the the line segment to be clipped (x, y): ");
scanf ("%f %f", &givenLineSegmentP2[X_COORDINATE],
&givenLineSegmentP2[Y_COORDINATE]);
givenLineSegmentP2[Z_COORDINATE] = givenLineSegmentP1[Z_COORDINATE]
= 0;
p1[X_COORDINATE] = xBoundaryLeft;
p1[Y_COORDINATE] = yBoundaryUpper;
p1[Z_COORDINATE] = 0;
p2[X_COORDINATE] = xBoundaryRight;
p2[Y_COORDINATE] = yBoundaryUpper;
p2[Z_COORDINATE] = 0;
p3[X_COORDINATE] = xBoundaryRight;
p3[Y_COORDINATE] = yBoundaryLower;
p3[Z_COORDINATE] = 0;
p4[X_COORDINATE] = xBoundaryLeft;
p4[Y_COORDINATE] = yBoundaryLower;
p4[Z_COORDINATE] = 0;
/* case 2. One point inside and the other outside the rect. Split into two cases.
*/
/* case 2a: outP1 != 0 && outP2 == 0: First point is outside and second inside.
*/
else if ((outP1[b0] != 0 || outP1[b1] != 0 || outP1[b2] != 0 || outP1[b3] != 0) &&
(outP2[b0] == 0 && outP2[b1] == 0 && outP2[b2] == 0 && outP2[b3] ==
0)) {
if (clippedLineP1[X_COORDINATE] == clippedLineP2[X_COORDINATE]) {
/* then m = (y2-y1)/(x2-x1) would be infinity. */
if (outP1[b0] == 1) {
clippedLineP1[Y_COORDINATE] = yBoundaryUpper;
outP1[b0] = 0;
}
else {
clippedLineP1[Y_COORDINATE] = yBoundaryLower;
outP1[b2] = 0;
}
continue;
}
m = clippedLineP1[Y_COORDINATE]-clippedLineP2[Y_COORDINATE];
m /= clippedLineP1[X_COORDINATE]-clippedLineP2[X_COORDINATE];
if (m == 0) {
if (outP1[b2] == 1) {
clippedLineP1[X_COORDINATE] = xBoundaryLeft;
outP1[b2] = 0;
}
else {
clippedLineP1[X_COORDINATE] = xBoundaryRight;
outP1[b3] = 0;
}
continue;
}
/* Line is neither horizontal nor vertical at this point */
/* We try to clip whichever bound exceeds that we see first. After clipping
at that point, we immediately check if the other (if any) has been clipped
too. */
c = clippedLineP1[Y_COORDINATE]-(m*clippedLineP1[X_COORDINATE]);
if (outP1[b0] == 1) {
clippedLineP1[Y_COORDINATE] = yBoundaryUpper;
clippedLineP1[X_COORDINATE] = (yBoundaryUpper-c)/m;
outP1[b0] = 0;
if (clippedLineP1[X_COORDINATE] <= xBoundaryRight &&
clippedLineP1[X_COORDINATE] >= xBoundaryLeft) {
outP1[b2] = outP1[b3] = 0;
break;
}
continue;
}
if (outP1[b1] == 1) {
clippedLineP1[Y_COORDINATE] = yBoundaryLower;
clippedLineP1[X_COORDINATE] = (yBoundaryLower-c)/m;
outP1[b1] = 0;
if (clippedLineP1[X_COORDINATE] <= xBoundaryRight &&
clippedLineP1[X_COORDINATE] >= xBoundaryLeft) {
outP1[b2] = outP1[b3] = 0;
break;
}
continue;
}
if (outP1[b2] == 1) {
clippedLineP1[X_COORDINATE] = xBoundaryLeft;
clippedLineP1[Y_COORDINATE] = (m*xBoundaryLeft)+c;
outP1[b2] = 0;
if (clippedLineP1[Y_COORDINATE] <= yBoundaryUpper &&
clippedLineP1[Y_COORDINATE] >= yBoundaryLower) {
outP1[b0] = outP1[b1] = 0;
break;
}
continue;
}
if (outP1[b3] == 1) {
clippedLineP1[X_COORDINATE] = xBoundaryRight;
clippedLineP1[Y_COORDINATE] = (m*xBoundaryRight)+c;
outP1[b3] = 0;
if (clippedLineP1[Y_COORDINATE] <= yBoundaryUpper &&
clippedLineP1[Y_COORDINATE] >= yBoundaryLower) {
outP1[b0] = outP1[b1] = 0;
break;
}
continue;
}
}
/* Case2b: If first point is inside and second point is outside. */
else if ((outP2[b0] != 0 || outP2[b1] != 0 || outP2[b2] != 0 || outP2[b3] != 0) &&
(outP1[b0] == 0 && outP1[b1] == 0 && outP1[b2] == 0 && outP1[b3] ==
0)) {
/* Exchange first and second points completely including outP[] and
continue. */
for (i = 0; i < 3; i++) tempPoint[i] = clippedLineP1[i];
for (i = 0; i < 3; i++) clippedLineP1[i] = clippedLineP2[i];
for (i = 0; i < 3; i++) clippedLineP2[i] = tempPoint[i];
for (i = 0; i < 4; i++) outTemp[i] = outP1[i];
for (i = 0; i < 4; i++) outP1[i] = outP2[i];
for (i = 0; i < 4; i++) outP2[i] = outTemp[i];
continue;
}
/* Case 3. if (outP1 & outP2 != 0): Both points exceed the same boundary. */
else if ((outP1[b0] && outP2[b0] == 1) || (outP1[b1] && outP2[b1] == 1) ||
(outP1[b2] && outP2[b2] == 1) || (outP1[b3] && outP2[b3] == 1)) {
/* set clippedLineP1 and clippedLineP2 both to 0,0,0 */
for (i = 0; i < 3; i++) clippedLineP2[i] = clippedLineP1[i] = 0;
break;
}
/* case 4. Both endpoints are outside, but of different edges. */
else {
/* Determine the points of intersection of the line with the four boundaries
and determine the outcode for each of these intersection points. We can
then estimate if the line is entirely out of the boundary or not. */
float intersectionP1[3], intersectionP2[3], intersectionP3[3],
intersectionP4[3];
int validIntersections = 0;
void myInit () {
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho (-50.0, 50.0, -50.0, 50.0, -50.0, 50.0);
glMatrixMode (GL_MODELVIEW);
}
void display () {
/* Clipped line is displayed in red, and unclipped in black. */
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glBegin (GL_LINE_LOOP);
glVertex3fv (p1);
glVertex3fv (p2);
glVertex3fv (p3);
glVertex3fv (p4);
glEnd ();
glBegin (GL_LINES);
glVertex3fv (givenLineSegmentP1);
glVertex3fv (givenLineSegmentP2);
glColor3f (1.0, 0.0, 0.0);
glLineWidth (2);
glVertex3fv (clippedLineP1);
glVertex3fv (clippedLineP2);
glEnd();
glFlush();
}
EXPERIMENT NO.: 9
Sutherland Hodgeman polygon clipping algorithm
#include<GL/glut.h>
#include<GL/glu.h>
#include<GL/gl.h>
#include<stdio.h>
struct Point{
float x,y;
} w[4],oVer[4];
int Nout;
void init(){
glClearColor(0.0f,0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,100.0,0.0,100.0,0.0,100.0);
glClear(GL_COLOR_BUFFER_BIT);
w[0].x =20,w[0].y=10;
w[1].x =20,w[1].y=80;
w[2].x =80,w[2].y=80;
w[3].x =80,w[3].y=10;
}
void display(void){
Point inVer[4];
init();
// As Window for Clipping
glColor3f(1.0f,0.0f,0.0f);
drawPoly(w,4);
// As Rect
glColor3f(0.0f,1.0f,0.0f);
inVer[0].x =10,inVer[0].y=40;
inVer[1].x =10,inVer[1].y=60;
inVer[2].x =60,inVer[2].y=60;
inVer[3].x =60,inVer[3].y=40;
drawPoly(inVer,4);
// As Rect
glColor3f(0.0f,0.0f,1.0f);
clipAndDraw(inVer,4);
// Print
glFlush();
}