2d output primitives graphics packages provide basic operations (called primitive operations) to...

Post on 18-Dec-2015

222 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

2D Output Primitives

Graphics packages provide basic operations (called primitive operations) to describe a scene in terms of geometric structures. The following are some of these primitives:

1. Points: Set a point on using an intensity attributes.

2. Lines: drawing lines

3. Polygons: drawing polygons

Point operations

setpixel(x, y, intensity)This operation turns the pixel at the position (x, y) on and sets its attribute intensity to the intensity value. Assign the point with coordinates (x, y) to a variable used as the current point.

getpixel(x, y)An operation that returns the intensity of the pixel at position (x, y)

Line Operations

Line(x1, y1, x2, y2, intensity)Draws a line between the two points (x1,y1) to (x2, y2). The pixels are set to intensity value equals intensity.

LineTo(x, y, intensity) Draws a line between the two points the current point and (x, y).

Linerel(Δx, Δy) Draws a line between the two points the current point

to a new point (current.X+ Δx, current.y+Δy)

Basic Math ReviewSlope-Intercept Formula For A Line

Given a third point on the line:

P = (X,Y)

Slope = (Y - Y1)/(X - X1)

= (Y2 - Y1)/(X2 - X1)

Solving For Y

Y = [(Y2-Y1)/(X2-X1)]X

+ [-(Y2-Y1)/(X2-X1)]X1 + Y1

or

Y = mx + b

Cartesian Coordinate System

2

4

1 2 3 4 5 6

3

5

6

1 P1 = (X1,Y1)

P2 = (X2,Y2)

P = (X,Y)

SLOPE =

RISE

RUN=

Y2-Y1X2-X1

Other Helpful FormulasLength of line segment between P1 and P2:

L = [ (X2-X1)2 + (Y2-Y1)2 ]1/2

Midpoint of a line segment between P1 and P3:

P2 = ( (X1+X3)/2 , (Y1+Y3)/2 )

Two lines are perpendicular iff

1) M1 = -1/M2

or

2) Cosine of the angle between them is 0.

Line Parametric Equation Given points P1 = (X1, Y1) and P2 = (X2, Y2)

X = X1 + t(X2-X1)

Y = Y1 + t(Y2-Y1)

t is called the parameter. When

t = 0 we get (X1,Y1)

t = 1 we get (X2,Y2)

As 0 < t < 1 we get all the other points on the line segment between (X1,Y1) and (X2,Y2).

Basic Line and Circle Algorithms1. Must compute integer coordinates of pixels which lie on or near a line or

circle.

2. Pixel level algorithms are invoked hundreds or thousands of times when an image is created or modified.

3. Lines must create visually satisfactory images. Lines should appear straight Lines should terminate accurately Lines should have constant density Line density should be independent of line length and angle.

4. Line algorithm should always be defined.

Scan line problem

Intensity changes based on the line slope.

Digital Differential Analyzer (DDA) Simple Line Algorithm

void DDA(int X1, int Y1, int X2, int Y2 ) {

int steps, i;float X,Y,Xinc,Yinc;steps = abs(X2 - X1);If (abs(Y2 - Y1) > steps)

steps = abs(Y2-Y1);Xinc = (X2 - X1)/steps;Yinc = (Y2 - Y1)/steps;X = X1;Y = Y1;

DDA creates good lines but it is too time consuming due to the round function and long operations on real values.

for (i = 0; i <= steps; i++)

{

setpixel(Round(X), Round(Y), intensity);

X = X + Xinc;

Y = Y + Yinc

}

} // end DDA

DDA ExampleCompute which pixels should be turned on to represent the line from (6,9)

to (11,12).

steps = Max of (ABS(11-6), ABS(12-9)) = 5

Xinc = 1

Yinc = 0.6

Values computed are:

(6,9), (7,9.6),

(8,10.2), (9,10.8),

(10,11.4), (11,12)

6 7 8 9 10 11 12 13

9

10

11

12

13

Fast Lines Using The Midpoint MethodAssumptions: Assume we wish to draw a line between points (0,0) and (a,b)

with slope m between 0 and 1 (i.e. line lies in first octant).

