cg mc0072 1

10
 Student ID: 581116963 Name: Suresh Kumar.S Master of Computer Application (MCA)   Semester 2 (May  2011) Subject: MC0067  Database Management System (DBMS and Oracle 9i) MC0072  Computer Graphics 1)Explain how to modify the line clipping algorithm to clip polygons. To clip a line, we need to consider only its endpoints, not its infinitely many interior points. If both endpoints of a line lie inside the clip rectangle (eg AB, refer to the first example ), the entire line lies inside the clip rectangle and can be trivially accepted. If one endpoint lies inside and one outside(eg CD), the line intersects the clip rectangle and we must compute the intersection point. If both endpoints are outside the clip rectaangle, the line may or may not intersect with the clip rectangle (EF, GH, and IJ), and we need to perform further calculations to determine whether there are any intersections. The brute-force approach to clipping a line that cannot be trivially accepted is to intersect that line with each of the four clip-rectangle edges to see whether any intersection points lie on those edges; if so, the line cuts the clip rectangle and is partially inside. For each line and clip- rectangle edge, we therefore take the two mathematically infinite lines that contain them and intersect them. Next, we test whether this intersection point is "interior" -- that is, whether it lies within both the clip rectangle edge and the line; if so, there is an intersection with the clip rectangle. In the first example, intersection points G' and H' are interior, but I' and J' are not. Steps for Cohen-Sutherland algorithm a)End-points pairs are check for trivial acceptance or trivial rejected using the outcode.  b)If not trivial-accepance or trivial-rejected, divided into two segments at a clip edge. c)Iteratively clipped by testing trivial-acceptance or trivial-rejected, and divided into two segments until completely inside or trivial-rejected. To perform trivial accept and reject tests, we extend the edges of the clip rectangle to divide the plane of the clip rectangle into nine regions. Each region is assigned a 4-bit code deteermined by where the region lies with respect to the outside halfplanes of the clip-rectangle edges. Each bit in the outcode is set to either 1 (true) or 0 (false); the 4 bits in the code correspond to the following conditions: Bit 1 : outside halfplane of top edge, above top edge Y > Ymax Bit 2 : outside halfplane of bottom edge, below bottom edge Y < Ymin Bit 3 : outside halfplane of right edge, to the right of right edge X > Xmax Bit 4 : outside halfplane of left edge, to the left of left edge X < Xmin

Upload: suresh-kumar

Post on 06-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: cg MC0072 1

8/2/2019 cg MC0072 1

http://slidepdf.com/reader/full/cg-mc0072-1 1/10

 Student ID: 581116963

Name: Suresh Kumar.S 

Master of Computer Application (MCA) – Semester 2 (May – 2011)

Subject: MC0067 – Database Management System (DBMS and Oracle 9i)

MC0072 – Computer Graphics

1)Explain how to modify the line clipping algorithm to clip polygons.

To clip a line, we need to consider only its endpoints, not its infinitely many interior points. If

both endpoints of a line lie inside the clip rectangle (eg AB, refer to the first example ), the entireline lies inside the clip rectangle and can be trivially accepted. If one endpoint lies inside andone outside(eg CD), the line intersects the clip rectangle and we must compute the intersectionpoint. If both endpoints are outside the clip rectaangle, the line may or may not intersect with theclip rectangle (EF, GH, and IJ), and we need to perform further calculations to determinewhether there are any intersections.

The brute-force approach to clipping a line that cannot be trivially accepted is to intersect thatline with each of the four clip-rectangle edges to see whether any intersection points lie on thoseedges; if so, the line cuts the clip rectangle and is partially inside. For each line and clip-rectangle edge, we therefore take the two mathematically infinite lines that contain them andintersect them. Next, we test whether this intersection point is "interior" -- that is, whether it lies

within both the clip rectangle edge and the line; if so, there is an intersection with the cliprectangle. In the first example, intersection points G' and H' are interior, but I' and J' are not.

Steps for Cohen-Sutherland algorithm

a)End-points pairs are check for trivial acceptance or trivial rejected using the outcode. 

b)If not trivial-accepance or trivial-rejected, divided into two segments at a clip edge.

c)Iteratively clipped by testing trivial-acceptance or trivial-rejected, and divided into twosegments until completely inside or trivial-rejected.

To perform trivial accept and reject tests, we extend the edges of the clip rectangle to divide theplane of the clip rectangle into nine regions. Each region is assigned a 4-bit code deteerminedby where the region lies with respect to the outside halfplanes of the clip-rectangle edges. Eachbit in the outcode is set to either 1 (true) or 0 (false); the 4 bits in the code correspond to thefollowing conditions:

