Anda di halaman 1dari 28

2/24/2011

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.

Answer1:We are Given the following things


A vector shift P(12,15) of an object
A vector shift P’(20, 10) in the translated position
Now, we have to determine translation vector:

X’=x+tx
Y’=y+ty
->tx=x’-x
->ty=y’-y

Applying valuesvalues into the Tx and Ty


tx=20-12=8
ty=15-10=5

MATRIX ANGLE is 45 because the point P’ is rotated by 45degrees clockwise w.r.t P

The Transformation matrix is given by:

|X’ cos45 sin45 0 20 |


|Y’ -sin45 cos45 0 * 10 |
|1 = 0 0 1 1 |

Computing the values for X’ and Y’-


X’=cos45*20+sin45*10=21
Y’=-sin45*20+cos45*10=-7
1=1

Coordinates of P’’=(21,-7,1)

QUES2:Write a program of bresenham’s line drawing algorithm. Also write


down why bresenham’s is better than DDA algorithm?

Answer2: Program for bresenham’s line drawing algorithm:

#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);

};

int lines::sign(int xx)

if(xx<0)
return -1;

if(xx==0)

return 0;

if(xx>0)

return 1;

return 0;

lines::lines()

x=0;y=0;

cout<<"

"Enter The Co-Ordinates (x1,y1) ":=";


cin>>x1>>y1;

cout<<"

"Enter The Co-Ordinates (x2,y2) ":=";

cin>>x2>>y2;

cout<<"

"Enter The Width Of The Line ":=";

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);

outtextxy(20,10,"The Points Are:=");

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;

char *mess[]={"B","R","E","S","E","N","H","A","M","'","S"," ","L","I","N","E","


","A","L","G","O","R","I","T","H","M"};

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();

Bresenham Better Than DDA:


DDA uses float numbers and uses operators such as division and multiplication in its calculation.
Bresenhams algorithm uses ints and only uses addition and subtraction. Due to the use of only addition
subtraction and bit shifting (multiplication and division use more resources and processor power)
bresenhams algorithm is faster than DDA in producing the line. Im not sure, though if i remember
right, they still produce the same line in the end.
One note concerning efficiency: Fixed point DDA algorithms are generally superior to Bresenhams
algorithm on modern computers. The reason is that Bresenhams algorithm uses a conditional branch in
the loop, and this results in frequent branch mispredictions in the CPU. Fixed point DDA also has
fewer instructions in the loop body (one bit shift, one increment and one addition to be exact. In
addition to the loop instructions and the actual plotting). As CPU pipelines become deeper,
mispredictions penalties will become more Severe.

QUES3: Can the composite transformations like Rotation w.r.t. fixed


point, scaling w.r.t. fixed point etc. can be performed by way of
matrices without using Homogeneous coordinates? Support your
answer (in favour or against) with a example .
Answer3: No, I do not agree the composite transformations like Rotation w.r.t. fixed point,
scaling w.r.t. fixed point etc. can be performed by way of matrices without using Homogeneous
coordinates.

Matrices represent the linear transformations.. non linear transformations on a n-dimensional


space Rn, are represented as linear transformations on a n+1-dimensional real projective space
RPn+1. 
2 types of transformations are included in it and are: 1)Non-linear transformations

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.

Here are some examples:


To rotate about the z axis by angle t, multiply by:

| 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 |

To translate by 3 in x, 4 in y and 5 in z, multiply by:


| 1 0 0 0 |
| 0 1 0 0 |
| 0 0 1 0 |
| 3 4 5 1 |

QUES4:Explain mid-point ellipse drawing algorithm (derivation). Write


a program for drawing ellipse by mid-point method.
Answer4:

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.

Consider the general equation of an ellipse,

b2x2 + a2y2 – a2b2 = 0

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

In region I (dy/dx > –1),

x is always incremented in each step, i.e. xk+1 = xk + 1. yk+1 = yk if E is selected, or yk+1 = yk –