The general formula for a line is y = mx + B where m is the slope of the line and B is the y-intercept. From our assumptions m = b/a and B = 0.

y = (b/a)x + 0 --> f(x,y) = bx - ay = 0 is an equation for the line.

+x-x

-y

+y

Fast Lines (cont.)For lines in the first octant, the next

pixel is to the right or to the right and up.

Assume:

Distance between pixels centers = 1

Having turned on pixel P at (xi, yi), the next pixel is

T at (xi+1, yi+1) or S at (xi+1, yi).

Choose the pixel closer to the line f(x, y) = bx - ay = 0.

The midpoint between pixels S and T is (xi + 1,yi + 1/2).

(x +1, y + 1/2 + e)i i

e

(x +1,y + 1/2)i i

P = (x ,y )ii S = (x + 1, y )i i

T = (x + 1, y + 1)ii

Fast Lines (cont.) Let p(x, y) be the point of intersection of

the line and the line between the two points S and T.

Let e be the difference between the y and yi.

If e is positive the line crosses above the midpoint and is closer to T.

If e is negative, the line crosses below the midpoint and is closer to S.

To pick the correct point we only need to know the sign of e.

(x +1, y + 1/2 + e)i i

e

(x +1,y + 1/2)i i

P = (x ,y )ii S = (x + 1, y )i i

T = (x + 1, y + 1)ii

Fast Lines - The Decision Variablef(xi+1,yi+ 1/2 + e) = b(xi+1) - a(yi+ 1/2 + e)

= b(xi + 1) - a(yi + 1/2) – ae

= f(xi + 1, yi + 1/2) - ae = 0

Let di = f(xi + 1, yi + 1/2) = ae;

di is known as the decision variable.

Since ae = di has the same sign as e.

Fast Lines – Updating di

Algorithm:

If di>0Then

Choose T = (xi + 1, yi + 1) as next point

di+1 = f(xi+1 + 1, yi+1 + 1/2) = f(xi +1+1,yi +1+1/2)

= b(xi +1+1) - a(yi +1+1/2) = f(xi + 1, yi + 1/2) + b - a

= di + b - aElse

Choose S = (xi + 1, yi) as next point

di+1 = f(xi+1 + 1, yi+1 + 1/2) = f(xi +1+1,yi +1/2)

= b(xi +1+1) - a(yi +1/2) = f(xi + 1, yi + 1/2) + b

= di + b

Fast Line Algorithm

The initial value for the decision variable, d0, may be calculated directly from the formula at point (0,0).

d0 = f(0+1, 0+1/2) = b(1) - a(1/2) = b-a/2 Therefore, the algorithm for a line from (0,0) to (a,b)

in the first octant is as follows:

Fast Line Algorithm

x = y =0; d = b - a/2; for( i = 0; i < a; i++) { setpixel(x,y); If (d >0) { x ++; y ++; d += b – a} else { x ++; d += b } } Note that the only non-integer value is a/2. If we then multiply by 2 to

get d' = 2d, we can do all integer arithmetic using only the operations +, -, and left shift. The algorithm still works since we only care about the sign, not the value of d.

Bresenham’s Line Algorithm We can also generalize the algorithm to work for lines beginning at

points other than (0,0) by giving x and y the proper initial values. This results in Bresenham's Line Algorithm.

void line(int xstart, int xend, int ystart, int yend) {dx = ABS(xend - xstart);dy = ABS(yend - ystart);d = 2*dy - dx;Incr1 = 2*(dy-dx);Incr2 = 2*dy;If (xstart > xend)

swap end pointx = xstart;y = ystart

setpixel(x,y);

while (x <xend) {

x ++;

If (d >0) { y++;

d += incr1;}

else

d += incr2;

setpixel(x, y);}

}

Optimizations

Speed can be increased even more by detecting cycles in the decision variable. These cycles correspond to a repeated pattern of pixel choices.

The pattern is saved and if a cycle is detected it is repeated without recalculating.

di= 2 -6 6 -2 10 2 -6 6 -2 10

11 12 13 14 15 16 17

9

10

11

12

13

14

15

16

6 7 8 9 10

Simple Circle AlgorithmsSince the equation for a circle on radius r

centered at (0,0) is

x2 + y2 = r2,

