18
1 Computer Graphics Week5 – Shapes & Scan Conversion

1 Computer Graphics Week5 – Shapes & Scan Conversion

Embed Size (px)

Citation preview

Page 1: 1 Computer Graphics Week5 – Shapes & Scan Conversion

1

Computer Graphics

Week5 – Shapes & Scan Conversion

Page 2: 1 Computer Graphics Week5 – Shapes & Scan Conversion

Shapes and Scan Converting

• The process by which an idealized shape, such as a line or a circle, is transformed into the correct "on" values for a group of pixels on the computer is called scan conversion and is also referred to as rasterizing.

2

Page 3: 1 Computer Graphics Week5 – Shapes & Scan Conversion

1-Line and linear shapes

• The most popular line-drawing algorithm is the midpoint-line algorithm.

• This algorithm takes the x- and y- coordinates of a line's endpoints as input and then calculates the x, y-coordinate pairs of all the pixels in between.

3

Page 4: 1 Computer Graphics Week5 – Shapes & Scan Conversion

• Basic linear shapes such as triangles and polygons can be defined by a series of lines.

• A polygon is defined by n number of vertices connected by lines, where n is the number of sides in the polygon.

• A quadrilateral, which is a special case of a polygon is defined by four vertices, and a triangle is a polygon with three vertices

4

Page 5: 1 Computer Graphics Week5 – Shapes & Scan Conversion

5

Relative drawing

• void moveTo(GlPoint point){currPoint = point;}

• void lineTo(GlPoint point) {glBegin(GL_LINES);

glVertex2f(currPoint.x,currPoint.y);glVertex2f(point.x, point.y); //draw the line

glEnd();currPoint= point; //update current point to new pointglFlush();

}

Page 6: 1 Computer Graphics Week5 – Shapes & Scan Conversion

6

Page 7: 1 Computer Graphics Week5 – Shapes & Scan Conversion

• OpenGL geometric primitive types

7

Page 8: 1 Computer Graphics Week5 – Shapes & Scan Conversion

2-Circle and hyperbolic shapes

• The basic mechanics for these algorithms are the same as for lines: figure out the pixels along the path of the shape, and turn the appropriate pixels on.

• Interestingly, we can also draw a curved segment by approximating its shape using line segments. The smaller the segments, the closer the approximation.

8

Page 9: 1 Computer Graphics Week5 – Shapes & Scan Conversion

• For example, consider a circle. Recall from trigonometry that any point on a circle of radius r (and centered at the origin) has an x, y-coordinate pair that can be represented as a function of the angle theta the point makes with the axes

9

Page 10: 1 Computer Graphics Week5 – Shapes & Scan Conversion

10

Points along a circle

Page 11: 1 Computer Graphics Week5 – Shapes & Scan Conversion

11

Page 12: 1 Computer Graphics Week5 – Shapes & Scan Conversion

12

2D-Graphical objects

• PolyLines- input from -file-interactive (using mouse)-computed (using relative drawing)

• Curves-implicit form-parametric form

Page 13: 1 Computer Graphics Week5 – Shapes & Scan Conversion

13

Drawing circle

void drawCircle (Point2 center, float radius){ const int numVerts = 50;

// use larger value for a better circle ngon(numVerts, center.getX(), center.getY(),

radius, 0); }

Page 14: 1 Computer Graphics Week5 – Shapes & Scan Conversion

14

Drawing Arcsvoid drawArc(GlPoint center, float radius, float startAngle, float sweep){ // startAngle and sweep are in degrees const int n = 30; // number of intermediate segments in arc float angle = startAngle * 3.14159265 / 180; // initial angle in radians

float angleInc = sweep * 3.14159265 /(180 * n); // angle incrementGlPoint point;point.x=center.x+ radius * cos(angle);point.y=center.y+ radius * sin(angle);moveTo(point);

for(int k = 1; k < n; k++, angle += angleInc){point.x=center.x+ radius * cos(angle);point.y=center.y+ radius * sin(angle);lineTo(point);}

}

Page 15: 1 Computer Graphics Week5 – Shapes & Scan Conversion

15

Curves

• Implicit formF(x,y) = 0

• Straight lineF(x,y)=(y-A2)(B1 –A1)-(x-A1)(B2 –A2)

• Circle F(x,y)= x2 +y2 – R2

• Inside – outside functionF(x,y) < 0 inside the curve F(x,y) > 0 outside the curve

Page 16: 1 Computer Graphics Week5 – Shapes & Scan Conversion

16

Curves

• Parametric form –CurrentPoint = (x(t), y(t)) t Î T

• Straight linex(t) = A1 +t(B1 –A1) y(t) = A2 +t(B2 –A2)

• Ellipsex(t) = A.cos(t) // A is width of ellipse y(t) = B.sin(t) // B is heigth of ellipse

Page 17: 1 Computer Graphics Week5 – Shapes & Scan Conversion

17

Drawing ellipsevoid drawEllipse(GlPoint center,float A,float B){float cx=center.x;float cy=center.y;GlPoint point;point.x=cx+A;point.y=cy;moveTo(point);

for (float t=0.0; t<=TWOPI+0.01; t+=TWOPI/100){

point.x=cx+A*cos(t);point.y=cy+B*sin(t);lineTo(point);

}glFlush();

}

Page 18: 1 Computer Graphics Week5 – Shapes & Scan Conversion

18

Other conic sections

ParabolaF(x,y)= y2-4axx(t)=at2y(t)=2at

HyperbolaF(x,y)= (x/a)2- (y/b)2-1x(t)=a/cos(t) y(t)=b.sin(t)/cos(t)