Bit 1 : outside halfplane of top edge, above top edgeY > Ymax

Bit 2 : outside halfplane of bottom edge, below bottom edgeY < Ymin

Bit 3 : outside halfplane of right edge, to the right of right edgeX > Xmax

Bit 4 : outside halfplane of left edge, to the left of left edgeX < Xmin

Page 2: cg MC0072 1

8/2/2019 cg MC0072 1

http://slidepdf.com/reader/full/cg-mc0072-1 2/10

 Student ID: 581116963

Name: Suresh Kumar.S 

Master of Computer Application (MCA) – Semester 2 (May – 2011)

Subject: MC0067 – Database Management System (DBMS and Oracle 9i)

If the x coordinate boundaries of the clipping rectangle are Xmin and Xmax, and the ycoordinate boundaries are Ymin and Ymax, then the following inequalities must be satisfied fora point at (X,Y) to be inside the clipping rectangle:

Xmin < X < Xmax

and Ymin < Y < Ymax

If any of the four inequalities does not hold, the point is outside the clipping rectangle.

2) What is DDA line drawing algorithm explain it with the suitable example? discuss the meritand demerit of the algorithm

Line DDA Algorithm:  The digital differential analyzer(DDA) is a scan conversion line algorithm based on calculation

either Dy or Dx.  The line at unit intervals is one coordinate and determine corresponding integer values nearest

line for the other coordinate.  Consider first a line with positive slope.

Step : 1

If the slope is less than or equal to 1 ,the unit x intervals Dx=1 and compute each successive yvalues.

Dx=1

m = Dy / Dxm = ( y2-y1 ) / 1m = ( yk+1 – yk ) /1

yk+1 = yk + m

  subscript k takes integer values starting from 1,for the first point and increment by 1 until thefinal end point is reached.

  m->any real numbers between 0 and 1  Calculate y values must be rounded to the nearest integer

Step : 2If the slope is greater than 1 ,the roles of x any y at the unit y intervals Dy=1 and compute eachsuccessive y values.

Dy=1

m= Dy / Dx

Page 3: cg MC0072 1

8/2/2019 cg MC0072 1

http://slidepdf.com/reader/full/cg-mc0072-1 3/10

 Student ID: 581116963

Name: Suresh Kumar.S 

Master of Computer Application (MCA) – Semester 2 (May – 2011)

Subject: MC0067 – Database Management System (DBMS and Oracle 9i)

m= 1/ ( x2-x1 )m = 1 / ( xk+1 – xk )

xk+1 = xk + ( 1 / m )

  Equation 6 and Equation 7 that the lines are to be processed from left end point to the right endpoint.

Step : 3If the processing is reversed, the starting point at the right

Dx=-1

m= Dy / Dxm = ( y2 – y1 ) / -1yk+1 = yk - m

Iintervals Dy=1 and compute each successive y values.

Step : 4Here, Dy=-1

m= Dy / Dxm = -1 / ( x2 – x1 )m = -1 / ( xk+1 – xk )

xk+1 = xk + ( 1 / m )

Equation 6 and Equation 9 used to calculate pixel position along a line with –ve slope.

Advantage and disadvantagesfaster method for calculating pixel position then the equation of a pixel position.Y=mx+b

The accumulation of round of error is successive addition of the floating point increments isused to find the pixel position but it take lot of time to compute the pixel position.

3. Write a short note on:

A) Reflection

B) Sheer

C) Rotation about an arbitrary axis

a)Reflection :

Reflection in computer graphics is used to emulate reflective objects like mirrors and shiny

surfaces.

Page 4: cg MC0072 1

8/2/2019 cg MC0072 1

http://slidepdf.com/reader/full/cg-mc0072-1 4/10

 Student ID: 581116963

Name: Suresh Kumar.S 

Master of Computer Application (MCA) – Semester 2 (May – 2011)

Subject: MC0067 – Database Management System (DBMS and Oracle 9i)

Reflection is accomplished in a ray trace renderer by following a ray from the eye to the mirror

and then calculating where it bounces from, and continuing the process until no surface is

found, or a non-reflective surface is found. Reflection on a shiny surface like wood or tile can

add to the photorealistic effects of a 3D rendering.

  Polished - A Polished Reflection is an undisturbed reflection, like a mirror or chrome.

  Blurry - A Blurry Reflection means that tiny random bumps on the surface of the material

cause the reflection to be blurry.

  Metallic - A reflection is Metallic if the highlights and reflections retain the color of the