an obvious choice is to plot

y = ±¦(r2 - x2)

as -r ≤ x ≤ r.

Brute Force Circle AlgorithmCircle(int r)int x,y; for (x=-r; x<=r ; x++)

{y = SQRT(r*r - x*x);SetPixel(round(x), round(y));

} This works, but is inefficient because of the

multiplications and square root operations. It also creates large gaps in the circle for values of x

close to R (and clumping يتجمع for x near 0).

A better approachA better approach to avoid the gaps is to plot (X,Y) using

x = r cos θ y = r sin θ

as θ takes on values between 0 and 360 degrees. Is it an efficient way? Can we improve it?

Circle Drawing AlgorithmWe only need to calculate the values on the border of the circle in the first

octant. The other values may be determined by symmetry. Assume a circle of radius r with center at (0,0).

void Circle_Points( int x, int y)

{

setpixel(x,y);

setpixel(y,x);

setpixel(y,-x);

setpixel(x,-y);

setpixel(-x,-y);

setpixel(-y,-x);

setpixel(-y,x);

setpixel(-x,y)

}

(a,b)

(b,a)

(a,-b)

(b,-a)

(-a,-b)

(-a,b)

(-b,-a)

(-b,a)

Fast CirclesConsider only the first octant of a circle of radius r centered on the origin. We

begin by plotting point (r,0) and end when x = y.

The decision at each step is whether to choose the pixel directly above the current pixel or the pixel which is above and to the left.

Assume Pi = (xi, yi) is the current pixel.

Ti = (xi, yi +1) is the pixel directly above

Si = (xi -1, yi +1) is the pixel above and to the left.

x=yx + y - r = 022 2

Fast Circles If (x, y) is a point on the circle then f(x, y) = x2 + y2 - r2 = 0

Let e be the distance between the circle and the midpoint(xi-1/2, yi+1).

After P is plotted, then either T or S will be the next point to be plotted. This depends on e.

T = (xi, yi +1)

S = (xi-1, yi+1)

e

(xi -1/2, yi + 1)

p

Fast Circles

Since (xi - 1/2 + e, yi + 1) is on the circle, then

f(xi - 1/2 + e, yi + 1) = 0

= (xi - 1/2 + e)2 + (yi + 1)2 - r2

= (xi- 1/2)2 + (yi+1)2 - r2 + 2(xi-1/2)e + e2

= f(xi - 1/2, yi + 1) + 2(xi - 1/2)e + e2 = 0 Let di = f(xi - 1/2, yi+1) = -2(xi - 1/2)e - e2

Now di = -2(xi - 1/2)e - e2 = -2e xi + e – e2. Notice that1. |e| < 0.5,

2. If e > 0 then di < 0

3. If e < 0 then di > 0 Depending on e (or equivalently on di but the opposite ) if e > 0,

choose T else choose S.

Fast Circles (cont.) In both cases the next d, namely di+1, has to be computed. According

to the above formula di = f(xi - 1/2, yi+1) and depending on the selected point di+1is computed as follows:

If di > 0 (e < 0 ), then S = (xi - 1, yi + 1) is selected and

di+1 = f(xi -1-1/2, yi + 1 + 1) = ((xi - 1/2) - 1)2 + ((yi + 1) + 1)2 - r2

= (xi -1/2)2 + (- 1)2 – 2(xi -1/2) + (yi + 1)2 + 12 + 2((yi + 1) - r2

= di - 2(xi -1) + 2(yi + 1) + 1 = di + 2(yi+1- xi+1) + 1

If di < 0 (e > 0) then T = (xi, yi + 1) is selected and

di+1 = f(xi - 1/2, yi + 1 + 1) = (xi - 1/2)2 + ((yi + 1) + 1)2 - r2

= (xi - 1/2)2 + (yi + 1)2 + 2yi+1 + 1 – r2

= di + 2yi+1 + 1

Notice: di+1= di + 1 + either 2(yi+1- xi+1) or 2yi+1. What is d0.

Fast Circles (cont.) The initial value of di is computed using the formula:

di = f(xi - 1/2, yi+1)

d0 = f(r - 1/2, 0 + 1) = (r - 1/2)2 + 12 - r2

= 5/4 - r {1-r can be used if r is an integer}

When point S = (xi - 1, yi + 1) is chosen then

di+1 = di + -2xi+1 + 2yi+1 + 1

When point T = ( xi , yi + 1) is chosen then

di+1 = di + 2yi+1 + 1

Fast Circle Algorithm// Start with x = r & y =0; Increment y and compute the

// corresponding x value.x = r;y = 0;d = 1 - r;While (y<=x) {

Circle_Points(x,y); y = y + 1;If (d < 0) {

d = d + 2*y + 1;}

Else {x = x - 1;d = d + 2*(y-x) + 1

}}

