Midpoint Ellipse Algorithm -...

37
Midpoint Ellipse Algorithm Midpoint Ellipse Algorithm n Use symmetry of ellipse n Divide the quadrant into two regions n the boundary of two regions is the point at which the curve has a slope of -1. n Process by taking unit steps in the x direction to the point P, then taking unit steps in the y direction n Apply midpoint algorithm. r x r y Slope = -1 P

Transcript of Midpoint Ellipse Algorithm -...

Midpoint Ellipse AlgorithmMidpoint Ellipse Algorithm

n Use symmetry of ellipsen Divide the quadrant into two regions

n the boundary of two regions is the point at which thecurve has a slope of -1.

n Process by taking unit steps in the x direction to thepoint P, then taking unit steps in the y direction

n Apply midpoint algorithm.

rx

ry

Slope = -1P

Midpoint Ellipse AlgorithmMidpoint Ellipse Algorithm

rx

ry

fellipse(x,y) = ry2x2 + rx

2y2 – rx2ry

2

< 0 inside the ellipse boundary= 0 on the ellipse boundary> 0 outside the ellipse boundary

122

=÷÷ø

öççè

æ -+÷÷

ø

öççè

æ -

y

c

x

c

ryy

rxx

p1k = fellipse (xk+1, yk – ½)

p2k = fellipse (xk+½, yk – 1)

Maintaining Geometric Maintaining Geometric PropertiesProperties

n When line drawing,n exclude the last point

n When drawing an enclosed area,n display the area using only those pixels that

are interior to the object boundaries

InsideInside--Outside TestsOutside Tests

n For each pixel, determine if it is inside or outside of a given polygon.

n Approachn From any point P being tested, cast a

ray to a distant point in an arbitrary direction

n If the number of crossings is odd, then P is an interior point.

n If the number of crossings is even, then P is an exterior point.

OddOdd--even Rule (Oddeven Rule (Odd--parity Rule)parity Rule)

n Be sure that the line path does not intersect any line-segment endpoints.

OddOdd--Even RuleEven Rule

OddOdd--Even RuleEven Rule

n Edge Crossing Rulesn an upward edge includes its starting endpoint, and

excludes its final endpoint; n a downward edge excludes its starting endpoint, and

include its final endpoint; n horizontal edges are excluded;

P

n Very fragile algorithmn Ray crosses a vertexn Ray is coincident with an edge

n Commonly used in ECAD

n Suitable for H/W

OddOdd--Even RuleEven Rule

n A winding numbern the # of times the boundary of an object “winds”

around a particular point P in the counterclockwise direction

n Non-zero values: interior pointsn Zero values : exterior points

Nonzero Winding Number RuleNonzero Winding Number Rule

InsideInside--Outside TestsOutside Tests

Nonzero Winding Nonzero Winding Number RuleNumber Rule

n Winding num: initialized to 0n From any point P being tested, cast a ray to a distant point in

an arbitrary direction n +1: edge crossing the line from right to leftn -1: left to right

n Use the sign of the cross product of the line and edge vectorsn Non-zero: interiorn Zero: exterior

n Be sure that the line path does not pass through any line-segment endpoints.

How to decide interiorHow to decide interior

Vertices are numbered: 0 1 2 3 4 5 6 7 8 9

Polygon TablesPolygon Tables

n An objectn described as a set of polygon surface facets

n Geometric datan vertex tablen edge table n surface-fact table

n Other parametersn Color, transparency, light-

reflection properties

Area FillingArea Filling

n Which pixels?n What value?

How to generate a solid color/patterned polygon area

ScanScan--line fill algorithmline fill algorithm

ScanScan--Line Fill AlgorithmLine Fill Algorithm

n For each scan line(1) Find intersections (the extrema of spans)

n Use Bresenham's line-scan algorithmn Note that in a line drawing algorithm there is

no difference between interior and exterior pixels

(2) Sort intersections (increasing x order)

(3) Fill in between pair of intersections

n xk+1 = xk + Δx / Δyn example (left edge)

n m = 5/2 n xmin = 3n the sequence of x values

n 3, 3+2/5, 3+4/5, 3+5/6=4+1/5

ScanScan--Line fill algorithmLine fill algorithm

y

x

pixel

1 2 3 4

3 3+2/5 3+4/5 4+1/5

(3,1) (3,2) (4,3) (4,4)

Find intersectionsFind intersections

Span RulesSpan Rules

n intersection at integer coordinaten leftmost : interiorn rightmost: exterior

A standard convention is to say that a point on a left or bottom edge is inside, and a point on a right or top edge is outside.

n shared verticesn count parity at ymin vertices onlyn shorten edges

n horizontal edgesn do not count vertices

ScanScan--line Fill Algorithmline Fill Algorithmn Not quite simple

n A scan line passed through a vertex, it intersectstwo polygon edges at that point.

ScanScan--line Fill Algorithmline Fill Algorithmn Scan line y’

n intersects an even number of edgesn Two pairs of intersection points correctly identify the

interior span

n Scan line yn intersects an odd number (5) of edgesn Must count the vertex intersections as only one point

ScanScan--line Fill Algorithmline Fill Algorithmn How to distinguish these cases

n Scan line yn Two intersecting edges are on opposite sides of the scan

linen Counted as just one boundary intersection point

n Scan line y’n Two intersecting edges are both above the scan line

n By tracing around the boundary,n If three endpoint y values of two consecutive edges

monotonically increase or decrease® count the shared vertex as a single intersection

