0% found this document useful (0 votes)
51 views25 pages

Window To View Port

This document describes mapping between window and viewport coordinates in graphics programming. It defines classes for points, lines, and rectangles to represent coordinates. Functions are provided to apply the window-to-viewport transformation, clip polygons to the viewport, check for line intersections, and draw various shapes. The main function demonstrates mapping an polygon by applying the transformation and clipping it to the viewport rectangle.

Uploaded by

Naga Lakshmi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views25 pages

Window To View Port

This document describes mapping between window and viewport coordinates in graphics programming. It defines classes for points, lines, and rectangles to represent coordinates. Functions are provided to apply the window-to-viewport transformation, clip polygons to the viewport, check for line intersections, and draw various shapes. The main function demonstrates mapping an polygon by applying the transformation and clipping it to the viewport rectangle.

Uploaded by

Naga Lakshmi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 25

WINDOW TO VIEW PORT MAPPING

Rate This

/* WINDOW TO VIEW PORT


MAPPING */
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
class PointCoordinates
{
public:
float x;
float y;
PointCoordinates( )
{
x=0;
y=0;
}
};
class LineCoordinates
{
public:

float x_1;
float y_1;
float x_2;
float y_2;
LineCoordinates( )
{
x_1=0;
y_1=0;
x_2=0;
y_2=0;
}
LineCoordinates(const float x1,const float y1,
const float x2,const float y2)
{
x_1=x1;
y_1=y1;
x_2=x2;
y_2=y2;
}
};
class RectangularCoordinates
{
public:

float x_min;
float y_min;
float x_max;
float y_max;
RectangularCoordinates(const float x1,const float y1,
const float x2,const float y2)
{
x_min=x1;
y_min=y1;
x_max=x2;
y_max=y2;
}
};
void show_screen( );
void apply_window_to_viewport_coordinate_transformation(
const RectangularCoordinates,
const RectangularCoordinates,
int &,int &);
void clip_polygon(const RectangularCoordinates,const int,const int []);
const int check_line(const LineCoordinates,const LineCoordinates);
const int check_point(const LineCoordinates,const float,const float);
const PointCoordinates get_intersection_point(LineCoordinates,
LineCoordinates);

void Polygon(const int,const int []);