1 if SE is selected. In order to make decision between S and SE, a prediction (xk+1, yk–½) is set
at the middle between the two candidate pixels. A prediction function Pk can be defined as
follows:

Pk= f(xk+1, yk–½)

= b2(xk+1)2 + a2(yk–½)2 – a2b2

= b2(xk+1)2 + a2(yk–½)2 – a2b2

b2(xk2 + 2xk + 1) + a2(yk2 – yk + ¼) – a2b2

If Pk < 0, select E :

Pk+1E = f(xk+2, yk–½)

= b2(xk+2)2 + a2(yk–½)2 – a2b2

= b2(xk2 + 4xk + 4) + a2(yk2 – yk + ¼) – a2b2

Change of PkE is: ∆PkE = Pk+1E – Pk = b2(2xk + 3)

If Pk > 0, select SE :

Pk+1SE= f(xk+2, yk–3/2)

= b2(xk+2)2 + a2(yk–3/2)2 – a2b2

= b2(xk2 + 4xk + 4) + a2(yk2 – 3yk + 9/4) – a2b2

Change of PkSE is ∆PkSE = Pk+1SE – Pk = b2(2xk + 3) – 2a2(yk – 1)

Calculate the changes of ∆Pk:

If E is selected,
∆Pk+1E = b2(2xk + 5)

∆2PkE = ∆Pk+1E – ∆PkE = 2b2

∆Pk+1SE = b2(2xk + 5) – 2a2(yk – 1)

∆2PkSE = ∆Pk+1SE – ∆PkSE = 2b2

If SE is selected,

∆Pk+1E = b2(2xk + 5)

∆2PkE = ∆Pk+1E – ∆PkE = 2b2

∆Pk+1SE = b2(2xk + 5) – 2a2(yk – 2)

∆2PkSE = ∆Pk+1SE – ∆PkSE = 2(a2 + b2)

Initial values:

x0 = 0, y0 = b, P0 = b2 + ¼a2(1 – 4b)

∆P0E = 3b2, ∆P0SE = 3b2 – 2a2(b – 1)

In region II (dy/dx < –1), all calculations are similar to that in region I except that y is

decremented in each step.

y is always decremented in each step, i.e. yk+1 = yk – 1.

xk+1 = xk if S is selected, or xk+1 = xk + 1 if SE is selected.

Pk= f(xk+½, yk–1)


= b2(xk+½)2 + a2(yk–1)2 – a2b2

= b2(xk2 + xk + ¼) + a2(yk2 – 2yk + 1) – a2b2

If Pk > 0, select S :

Pk+1S = f(xk+½, yk–2)

= b2(xk+½)2 + a2(yk–2)2 – a2b2

= b2(xk2 + xk + ¼) + a2(yk2 – 4yk + 4) – a2b2

Change of PkS is: ∆PkS = Pk+1S – Pk = a2(3 – 2yk)

If Pk < 0, select SE :

Pk+1SE= f(xk+3/2, yk–2)

= b2(xk+3/2)2 + a2(yk–2)2 – a2b2

= b2(xk2 + 3xk + 9/4) + a2(yk2 – 4yk + 4) – a2b2

Change of PkSE is ∆PkSE = Pk+1SE – Pk = 2b2(xk + 1) + a2(3 – 2yk)

Calculate the changes of ∆Pk:

If S is selected,

∆Pk+1S = a2(5 – 2yk)

∆2PkS = ∆Pk+1S – ∆PkS = 2a2

∆Pk+1SE = 2b2(xk + 1) + a2(5 – 2yk)

∆2PkSE = ∆Pk+1SE – ∆PkSE = 2a2


If SE is selected,

∆Pk+1S = a2(5 – 2yk)


∆2PkS = ∆Pk+1S – ∆PkS = 2a2

∆Pk+1SE = 2b2(2xk + 2) – a2(5-yk)

Implementation of the algorithm:


