0% found this document useful (0 votes)
104 views20 pages

Graphics Transformations in C

The document contains C code to perform polygon clipping using the Sutherland-Hodgman algorithm. It defines functions to clip lines against the left, right, bottom and top edges of a rectangular clipping window. It takes user input for the polygon vertices and clipping window coordinates, clips the polygon using the defined functions, and displays the original and clipped polygons.

Uploaded by

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

Graphics Transformations in C

The document contains C code to perform polygon clipping using the Sutherland-Hodgman algorithm. It defines functions to clip lines against the left, right, bottom and top edges of a rectangular clipping window. It takes user input for the polygon vertices and clipping window coordinates, clips the polygon using the defined functions, and displays the original and clipped polygons.

Uploaded by

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

#include<stdio.

h>  
#include<graphics.h>  
#include<math.h>  
main()  
{  
    intgd=0,gm,x1,y1,x2,y2,x3,y3;  
    double s,c, angle;  
    initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");  
    setcolor(RED);  
    printf("Enter coordinates of triangle: ");  
    scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2, &x3, &y3);  
    setbkcolor(WHITE);  
    cleardevice();  
    line(x1,y1,x2,y2);  
    line(x2,y2, x3,y3);  
    line(x3, y3, x1, y1);  
    getch();  
    setbkcolor(BLACK);  
    printf("Enter rotation angle: ");  
    scanf("%lf", &angle);  
    setbkcolor(WHITE);  
    c = cos(angle *M_PI/180);  
    s = sin(angle *M_PI/180);  
    x1 = floor(x1 * c + y1 * s);  
    y1 = floor(-x1 * s + y1 * c);  
    x2 = floor(x2 * c + y2 * s);  
    y2 = floor(-x2 * s + y2 * c);  
    x3 = floor(x3 * c + y3 * s);  
    y3 = floor(-x3 * s + y3 * c);  
    cleardevice();  
    line(x1, y1 ,x2, y2);  
    line(x2,y2, x3,y3);  
    line(x3, y3, x1, y1);  
    getch();  
    closegraph();  
    return 0;
}
Output
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>

int x1, y1, x2, y2, x3, y3, mx, my;

void draw();
void tri();

void main() {
int gd = DETECT, gm;
int c;
initgraph(&gd, &gm, "d:\\tc\\bgi ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d", &x1, &y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d", &x2, &y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d", &x3, &y3);

cleardevice();
draw();
getch();
tri();
getch();
}

void draw() {
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}

void tri() {
int x, y, a1, a2, a3, b1, b2, b3;
printf("Enter the Transaction coordinates");
scanf("%d%d", &x, &y);
cleardevice();
a1 = x1 + x;
b1 = y1 + y;
a2 = x2 + x;
b2 = y2 + y;
a3 = x3 + x;
b3 = y3 + y;
line(a1, b1, a2, b2);
line(a2, b2, a3, b3);
line(a3, b3, a1, b1);
}
Output
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>

int x1, y1, x2, y2, x3, y3, mx, my;

void draw();
void scale();

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

initgraph(&gd, &gm, " ");


printf("Enter the 1st point for the triangle:");
scanf("%d%d", &x1, &y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d", &x2, &y2);

printf("Enter the 3rd point for the triangle:");


scanf("%d%d", &x3, &y3);

draw();

scale();
}

void draw() {
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}

void scale() {
int x, y, a1, a2, a3, b1, b2, b3;
int mx, my;

printf("Enter the scalling coordinates");


scanf("%d%d", &x, &y);

mx = (x1 + x2 + x3) / 3;
my = (y1 + y2 + y3) / 3;
cleardevice();

a1 = mx + (x1 - mx) * x;
b1 = my + (y1 - my) * y;
a2 = mx + (x2 - mx) * x;
b2 = my + (y2 - my) * y;
a3 = mx + (x3 - mx) * x;
b3 = my + (y3 - my) * y;

line(a1, b1, a2, b2);


line(a2, b2, a3, b3);
line(a3, b3, a1, b1);

draw();
getch();
}

Output
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
char a;
clrscr();
initgraph(&gd,&gm,"");
printf("\n enter the coordinates of triangle");
sacnf("%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);
line(320,0,320,430);
line(0,240,640,240);
printf("\n enter the axis of reflection");
scanf("%d",&a);
if(a='x'||a='X')
{
x1=x1;
x2=x2;
x3=x3;
y1=y1+240;
y2=y2+240;
y3=y3+240;
}
else
if(a='y'||a='Y')
{
y1=y1;
y2=y2;
y3=y3;
x1+=320;
x2+=320;
x3+=320;
}
printf("\n triangle after reflection");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
closegraph();
}
OUTPUT
OUTPUT
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void bytecode();
void sutherland();
int a[4],b[4];
float m,xnew,ynew;
float xl = 100, yl = 100, xh = 300, yh = 300,xa = 10,ya = 200,xb = 250, yb
= 150;
void main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(5);
line(xa,ya,xb,yb);
setcolor(12);
rectangle(xl,yl,xh,yh);
m = (yb-ya)/(xb-xa);
bytecode();
sutherland();
getch();
}