void Rectangle(const int,const int,const int,const int);
void Line(const int,const int,const int,const int);
void Dashed_line(const int,const int,const int,const int,const int=0);
int main( )
{
int driver=VGA;
int mode=VGAHI;
initgraph(&driver,&mode,..\\Bgi);
show_screen( );
setcolor(15);
Line(90,150,90,390);
Line(50,350,320,350);
Line(400,150,400,390);
Line(360,350,580,350);
RectangularCoordinates WC(130,180,290,300);
RectangularCoordinates VC(440,230,550,300);
setcolor(15);
Line(90,180,95,180);
Line(90,300,95,300);
Line(130,345,130,350);
Line(290,345,290,350);
Line(400,230,405,230);

Line(400,300,405,300);
Line(440,345,440,350);
Line(550,345,550,350);
setcolor(15);
Rectangle(WC.x_min,WC.y_min,WC.x_max,WC.y_max);
Rectangle(VC.x_min,VC.y_min,VC.x_max,VC.y_max);
int n=8;
int polygon_vertices[18]={ 60,320 , 320,320 , 250,190 , 235,210 ,
230,195 , 215,220 , 200,200 , 60,320 };
setcolor(7);
Polygon(n,polygon_vertices);
setcolor(15);
settextstyle(0,0,1);
outtextxy(50,175,YW);
outtextxy(50,295,YW);
outtextxy(110,360,XW);
outtextxy(280,360,XW);
outtextxy(360,225,YV);
outtextxy(360,295,YV);
outtextxy(420,360,XV);
outtextxy(530,360,XV);
outtextxy(182,170,Window);
outtextxy(140,390,World Coordinates);

outtextxy(462,220,Viewport);
outtextxy(420,390,Device Coordinates);
settextstyle(2,0,4);
outtextxy(67,175,min);
outtextxy(67,295,max);
outtextxy(127,360,min);
outtextxy(297,360,max);
outtextxy(377,225,min);
outtextxy(377,295,max);
outtextxy(437,360,min);
outtextxy(547,360,max);
char Key=NULL;
do
{
Key=getch( );
}
while(Key!=T && Key!=t');
settextstyle(0,0,1);
setcolor(0);
outtextxy(90,450, Press T to see the Window-to-Viewport Transformation. );
setcolor(15);
outtextxy(85,450,setcolor(12);

);

outtextxy(213,450, Press any Key to exit. );


for(int count=0;count<n;count++)
apply_window_to_viewport_coordinate_transformation(WC,VC,
polygon_vertices[(count*2)],
polygon_vertices[((count*2)+1)]);
clip_polygon(VC,n,polygon_vertices);
getch( );
return 0;
}
void apply_window_to_viewport_coordinate_transformation(
const RectangularCoordinates wc,
const RectangularCoordinates vc,
int &x,int &y)
{
float Sx=((vc.x_max-vc.x_min)/(wc.x_max-wc.x_min));
float Sy=((vc.y_max-vc.y_min)/(wc.y_max-wc.y_min));
float Tx=vc.x_min;
float Ty=vc.y_min;
x=( Tx+ (Sx*(x-wc.x_min)));
y=(Ty+(Sy*(y-wc.y_min)));
}
void clip_polygon(const RectangularCoordinates wc,const int n,
const int polygon_edges[])

{
int edges_counter;
int number_of_edges=n;
int *edges=new int[(n*4)];
int *clipped_edges=new int[(n*4)];
for(int count_1=0;count_1<(n*2);count_1++)
edges[count_1]=polygon_edges[count_1];
LineCoordinates window_line;
for(int count_2=1;count_2<=4;count_2++)
{
edges_counter=1;
for(int count_3=0;count_3<(number_of_edges*4);count_3++)
clipped_edges[count_3]=0;
if(count_2==1)
{
window_line.x_1=wc.x_min;
window_line.y_1=wc.y_max;
window_line.x_2=wc.x_max;
window_line.y_2=wc.y_max;
}
else if(count_2==2)
{
window_line.x_1=wc.x_max;

window_line.y_1=wc.y_max;
window_line.x_2=wc.x_max;
window_line.y_2=wc.y_min;
}
else if(count_2==3)
{
window_line.x_1=wc.x_max;
window_line.y_1=wc.y_min;
window_line.x_2=wc.x_min;
window_line.y_2=wc.y_min;
}
else if(count_2==4)
{
window_line.x_1=wc.x_min;
window_line.y_1=wc.y_min;
window_line.x_2=wc.x_min;
window_line.y_2=wc.y_max;
}
int rule_no;
PointCoordinates point;
for(int count_4=1;count_4<number_of_edges;count_4++)
{
LineCoordinates line(edges[((count_4*2)-2)],

edges[((count_4*2)-1)],
edges[(count_4*2)],
edges[((count_4*2)+1)]);
rule_no=check_line(window_line,line);
switch(rule_no)
{
case 1 : clipped_edges[(edges_counter*2)]=
(int)(line.x_2+0.5);
clipped_edges[((edges_counter*2)+1)]=
(int)(line.y_2+0.5);
edges_counter++;
break;
case 2 : break;
case 3 : point=
get_intersection_point(window_line,line);
clipped_edges[(edges_counter*2)]=
(int)(point.x+0.5);
clipped_edges[((edges_counter*2)+1)]=
(int)(point.y+0.5);
edges_counter++;
break;
case 4 : point=
get_intersection_point(window_line,line);

clipped_edges[(edges_counter*2)]=
(int)(point.x+0.5);
clipped_edges[((edges_counter*2)+1)]=
(int)(point.y+0.5);
edges_counter++;
clipped_edges[(edges_counter*2)]=
(int)(line.x_2+0.5);
clipped_edges[((edges_counter*2)+1)]=
(int)(line.y_2+0.5);
edges_counter++;
break;
}
}
clipped_edges[0]=clipped_edges[((edges_counter*2)-2)];
clipped_edges[1]=clipped_edges[((edges_counter*2)-1)];
for(int count_5=0;count_5<(edges_counter*2);count_5++)
edges[count_5]=0;
number_of_edges=edges_counter;
for(int count_6=0;count_6<(edges_counter*2);count_6++)
edges[count_6]=clipped_edges[count_6];
}
setcolor(10);
Polygon(number_of_edges,edges);

delete edges;
delete clipped_edges;
}
const int check_line(const LineCoordinates line,const LineCoordinates edge)
{
int rule_number=0;
int point_1=check_point(line,edge.x_1,edge.y_1);
int point_2=check_point(line,edge.x_2,edge.y_2);
if(point_1==1 && point_2==1)
rule_number=1;
else if(point_1!=1 && point_2!=1)
rule_number=2;
else if(point_1==1 && point_2!=1)
rule_number=3;
else if(point_1!=1 && point_2==1)
rule_number=4;
return rule_number;
}
const int check_point(const LineCoordinates lc,const float x,const float y)
{
float c=(((lc.x_2-lc.x_1)*(y-lc.y_1))-((lc.y_2-lc.y_1)*(x-lc.x_1)));
if(c<=0)
return 1;

else
return 0;
}
const PointCoordinates get_intersection_point(LineCoordinates lc1,
LineCoordinates lc2)
{
float x_min=lc1.x_1;
float x_max=lc1.x_2;
float y_min=lc1.y_1;
float y_max=lc1.y_2;
if(lc1.x_1==lc1.x_2)
{
if(lc2.y_2>=lc2.y_1 && lc2.y_2>y_max)
y_max=lc2.y_2;
else if(lc2.y_1>lc2.y_2 && lc2.y_1>y_max)
y_max=lc2.y_1;
if(lc2.y_1<=lc2.y_2 && lc2.y_1<y_min)
y_min=lc2.y_1;
else if(lc2.y_2<lc2.y_1 && lc2.y_2<y_min)
y_min=lc2.y_2;
}
else if(lc1.y_1==lc1.y_2)
{

if(lc2.x_2>=lc2.x_1 && lc2.x_2>x_max)


x_max=lc2.x_2;
else if(lc2.x_2<lc2.x_1 && lc2.x_1>x_max)
x_max=lc2.x_1;
if(lc2.x_1<=lc2.x_2 && lc2.x_1<x_min)
x_min=lc2.x_1;
else if(lc2.x_2<lc2.x_1 && lc2.x_2<x_min)
x_min=lc2.x_2;
}
float x_mid;
float y_mid;
if(lc2.y_1>y_max)
{
while(lc2.y_1!=y_max)
{
x_mid=((lc2.x_1+lc2.x_2)/2);
y_mid=((lc2.y_1+lc2.y_2)/2);
if(y_mid>=y_max)
{
lc2.x_1=x_mid;
lc2.y_1=y_mid;
}
else

{
lc2.x_2=x_mid;
lc2.y_2=y_mid;
}
}
}
else if(lc2.y_1<y_min)
{
while(lc2.y_1!=y_min)
{
x_mid=((lc2.x_1+lc2.x_2)/2);
y_mid=((lc2.y_1+lc2.y_2)/2);
if(y_mid<=y_min)
{
lc2.x_1=x_mid;
lc2.y_1=y_mid;
}
else
{
lc2.x_2=x_mid;
lc2.y_2=y_mid;
}
}

}
if(lc2.x_1>x_max)
{
while(lc2.x_1!=x_max)
{
x_mid=((lc2.x_1+lc2.x_2)/2);
y_mid=((lc2.y_1+lc2.y_2)/2);
if(x_mid>=x_max)
{
lc2.x_1=x_mid;
lc2.y_1=y_mid;
}
else
{
lc2.x_2=x_mid;
lc2.y_2=y_mid;
}
}
}
else if(lc2.x_1<x_min)
{
while(lc2.x_1!=x_min)
{

x_mid=((lc2.x_1+lc2.x_2)/2);
y_mid=((lc2.y_1+lc2.y_2)/2);
if(x_mid<=x_min)
{
lc2.x_1=x_mid;
lc2.y_1=y_mid;
}
else
{
lc2.x_2=x_mid;
lc2.y_2=y_mid;
}
}
}
PointCoordinates ipt;
ipt.x=lc2.x_1;
ipt.y=lc2.y_1;
return ipt;
}
void Rectangle(const int x_1,const int y_1,const int x_2,const int y_2)
{
Dashed_line(x_1,y_1,x_2,y_1,1);
Dashed_line(x_2,y_1,x_2,y_2,1);

Dashed_line(x_2,y_2,x_1,y_2,1);
Dashed_line(x_1,y_2,x_1,y_1,1);
}
void Polygon(const int n,const int coordinates[])
{
if(n>=2)
{
Line(coordinates[0],coordinates[1],
coordinates[2],coordinates[3]);
for(int count=1;count<(n-1);count++)
Line(coordinates[(count*2)],coordinates[((count*2)+1)],
coordinates[((count+1)*2)],
coordinates[(((count+1)*2)+1)]);
}
}
void Line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)

