Sie sind auf Seite 1von 4

# EXPERIMENT NO.

Group A -4

## Title: scan line algorithm.

.Problem Statement: Write C++/Java program to fill polygon using scan line algorithm. Use mouse
interfacing to draw polygon
Software Requirement: Fedora,C++
Input: Polygon.
Output: Filled Polygon

Theory:

Polygon:

The boundary of the polygon is formed by joining the last point you specify to the first one.

Scan line:-

The scan line fill algorithm is an ingenious way of filling in irregular polygons. The algorithm begins
with a set of points. Each point is connected to the next, and the line between them is considered to be an
edge of the polygon. The points of each edge are adjusted to ensure that the point with the smaller y value
appears first. Next, a data structure is created that contains a list of edges that begin on each scan line of
the image. The program progresses from the first scan line upward. For each line, any pixels that contain
an intersection between this scan line and an edge of the polygon are filled in. Then, the algorithm
progresses along the scan line, turning on when it reaches a polygon pixel and turning off when it reaches
another one, all the way across the scan line.

## Scan Line Algorithm

This algorithm works by intersecting scanline with polygon edges and fills the polygon between pairs of
intersections. The following steps depict how this algorithm works.

Step 1 − Find out the Ymin and Ymax from the given polygon.
Step 2 − ScanLine intersects with each edge of the polygon from Ymin to Ymax. Name each
intersection point of the polygon. As per the figure shown above, they are named as p0, p1, p2,
p3.

Step 3 − Sort the intersection point in the increasing order of X coordinate i.e. (p0, p1), (p1, p2),
and (p2, p3).

Step 4 − Fill all those pair of coordinates that are inside polygons and ignore the alternate pairs.

Drawing Shapes

The Graphics class defines methods for drawing the following kinds of shapes:

 Lines (drawLine(), which draws a line in the Graphics object's current color, which
is initialized to the Component's foreground color)
 Rectangles (drawRect(), fillRect(), and clearRect() -- where fillRect() fills a rectangle
with the Graphics object's current color, and clearRect() fills a rectangle with the
Component's background color)
 Raised or lowered rectangles (draw3DRect() and fill3DRect())
 Round-edged rectangles (drawRoundRect() and fillRoundRect())
 Ovals (drawOval() and fillOval())
 Arcs (drawArc() and fillArc())
 Polygons (drawPolygon() and fillPolygon())

Except for polygons and lines, all shapes are specified using their bounding rectangle. Once you
understand rectangles, drawing other shapes is relatively easy. For this reason, this page
concentrates on rectangle drawing.
Pseudo code :
ALGORITHM:

## i. Intersect each scanline with all edges

ii. Sort intersections in x
iii. Calculate parity of intersections to determine in/out
iv. Fill the “in” pixels

## i. Horizontal edges should be excluded

ii. Vertices lying on scanlines handled by shortening of edges,

## 3. Coherence between scanlines tells us that

a. Edges that intersect scanline y are likely to intersect y + 1
b. X changes predictably from scanline y to y + 1 (Incremental Calculation Possible)

## Fig 8.1: Scan Line Algorithm

4. The slope of the edge is constant from one scan line to the next:
a. Let m denote the slope of the edge.
b.
5. Each successive x is computed by a

Each successive x is computed by adding the inverse of the slope and rounding to the nearest integer
MATHEMATICAL MODEL :
S= {I,O,F}
S = system
I= input polygon
O=output filled polygon using scan line algorithms

Xnew= Xold+(1/m)
where m = slope

CONCLUSION:.