reflective object.

  Glossy - This term can be misused. Sometimes it is a setting which is the opposite of Blurry.

(When "Glossiness" has a low value, the reflection is blurry.) However, some people use the

term "Glossy Reflection" as a synonym for "Blurred Reflection." Glossy used in this contextmeans that the reflection is actually blurred.

b)sheer:

 A shear can be thought of as “pulling and elongating” a graphic to the right (positive xvalue) to the left (negative x value), up (positive y value) or down (negative y value).

For x shear, we use the matrixA[1f][01]

+f produces a right shear, f >0-f produces a left shear, f < 0

For y shear, we use the matrixA[10][f1]

+f produces a top shear, f >0-f produces a bottom shear, f < 0

c)Rotation about arbitary axis:

(1) Translate space so that the rotation axis passes through the origin.

(2) Rotate space about the z axis so that the rotation axis lies in the xz plane.

(3) Rotate space about the y axis so that the rotation axis lies along the z axis.

(4) Perform the desired rotation by θ about the z axis.

Page 5: cg MC0072 1

8/2/2019 cg MC0072 1

http://slidepdf.com/reader/full/cg-mc0072-1 5/10

 Student ID: 581116963

Name: Suresh Kumar.S 

Master of Computer Application (MCA) – Semester 2 (May – 2011)

Subject: MC0067 – Database Management System (DBMS and Oracle 9i)

(5) Apply the inverse of step (3).

(6) Apply the inverse of step (2).

(7) Apply the inverse of step (1).

We will write our three-dimensional points in four homogeneous coordinates; i.e., (x,y,z) willbe written as (x,y,z, 1). This enables us to do coordinate transformations using 4x4 matrices.Note that these are really only necessary for translations, if we omitted translations from ourmovements we could do the motions with 3x3 rotation matrices obtained by deleting the lastrows and last columns of the 4x4 matrices. In this article vectors are multiplied by matrices onthe vector’s left. 

2 A translation matrix

The product TP1⋅

v is equivalent to the vector sum⟨

-a,-b,-c, 0⟩

+ v, i.e., this transformationmoves the point P1(a,b,c) to the origin.

3 3D Coordinate axes rotation matrices

Here are the matrices for rotation by α around the x-axis, β around the y-axis, and γ aroundthe z-axis.

Page 6: cg MC0072 1

8/2/2019 cg MC0072 1

http://slidepdf.com/reader/full/cg-mc0072-1 6/10

 Student ID: 581116963

Name: Suresh Kumar.S 

Master of Computer Application (MCA) – Semester 2 (May – 2011)

Subject: MC0067 – Database Management System (DBMS and Oracle 9i)

The general rotation matrix depends on the order of rotations. The first matrix rotatesabout x, then y, then z; the second rotates about z, then y, then x.

Page 7: cg MC0072 1

8/2/2019 cg MC0072 1

http://slidepdf.com/reader/full/cg-mc0072-1 7/10

 Student ID: 581116963

Name: Suresh Kumar.S 

Master of Computer Application (MCA) – Semester 2 (May – 2011)

Subject: MC0067 – Database Management System (DBMS and Oracle 9i)

4. Write a short note on:

a) Replicating pixels:

A quick extention to the scan-conversion inner loop to write multiple pixel at each

computed pixel works reasonably well for lines; here ,pixel are duplicated in columns for

lines with -1 < slope <1 and in rows for all other lines.the effects, however , is that the

lines end are always vertical or horizontal,which is not pleasing for rather thick lines,as

the pixel replication algorithm also produces noticiable gap in the places where line

segments are met at an angle, and misses pixels where there tjere is a shift from

horizontal to verticalreplication as a function of the slope.this latter anamoly shows up as

abnormal thinness in ellipse arcs at the boundaries between octants.the development in

the preceding section is concerned with the following frequency domain sampling of an

aperiodic finite energy sequence.

b) Filling area between boundaries:

The boundary fill algorithm fills a region with the given fill color until the given boundary

color is found. Here i have used the 8-point technique to color a region. The program is

using a recurssive function. The function return when the pixel to be colored is the

boundary color or is already the fill color.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void boundary_fill(int x,int y, int fcolor,int bcolor)