EllipsesThe circle algorithm can be generalized to work for an ellipse but only four way

symmetry can be used.

All the points in one quadrant must be computed. Since Bresenham's algorithm is restricted to only one octant, the computation must occur in two stages.

The changeover occurs when the point on the ellipse is reached where the tangent line has a slope of ±1. Thus we need two decision variables.

(X,0)(-X,0)

(0,Y)

(0,-Y)

(X/¦2, Y/¦2)

(X/¦2, -Y/¦2)(-X/¦2, -Y/¦2)

(-X/¦2, Y/¦2)

Ellipses Cont.

The equation of the ellipse with center (0,0) and major axes rx& ry is (x/rx)2 + (y/ry)2 = 1. If a point (x, y) is on the ellipse it satisfies the equation

f(x, y) = x2ry2 + y2rx

2 - rx2 ry

2.

The sampling in the first quadrant is divided into two octants: the first starts with x = 0 & y = ry; it ends at the point U with slope equals -1; the second starts with the point U and ends at x = rx and y = 0.

At U dy/dx = -1 * 2ry2 x/ 2rx

2y = -1. Therefore we move from the first octant to the second when ry

2 x ≥ rx2y.

Ellipses Cont.

In the first octant, suppose the point (xi, yi) is plotted, then the next point to be plotted is (xi+1, yi+1) and the candidate pointes are T(xi+1, yi) or S (xi+1, yi-1) depending on which is closer to the ellipse.

Evaluating the decision parameter at the midpoint d1 i = f(xi+1, yi-1/2) to determine which of the two points to use T or S. If d1i < 0, then the midpoint is inside thus T is selected

If d1i > 0, then the midpoint is outside thus S is selected.

Ellipses Cont.

Now evaluating the decision parameter at the new selected point:

d1i+1= f(xi+1+1, yi+1-1/2)

= [xi+1+1]2ry2 + [yi+1-1/2]2rx

2 - rx2 ry

2

It depends on the selected point T or S. If T is selected, then d1i+1 = f(xi+1+1, yi-1/2)

= d1i + 2[x+1]ry2+ry

2

If S is selected, then d1i+1 = f(xi+1+1, yi-1 - ½)

= d1i + 2[xi+1]ry2+ry

2 – 2rx2yi+2rx

2

d0 = f(1, ry-1/2) = ry2 – rx

2ry+ ¼ rx2

The algorithm

Ellipse (int rx, int ry) {Int rx2 = rx*rx; ry2= ry*ry; tworx2 = 2*rx2; twory2 =2*ry2;Int x = 0; y = ry; left = x*twory2; right = y*tworx2;Plot_points(x, y);d = round(ry2 – ry*rx2 + ¼ rx2);While left < right

{ x++; left = + twory2; if d < 0

d = + ry2+left; else {

y--; right = - tworx2; d = + ry2 + left – right;

} plot_points(x, y);

}

Polygons

A polygon is a sequence of lines connected one to the other and the last lines connected with the first. Therefore to drew a polygon we drew the constituent line segments and connect the end of the last segment with the beginning of the first one.

Polygon primitive operation requires a list of points of size equal number of polygon edges. Polygon-abs(int n, array points[n])draws absolute lines. Polygon-relative(int n, array Δpoints[n]) Polygon-fill

An Example

This polygonhas vertices:(0,1)(2,8)(4,6)(7,8)(9,4)(6,1)

and 6 edgese1, .. e60 1 4 5 6 7 8 92 3 10

2

3

0

1

4

5

6

7

8

9

e1

e2 e3

e4

e5

e6

top related