n Otherwise, (a local minimum or maximum)® add the two edge intersections with the scan line

ScanScan--line Fill Algorithmline Fill Algorithmn Implementation

n To shorten some polygon edges to split those vertices thatshould be counted as one intersection

n While processing non-horizontal edges in (counter)clockwisen Check each edge whether the edge & next edges have

either monotonically increasing or decreasing endpointsn If so, shorten the lower edge

ScanScan--line Fill Algorithmline Fill Algorithmn Coherence properties

n span coherence - all pixels on a span are set tothe same value

n scan-line coherence - consecutive scan lines areidentical

n edge coherence - edges intersected by scan line iare also intersected by scan line i+1

ScanScan--line Fill Algorithmline Fill Algorithmn Use edge coherence and the scan-line algorithm

n Sorted Edge Tablen Contains all the non-horizontal edges.n Edges are sorted by their smaller y coordinates.n Table entry: maximum y value, x-intercept value, inverse slope

n For each scan line, edges are sorted from left to right

n Active Edge Listn While processing scan lines from bottom to top,

product active edge list for each scan line.n Contains edges which intersect the current scan line.

n Edges are sorted on their x intersection values.

ScanScan--line Fill Algorithmline Fill Algorithm

Area Filling (Scan line method)

Scan line 9

Scan line 10

Span RulesSpan Rules

n intersection at integer coordinaten leftmost : interiorn rightmost: exterior

A standard convention is to say that a point on a left or bottom edge is inside, and a point on a right or top edge is outside.

n shared verticesn count parity at ymin vertices onlyn shorten edges

n horizontal edgesn do not count vertices

Area Filling (Filling Methods)Area Filling (Filling Methods)

n Pixel Adjacency

8-connected

4-connected

n Boundary-Fill Algorithmn If the boundary is specified in a single color, fill the interior,

pixel by pixel, until the boundary color is encounteredn starts from an interior point and paints the interior in a

specified color or intensity.

procedure boundaryFill4(x,y : integer // starting point in regionborderColor: color // color that defines boundaryfillColor : color); // filling color

varc : color

beginc := readPixel(x,y);if ( c ≠ borderColor and c ≠ fillColor ) then

beginwritePixel(x,y,newValue);boundaryFill4(x, y-1, borderColor , fillColor );boundaryFill4(x, y+1, borderColor , fillColor );boundaryFill4(x-1, y, borderColor , fillColor );boundaryFill4(x+1, y, borderColor , fillColor );

endend;

BoundaryBoundary--Fill AlgorithmFill Algorithm

BoundaryBoundary--Fill AlgorithmFill Algorithm

• There is the following problem with boundary_fill4:

• Involve heavy duty recursion which may consume memory and time

Solve with 8-connected

BoundaryBoundary--Fill Fill AlgoAlgo..

Efficiency in space!n Starting from the initial interior

point, first fill in the span on the starting scan line

n Locate and stack starting positions for spans on the adjacent scan lines

n in down to up ordern in left to right order

n All upper scan lines processed ðAll lower scan lines processed

FloodFlood--Fill AlgorithmFill Algorithm

n Flood-Fill Algorithmn When filling an area that is not defined within a single color

boundary…n start from an interior point and reassign all pixel values that

are currently set to a given interior color with the desired fill color.

procedure flood_fill4(x,y: integer //starting point in regionoldValue: color // value that defines interiornewvalue: color); replacement value

beginif readPixel(x,y) = oldValue then

beginwritePixel(x,y,newValue);flood_fill4(x,y-1,oldValue,newValue);flood_fill4(x,y+1,oldValue,newValue);flood_fill4(x-1,y,oldValue,newValue);flood_fill4(x+1,y,oldValue,newValue);

endend;

FloodFlood--Fill AlgorithmFill Algorithm

Patterned LinesPatterned Lines

n Patterned line from P to Q is not same as patterned line from Q to P.

P Q

P Q

n Patterns can be geometric or cosmeticn Cosmetic: Texture applied after transformationsn Geometric: Pattern subject to transformations

Cosmetic Geometric

Character, SymbolsCharacter, Symbols

n Bitmap font (Raster font)n Set up a pattern of binary values on a rectangular gridn The simplest to define and displayn But, more storage : each variation (size and format) must be

saved

Character, SymbolsCharacter, Symbolsn Outline font

n Use straight-line and curve sectionsn More flexible methodn Can be increased in size without distorting the character

shapesn By manipulating curve definitions for the character outlines.n More time, since they must be scan converted into the frame buffer

n Less storage

Character, SymbolsCharacter, Symbols

Outline font Bitmap font easy to rotate rotate by multiples of 90° easy to scale scale by powers of 2 variable length storage fixed length storage scan convert lines scan convert points fill if polygons draw as filled or outline may be anti-aliased or smoothed via curve fitting may be pre-anti-aliased

Comparison of Methods

Line AttributesLine Attributes

Butt cap

Round cap

Projecting square cap

Miter join

Round Join

Bevel join

Aliasing in CGAliasing in CG

Which isbetter?

Aliasing in CGAliasing in CG

n Digital technology can only approximate analog signals through a process known as sampling.

n Aliaising : the distortion of information due to low-frequency sampling (undersampling).

n Choosing an appropriate sampling rate depends on data size restraints, need for accuracy, the cost per sample…

n Errors caused by aliasing are called artifacts. Common aliasing artifacts in computer graphics include jagged profiles, disappearing or improperly rendered fine detail, and disintegrating textures.