{

if((getpixel(x,y) != fcolor) && (getpixel(x,y) != bcolor))

{

putpixel(x,y,fcolor);

Page 8: cg MC0072 1

8/2/2019 cg MC0072 1

http://slidepdf.com/reader/full/cg-mc0072-1 8/10

 Student ID: 581116963

Name: Suresh Kumar.S 

Master of Computer Application (MCA) – Semester 2 (May – 2011)

Subject: MC0067 – Database Management System (DBMS and Oracle 9i)

boundary_fill(x+1,y,fcolor,bcolor);

boundary_fill(x-1,y,fcolor,bcolor);

boundary_fill(x,y-1,fcolor,bcolor);

boundary_fill(x,y+1,fcolor,bcolor);

boundary_fill(x+1,y-1,fcolor,bcolor);

boundary_fill(x+1,y+1,fcolor,bcolor);

boundary_fill(x-1,y-1,fcolor,bcolor);

boundary_fill(x-1,y+1,fcolor,bcolor);

}

}

void main()

{

int x,y,fcolor,bcolor;

clrscr();

printf("Enter the seed pointn");

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

printf("Enter boundary color : ");

scanf("%d",&bcolor);

printf("Enter new color : ");

scanf("%d",&fcolor);

int gd=DETECT,gm=DETECT;

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

cleardevice();

 /*plase ceate your own shapes

to make a closed area.

*/ 

boundary_fill(x,y,fcolor,bcolor);

Page 9: cg MC0072 1

8/2/2019 cg MC0072 1

http://slidepdf.com/reader/full/cg-mc0072-1 9/10

 Student ID: 581116963

Name: Suresh Kumar.S 

Master of Computer Application (MCA) – Semester 2 (May – 2011)

Subject: MC0067 – Database Management System (DBMS and Oracle 9i)

getch();

}

c) Approximation by thick polyline:

Important Terms Related to Rectangle Filling

· Coherence: A coherence of a scene is a property of a scene by which we can relate one partof a scene with the other parts of a scene

· Primitive’s spatical coherence: It is the fact that primitives often do not change from pixel topixel with in a span or from scan line to scan line.

· Span coherence: It exits when all pixels on a span are set to the same value

· Scan-line coherence: It exists when consecutive scan lines that intersect the rectangle areidentical.

Self Assessment Questions

1. List the important terms related to rectangle filling.

Filling Polygons

Types of polygons

The classification of polygons is based on where the line segment joining any 2 points within thepolygon is going to lie. There are 2 types of polygons

· convex and

· concave

A convex polygon is a polygon in which the line segment joining any 2 points within the polygonlies completely inside the polygon. Fig 4.12 shows the examples of convex polygons.

Polygon Filling

Filling the polygon means highlighting all the pixels which lie inside the polygon with any colorother than background color. Polygons are easier to fill since they have linear boundaries.

There are 2 basic approaches used to fill the polygon. One way to fill a polygon is to start from agiven “seed point” known to be inside the polygon and highlight outward from this point i.eneighboring pixels until we encounter the boundary pixel. This approach is called seed fillbecause color flows from the seed pixel until reaching the polygon boundary. Another approachto fill the polygon is to apply the inside test i.e to check whether pixel is inside the polygon oroutside the polygon and then highlight pixels which lie inside the polygon. This approach isknown as scan line algorithm.

Page 10: cg MC0072 1

8/2/2019 cg MC0072 1

http://slidepdf.com/reader/full/cg-mc0072-1 10/10

 Student ID: 581116963

Name: Suresh Kumar.S 

Master of Computer Application (MCA) – Semester 2 (May – 2011)

Subject: MC0067 – Database Management System (DBMS and Oracle 9i)

4.4.1 Seed fill

This seed fill algorithm is further classified as flood-fill algorithm and boundary fill algorithm.Algorithms that fill interior defines regions are calledflood-fill algorithms, those that fill boundary-defined regions are called boundary-fill algorithms or edge-fill algorithms.

Boundary Fill algorithm

In this method edges of a polygon are drawn. Then starting with some seed (point) , any pointinside the polygon we examine the neighboring pixels to check whether the boundary pixel isreached. If boundary pixels are not reached, pixels are highlighted and the process is continueduntil boundary pixels are reached.

Boundary defined regions may be either 4-connected or 8-connected as shown in the fig 4.14. ifa region is 4-connected, then every pixel in the region may be reached by a combination ofmoves in only 4 directions: left, right, up and down. For an 8-connected region every pixel in theregion may be reached by a combination of moves in the 2 horizontal, 2 vertical and 4 diagonaldirections

Partial filling resulted using 4-connected algorithm

Procedure: boundary_fill(x,y,f_color,b_color)

{

If(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color)

{

Putpixel(x,y,f_color)

Boundaryfill(x+1, y, f_color, b_color)

Boundaryfill(x, y+1, f_color, b_color)

Boundaryfill(x-1, y, f_color, b_color)

Boundaryfill(x, y-1, f_color, b_color)