{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else

{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
x++;

p+=two_dx_dy;
}
putpixel(x,y,color);
}
}
}
void Dashed_line(const int x_1,const int y_1,const int x_2,
const int y_2,const int line_type)
{
int count=0;
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);

int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
if((count%2)!=0 && line_type==0)
putpixel(x,y,color);
else if((count%5)!=4 && line_type==1)

putpixel(x,y,color);
else if((count%10)!=8 && (count%10)!=9 && line_type==2)
putpixel(x,y,color);
else if((count%20)!=18 && (count%20)!=19 && line_type==3)
putpixel(x,y,color);
else if((count%12)!=7 && (count%12)!=8 &&
(count%12)!=10 && (count%12)!=11 && line_type==4)
putpixel(x,y,color);
count++;
}
}
else
{
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)

p+=two_dx;
else
{
x++;
p+=two_dx_dy;
}
if((count%2)!=0 && line_type==0)
putpixel(x,y,color);
else if((count%5)!=4 && line_type==1)
putpixel(x,y,color);
else if((count%10)!=8 && (count%10)!=9 && line_type==2)
putpixel(x,y,color);
else if((count%20)!=18 && (count%20)!=19 && line_type==3)
putpixel(x,y,color);
else if((count%12)!=7 && (count%12)!=8 &&
(count%12)!=10 && (count%12)!=11 && line_type==4)
putpixel(x,y,color);
count++;
}
}
}
void show_screen( )
{

setfillstyle(1,1);
bar(180,26,460,38);
settextstyle(0,0,1);
setcolor(15);
setcolor(11);
outtextxy(190,29,Window-to-Viewport Transformation);
setcolor(15);
setcolor(12);
outtextxy(90,450, Press T to see the Window-to-Viewport Transformation. );
}

You might also like