Professional Documents
Culture Documents
Chapitre 03 - Algo 2D Partie 5.fr - en
Chapitre 03 - Algo 2D Partie 5.fr - en
2D algorithm
Mohamad Saade
2D algorithms
• Pixel and Image
• 2D Drawing of Base Objects
– Drawing segments and curves
– Traceof segmentcircle
– Trace of a curve
• Clippingwindowing (Cutting)
– Clippingrectangular
– Clippingparametric
– 3D windowing
• Filling
3D windowing
• Setting the viewing volume
– windowrectangular belonging to the projection
plane
– projectionparallel :parallelepiped whose edgesare
parallelto the direction of projection
– projectionperspective :infinite pyramid whose
summitis theprojection center
Window volume shapes
• parallelepiped
Window volume shapes
• Pyramidal
Window volumes
• Generally:
– Volumesfinished windows
– Datain the observer frame
Cohen-Sutherland algorithm
1. Affecta 6-bit code at each end of the segment
– bit 1 = 1: the end is to the left of the volume
– bit 2 = 1: the end is to the right of the volume
– bit 3 = 1: the end is below the volume
– bit 4 = 1: the end is above the volume
– bit 5 = 1: the end is in front of the volume
– bit 6 = 1: the end is behind the volume
2. To classifythe segment
– ifcode1=000000and code2=000000 the segment is completely visible
– else if(code1 and code2)≠ 000000 he is invisible
– otherwise the segment is (potentially) partially visible
3. Examinethe remaining segments:
– calculationof the intersection of the segment with the plane of the volume
concerned
– calculateits end code
Calculation of intersections: plane
equations
• For a parallelepiped volume:
equationsplanstrivial
• Forun pyramidal volume:
• projections on the planOXobsZobs
– left plane: x = -w/dz
– right plane: x =w/dz
• projections on the planOZobsYobs
– upper plane: y =h/dz
– lower plane: y = -h/dz
• Z values
– front plane: z =ZBefore
– rear plane: z =Zarrière
Windowing volumes with OpenGL
• Perspective projection
– glFrustum(left,right,bottom,top,zNear,zFar);
– gluPerspective(angleFovy, ratio,zNear,zFar);
• Orthogonal projection
– glOrtho(left,right,bottom,top,zNear,zFar)
2D algorithms
Germ
Seed algorithm
• Recursive algorithm:
• At each iteration:
– We fill all the pixelsPiup to the color limit to the right and left of the
current seed
– We search among the pixels above and below thePithose who
arerightmostof a maximum horizontal sequence to fill
– These pixels are stacked as seeds for subsequent iterations.
Seed algorithm
Seed algorithm
Seed algorithm
Seed algorithm
• In the following example:
– It will be noted in particular in the last figure that the seed 3 is stacked.
We then stacked our scanning line. No other sprouts will be added
because the lines above and below are full.
– We then unstack germ 2 and fill its scanning line which again does not
give any other germ.
– We finally unstack germ 1 whose line is already filled (We could test it)
– The algorithm is then completed, the stack being empty. The maximum
stack size will have been 4, which is very economical compared to the
previous algorithm.
Seed algorithm
Scanline algorithm
• Process of filling an area defined by a set of points
• Line by line, frame by frame scanning
Algorithm ofscanline
• Consists of calculating horizontal segments
included in the polygon
Algorithm ofscanline
• Idea of the algorithm:
• Foreach horizontal line
– Determinedthe pixels havethe interiorof the polygon
– Pixels marked/displayedwith the correct value /color
• Principle of the algorithm:
• Foreach horizontal line
1. DeterminedTHEendshorizontal segments includedin thepolygon
• Ends = intersection between horizontal line withedgesof the polygon
2. Sort themendsin ascending order ofcontact detailsin x
3. Posterthe pixels havethe interiorof the polygon betweenend pairs
– Rulerof theparityto know if pixel hasthe interioror not
Active Edge Table (AET)
• For each horizontal line of a polygon we need to consider that
certain edges
• Keeps a table of active edges =ActiveEdgeTable
– Updating this table each time a row changes
• We extract from this table the intervals to be colored
Initialization:EdgeTable (AND)
• Order of description of an edge
– Be sure thaty1<y2for each edge(x1,y1) (x2,y2)
• Building an edge tableEdgeTable (AND)
– Sorting edges according to their minimum Y
– Edges starting with the same Y are placed in the
same “box”
– Each element contains(Ymax,Xmin,incrementX)
Maintain AET
• Transition to the following horizontal line:
– Remove each edge including2= current line
– Update the value of x for each remaining edge
– Add each edge whose y1 is equal to the current
line
Drawing from AET
• Sort active edges by x
• The pairs of edges give the segments to draw
• Attention
– Maximum vertices are not drawn
– Special case when a polygon shares edges with a
neighboring polygon
• See example.
Example
Example
Example