The algorithm described above shows how to obtain the pixel coordinates in the first quarter
only. The ellipse centre is assumed to be at the origin. In actual implementation, the pixel
coordinates in other quarters can be simply obtained by use of the symmetric characteristics of
an ellipse. For a pixel (x, y) in the first quarter, the corresponding pixels in other three quarters
are (x, –y), (–x, y) and (–x, –y) respectively. If the centre is at (xC, yC), all calculated coordinate
(x, y) should be adjusted by adding the offset (xC, yC). For easy implementation, a function
PlotEllipse() is defined as follows:

PlotEllipse (xC, yC, x, y)

putpixel(xC+x, yC+y)

putpixel(xC+x, yC–y)

putpixel(xC–x, yC+y)

putpixel(xC–x, yC–y)

end PlotEllipse

The function to draw an ellipse is described in the following pseudo-codes:

DrawEllipse (xC, yC, a, b)

Declare integers x, y, P, ∆PE, ∆PS, ∆PSE, ∆2PE, ∆2PS and ∆2PSE

// Set initial values in region I

Set x = 0 and y = b

P = b2 + (a2(1 – 4b) – 2)/4

// Intentionally –2 to round off the value

∆PE = 3b2

∆2PE = 2b2

∆PSE = ∆PE – 2a2(b – 1)

∆2PSE = ∆2PE + 2a2

// Plot the pixels in region I

PlotEllipse(xC, yC, x, y)

while ∆PSE < 2a2 + 3b2

if P < 0 then
// Select E

P = P + ∆PE

∆PE = ∆PE + ∆2PE

∆PSE = ∆PSE + ∆2PE

else

// Select SE

P = P + ∆PSE

∆PE = ∆PE + ∆2PE

∆PSE = ∆PSE + ∆2PSE

decrement y

end if

increment x

PlotEllipse(xC, yC, x, y)

end while

// Set initial values in region II

P = P – (a2(4y – 3) + b2(4x + 3) + 2) / 4 // Intentionally +2 to round off the value

∆PS = a2(3 – 2y)

∆PSE = 2b2 + 3a2


∆2PS = 2a2 // Plot the pixels in region II

while y > 0

if P > 0 then

// Select S

P = P + ∆PE

∆PE = ∆PE + ∆2PS

∆PSE = ∆PSE + ∆2PS

else

// Select SE
P = P + ∆PSE

∆PE = ∆PE + ∆2PS

∆PSE = ∆PSE + ∆2PSE

increment x

end if

decrement y

PlotEllipse(xC, yC, x, y)

end while

end DrawEllipse

QUES5: Differentiate between mid-point circle and bresenham’s circle?


Answer 5:

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...

QUES6: Write a program to draw a clown by using line drawing, circle


drawing, ellipse drawing?
Answer 6:
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

int gd = DETECT, gm;

int dx, dy, p, end;

float x1, x2, y1, y2, x, y;

initgraph(&gd, &gm, "c:\tc\bgi");

printf("Enter Value of X1: ");


scanf("%f", &x1);

printf("Enter Value of Y1: ");

scanf("%f", &y1);

printf("Enter Value of X2: ");

scanf("%f", &x2);

printf("Enter Value of Y2: ");

scanf("%f", &y2);

dx = abs(x1 - x2);

dy = abs(y1 - y2);

p = 2 * dy - dx;

int i,j,xx=190,xxx=480;

myCircle a;

char *mess[]={"B","R","E","S","E","N","H","A","M","'","S"," ","C","I","R","C","L","E","


","A","L","G","O","R","I","T","H","M"};

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();

if(x1 > x2)

x = x2;

y = y2;

end = x1;

}
else

x = x1;

y = y1;

end = x2;

putpixel(x, y, 10);

while(x < end)

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;

cout<<""Enter The Co-Ordinates Of The Circle ":=";

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;

cout<<""Enter The Major & Minor Axis Of Ellipse ":=";

cin>>major>>minor;
cout<<""Enter The Center Of The Ellipse ":=";

cin>>x>>y;

void myCircle::showCircle()

char *s; int ax,ay; float ar;

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();

Anda mungkin juga menyukai