RB1801B31 | 10809529
SUBMITTE
D BY:
VIKRAM COMPUTER GRAPHICS
KUMAR
Ques1:Assume a vector shift of an object P(12,15) in its first position and
P’(20, 10) in the translated position. Determine the translation vector V and
the translation units Tx and Ty. Further, compute the point P’’ such that the
point P’ is rotated by 45 degrees clockwise w.r.t. the point P.
X’=x+tx
Y’=y+ty
->tx=x’-x
->ty=y’-y
Coordinates of P’’=(21,-7,1)
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
class lines
private:
int length,x1,y1,x2,y2,x,y,dx,dy,wx,wy,w,width;
public:
lines(); //Constructor
void showline();
int sign(int);
};
if(xx<0)
return -1;
if(xx==0)
return 0;
if(xx>0)
return 1;
return 0;
lines::lines()
x=0;y=0;
cout<<"
cout<<"
cin>>x2>>y2;
cout<<"
cin>>width;
void lines::showline()
char *s;
int s1,s2,ic;
float temp;
x=x1;y=1;
w=width;
if(y2-y1)
wx=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(y2-y1));
if(x2-x1)
wy=((w-1)/2)*(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))/abs(x2-x1));
dx=abs(x2-x1);
dy=abs(y2-y1);
s1=sign(x2-x1);
s2=sign(y2-y1);
if(dy>dx)
wy=wx;
if(dy>dx)
temp=dy;
dy=dx;
dx=temp;
ic=1;
else
ic=0;
float d=2*dy-dx;
setcolor(0);
for(int i=1;i<=dx;i++)
for(int j=0;j<wy;j++)
putpixel((x+320),480-(y+240+j),5);
for(j=0;j<wy;j++)
putpixel((x+320),480-(y+240-j),5);
putpixel((x+320),480-(y+240),5);
while(d>=0)
{
if(ic==1)
x+=s1;
else
y+=s2;
d-=2*dx;
if(ic==1)
y+=s2;
else
x+=s1;
d+=2*dy;
}
setcolor(15);
sprintf(s,"A(%d,%d)",x1,y1);
outtextxy(20,20,s);
sprintf(s,"B(%d,%d)",x2,y2);
outtextxy(20,30,s);
getch();
void main()
{
int gd=DETECT,gm,i,j,xx=200,xxx=470;
clrscr();
lines a;
initgraph(&gd,&gm,"..\bgi");
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
for(i=0,j=25;i<14,j>=12;i++,j--)
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
for(i=130;i<=510;i+=10)
for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
if(i==320)
continue;
outtextxy(i,237,"+");
for(i=50;i<=430;i+=10)
if(i==240)
continue;
outtextxy(317,i,"-");
outtextxy(310,230,"O");
outtextxy(530,240,"X");
outtextxy(320,450,"-Y");
outtextxy(100,240,"-X");
outtextxy(320,30,"Y");
a.showline();
closegraph();
And
2) Projective transformations.
Eeach and every point is described by n Cartesian coordinates, point is described by n+1
homogeneous coordinates.
These n+1-dimensional transformation matrices are called non-linear transformation matrices.
As they contain homogeneous coordinates and called sometimes, homogeneous transformation
matrices. However we can say that, this terminology is misleading, because most of the
transformations represented are definitely non-homogeneous, and that’s why it is made non
linear.
| cos t sin t 0 0 |
| -sin t cos t 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
To mirror about the x-axis, multiply by:
| -1 0 0 0 |
| 0 1 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
Midpoint ellipse algorithm is a method for drawing ellipses in computer graphics. This method
is modified from Bresenham’s algorithm. The advantage of this modified method is that only
addition operations are required in the program loops. This leads to simple and fast
implementation in all processors. Let us consider one quarter of an ellipse. The curve is divided
into two regions. In region I, the slope on the curve is greater than –1 while in region II less than
–1.
Where a is the horizontal radius and b is the vertical radius, we can define an function f(x,y) by
which the error due to a prediction coordinate (x,y) can be obtained. The appropriate pixels can
be selected according to the error so that the required ellipse is formed. The error can be
confined within half a pixel.
Set f(x,y) = b2x2 + a2y2 – a2b2
If Pk < 0, select E :
If Pk > 0, select SE :
If E is selected,
∆Pk+1E = b2(2xk + 5)
If SE is selected,
∆Pk+1E = b2(2xk + 5)
Initial values:
x0 = 0, y0 = b, P0 = b2 + ¼a2(1 – 4b)
In region II (dy/dx < –1), all calculations are similar to that in region I except that y is
If Pk > 0, select S :
If Pk < 0, select SE :
If S is selected,
putpixel(xC+x, yC+y)
putpixel(xC+x, yC–y)
putpixel(xC–x, yC+y)
putpixel(xC–x, yC–y)
end PlotEllipse
Set x = 0 and y = b
∆PE = 3b2
∆2PE = 2b2
PlotEllipse(xC, yC, x, y)
if P < 0 then
// Select E
P = P + ∆PE
else
// Select SE
P = P + ∆PSE
decrement y
end if
increment x
PlotEllipse(xC, yC, x, y)
end while
while y > 0
if P > 0 then
// Select S
P = P + ∆PE
else
// Select SE
P = P + ∆PSE
increment x
end if
decrement y
PlotEllipse(xC, yC, x, y)
end while
end DrawEllipse
Circles have the property of being highly symmetrical, which is handy when it comes to drawing
them on a display screen. So, Bresenhams circle algorithm results in a much sharp circle,
compared to midpoint circle algorithm. In mid point, decision parameter depends on the last
decision parameter and the pixels next to them whereas in bresenham decision parameter only
depends on previous decision parameter...
#include<conio.h>
#include<graphics.h>
void main()
scanf("%f", &y1);
scanf("%f", &x2);
scanf("%f", &y2);
dx = abs(x1 - x2);
dy = abs(y1 - y2);
p = 2 * dy - dx;
int i,j,xx=190,xxx=480;
myCircle a;
cleardevice();
rectangle(120,40,320,240);
rectangle(320,40,520,240);
rectangle(120,240,320,440);
rectangle(320,240,520,440);
int x,y,r,d,x1,y1,minor,major,dtheta,ratio,a,b,x2,y2;
public:
myCircle();
void showCircle();
x = x2;
y = y2;
end = x1;
}
else
x = x1;
y = y1;
end = x2;
putpixel(x, y, 10);
x = x + 1;
if(p < 0)
p = p + 2 * dy;
else
y = y + 1;
p = p + 2 * (dy - dx);
putpixel(x, y, 10);
getch();
closegraph();
myCircle::myCircle()
x=0;y=0;
cin>>x>>y;
cout<<""Enter The Radius Of The Circle ":=";
cin>>r;
void myCircle::showCircle()
char *s;
x1=0;y1=r;
d=3-2*r;
while(x1<=y1)
putpixel((x+x1+320),(y+y1+240),5);
putpixel((x-x1+320),(y+y1+240),5);
putpixel((x+x1+320),(y-y1+240),5);
putpixel((x-x1+320),(y-y1+240),5);
putpixel((x+y1+320),(y+x1+240),5);
putpixel((x-y1+320),(y+x1+240),5);
putpixel((x+y1+320),(y-x1+240),5);
putpixel((x-y1+320),(y-x1+240),5);
if(d<0)
d+=4*x1+6;
else
d+=4*(x1-y1)+10;
y1--;
x1++;
sprintf(s,"Center(%d,%d)",x,y);
printf(s,"Radius=%d",r);
for(i=0,j=27;i<16,j>=14;i++,j
xx+=10;
outtextxy(xx,10,mess[i]);
xxx-=10;
outtextxy(xxx,10,mess[j]);
delay(100);
for(i=130;i<=510;i+=10)for(j=50;j<=430;j+=10)
putpixel(i,j,15);
for(i=130;i<=510;i+=10)
if(i==320)
continue;
outtextxy(i,237,"+");
for(i=50;i<=430;i+=10)
if(i==240)
continue;
outtextxy(317,i,"-");
a.showCircle();
myCircle::myCircle()
x=0;
y=0;
cin>>major>>minor;
cout<<""Enter The Center Of The Ellipse ":=";
cin>>x>>y;
void myCircle::showCircle()
x1=1;
ratio=major/minor;
getaspectratio(&ax,&ay);
ar=1;
while(x1<=major)
y1=minor*sqrt((1-(x1*x1/major*major)));
putpixel((x+x1*ar+320),(y+y1+240),5);
putpixel((x+x1*ar+320),(y-y1+240),5);
putpixel((x-x1*ar+320),(y-y1+240),5);
putpixel((x-x1*ar+320),(y+y1+240),5);
dtheta=1/sqrt(x1*x1+y1*y1);
x1++;
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
circle(300,200,150);
line(300,140,300,240);
ellipse(250,160,90,270,15,20);
ellipse(250,160,270,90,15,20);
ellipse(350,160,90,270,15,20);
ellipse(350,160,270,90,15,20);
arc(300,240,180,360,30);
getch();
closegraph();