void bytecode()
{
if(xa < xl)
a[3] = 1;
else a[3] = 0;

if(xa>xh)
a[2] = 1;
else a[2] = 0;

if(ya < yl)


a[1] = 1;
else a[1] = 0;

if (ya > yh)


a[0] = 1;
else a[0] = 0;

if(xb < xl)


b[3] = 1;
else b[3] = 0;

if(xb>xh)
b[2] = 1;
else b[2] = 0;

if(yb < yl)


b[1] = 1;
else b[1] = 0;

if (yb > yh)


b[0] = 1;
else b[0] = 0;
}

void sutherland()
{
printf("press a key to continue");
getch();
if(a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0 && b[0] == 0 &&
b[1] == 0 && b[2] == 0 && b[3] == 0 )
{

printf("no clipping");
line(xa,ya,xb,yb);
}
else if(a[0]&&b[0] || a[1]&&b[1] || a[2]&&b[2] || a[3]&&b[3])
{
clrscr();
printf("line discarded");
rectangle(xl,yl,xh,yh);
}
else
{
if(a[3] == 1 && b[3]==0)
{
ynew = (m * (xl-xa)) + ya;
setcolor(12);
rectangle(xl,yl,xh,yh);
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xl,ynew,xb,yb);
}
else if(a[2] == 1 && b[2] == 0)
{
ynew = (m * (xh-xa)) + ya;
setcolor(12);
rectangle(xl,yl,xh,yh);
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xl,ynew,xb,yb);
}
else if(a[1] == 1 && b[1] == 0)
{
xnew = xa + (yl-ya)/m;
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xnew,yh,xb,yb);
}

else if(a[0] == 1 && b[0] == 0)


{
xnew = xa + (yh-ya)/m;
setcolor(0);
line(xa,ya,xb,yb);
setcolor(15);
line(xnew,yh,xb,yb);
}
}
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;

void clipl(float x1,float y1,float x2,float y2)


{
if(x2-x1)
m=(y2-y1)/(x2-x1);
if(x1>=xmin && x2>=xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1<xmin && x2>=xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1>=xmin && x2<xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}

void clipt(float x1,float y1,float x2,float y2)


{
if(y2-y1)
m=(x2-x1)/(y2-y1);
if(y1<=ymax && y2<=ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1>ymax && y2<=ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1<=ymax && y2>ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}

void clipr(float x1,float y1,float x2,float y2)


{
if(x2-x1)
m=(y2-y1)/(x2-x1);
if(x1<=xmax && x2<=xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1>xmax && x2<=xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1<=xmax && x2>xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}

void clipb(float x1,float y1,float x2,float y2)


{
if(y2-y1)
m=(x2-x1)/(y2-y1);
if(y1>=ymin && y2>=ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1<ymin && y2>=ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1>=ymin && y2<ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}

void main()
{
int gdriver=DETECT,gmode,n,poly[20],i;
float xi,yi,xf,yf,polyy[20];
clrscr();
printf("Coordinates of rectangular clip window :\nxmin,ymin:");
scanf("%f%f",&xmin,&ymin);
printf("Coordinates of rectangular clip window :\nxmax,ymax:");
scanf("%f%f",&xmax,&ymax);
printf("\n\nPolygon to be clipped :\nNumber of sides :");
scanf("%d",&n);
printf("Enter the coordinates :");
for (i=0;i<2*n;i++)
scanf("%f",&polyy[i]);
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i<2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
rectangle(xmin,ymax,xmax,ymin);
printf("\tUNCLIPPED POLYGON");
printf("hi");
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i<2*n;i+=2)
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i<k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
rectangle(xmin,ymax,xmax,ymin);
printf("\tCLIPPED POLYGON");
getch();
closegraph();
}
OUTPUT

#include <stdio.h>
  
// Function for window to viewport transformation
void WindowtoViewport(int x_w, int y_w, int x_wmax,
                      int y_wmax, int x_wmin, int y_wmin,
                      int x_vmax, int y_vmax, int x_vmin,
                      int y_vmin)
{
    // point on viewport
    int x_v, y_v;
  
    // scaling factors for x coordinate and y coordinate
    float sx, sy;
  
    // calculatng Sx and Sy
    sx = (float)(x_vmax - x_vmin) / (x_wmax - x_wmin);
    sy = (float)(y_vmax - y_vmin) / (y_wmax - y_wmin);
  
    // calculating the point on viewport
    x_v = x_vmin + (float)((x_w - x_wmin) * sx);
    y_v = y_vmin + (float)((y_w - y_wmin) * sy);
  
    printf("The point on viewport: (%d, %d )\n ", x_v, y_v);
}
  
// Driver Code
void main()
{
    // boundary values for window
    int x_wmax = 80, y_wmax = 80, x_wmin = 20, y_wmin = 40;
  
    // boundary values for viewport
    int x_vmax = 60, y_vmax = 60, x_vmin = 30, y_vmin = 40;
  
    // point on window
    int x_w = 30, y_w = 80;
  
    WindowtoViewport(30, 80, 80, 80, 20, 40, 60, 60, 30, 40);
}

OUTPUT

You might also like