Basic Graph Generation algorithm

Source: Internet
Author: User

1. Straight Line Generation algorithm

The so-called entity generation refers to the transformation of the parameter representation of the finished entity (as specified by the user of the graphics package) to the bitmap representation (the representation required by the raster Display system refresh). Usually also referred to as a scan transformation entity.

Scan transformation of a line: determines the set of pixels that are best approximated to the line, and writes the pixels in the order of scan lines.

Three commonly used algorithms: 1, numerical differential method dda;2, Midpoint drawing line method, 3, Bresenham algorithm.

Create a target to get a set of pixels that are sufficiently close to the line segment

The precondition of generation: 1, the pixel grid is uniform, the coordinate is integer value, 2, the line segment width is 1, 3, the line segment slope k value range is [ -1,1].

1.1 Numerical Differential method1.1.1 Basic Ideas

The straight segment of the P0 (x0, y0), P1 (x1, y1) is known to be the L: y=kx+b line slope is k = (y1-y0)/(x1-x0), X starts from the left endpoint x0, the right endpoint X1 in step =1 (pixels), calculates the corresponding The y-coordinate y=kx+b the pixel point (x, Y (x)) as the coordinates of the current point.

Calculate yi+1 = Kxi+1+b

= K1XI+B+KDX

= YI+KDX where dx = 1; Yi+1 = Yi+k.

1.1.2 Analysis
    • When x increments by 1,y increments K(that is, the linear slope);
    • Note that the above analysis algorithm only applies to | k| The situation of ≤1. In this case,x increments by 1 andy increases by up to 1.
    • When | k| >1, you must swap x,y position
    • It is divided into 8 quadrants according to the direction of the line from (X0,y0) to (x1,y1). For a straight line in the 1th a quadrant, Dx=1, Dy=k. For a straight line in the 1b quadrant, the value Dy=1, dx=1/k.

The corresponding relationship is as follows:

1.2 Midpoint Drawing Line Method1.2.1 Basic Ideas

The current pixel point is P (XP, YP), and the next pixel is P1 or P2. Set m= (xp+1, yp+0.5), the midpoint of P1 and P2, Q is the intersection of the ideal line and the x=xp+1 perpendicular. Compare Q to the y-coordinate of M.

As shown in the following:

When M is below the Q, then the P2 should be the next pixel point;

When M is above the Q, the P1 should be taken as the next point.

1.2.2 Analysis

Construction discriminant:d=f (M) =f (xp+1,yp+0.5) = A (xp+1) +b (yp+0.5) +c

among them a=y0-y1, b=x1-x0, c=x0y1-x1y0

When the d<0,m is under the L (Q Point), take the top right PU as the next pixel;

When the d>0,m is above the L (Q point), the right PD is the next pixel;

When d=0, choose P1 or P2 can, agreed to take PD as the next pixel;

D is the linear function of XP, YP, so it can be used in increment calculation to improve operation efficiency. Initial value d0=f (x0+1, y0+0.5) =a+0.5b.

(1) If the current pixel is in the d>=0 case, then take the positive right pixel PD (xp+1, YP), to be sentenced to the next pixel position, it should be calculated: Di+1=f (xp+2, yp+0.5) =a (xp+2) +b (yp+0.5) =d+a;

(2) If d<0, then take the upper right pixel pu (xp+1, yp+1). To determine the next pixel, calculate: di+1= F (xp+2, yp+1.5) =a (xp+2) +b (yp+1.5) +c=d+a+b, and increment to a+b.

Drawing lines starting from (x0, y0), the initial value of D d0=f (x0+1, y0+0.5) =f (x0, y0) +a+0.5b =a+0.5b.

You can use 2d instead of D to get rid of decimals and improve efficiency.

1.2.3 Example Demo

The line of P0 (0,0), P1 (5,2) is drawn by the midpoint drawing method.

First, the discriminant formula is constructed:d = A (xp+1) +b (yp+0.5) +c . where a = Y0-y1 = -2,b = x1-x0 = 5,c = x0y1-x1y0 = 0.

Then the initial value D0 = a + 0.5b = 0.5. Take the point P ' (1,0) to the position of the next pixel. According to the rules (1) and (2), the following table of results can be obtained:

I

Xi

Yi

D

1

0

0

0.5

2

1

0

-1.5

3

2

1

1.5

4

3

1

-0.5

5

4

2

2.5

expressed in coordinates:

1.3 , Bresenham algorithm1.3.1 Basic Ideas

A set of virtual gridlines is constructed across rows of pixel centers. Calculates the intersection of the line and each vertical gridline in the order of the line from the start point to the end point, and then determines the nearest pixel in the column's pixel to that intersection, based on the symbol of the error item.

1.3.2 Analysis

Set the linear equation to:, where k=dy/dx. Since the starting point of the line is in the center of the pixel, the initial value of the error item D d0=0.

for each increment of 1, the value ofD increments the slope value of the line by K, i.e. d=D+K. Once d≥ 1, subtract 1, so that D is between 0 and 1.

When D≥0.5, closest to the upper right pixel of the current pixel (XI+1,yi+ 1)

When D<0.5, it is closer to the right pixel (XI+1,Yi).

For ease of calculation, e=d-0.5,e has an initial value of-0.5, and the increment is K.

When e≥ 0 o'clock, take the pixel at the topright of the current pixel (Xi, Yi) (Xi+1,yi+ 1);

When e<0, it is closer to the right pixel (XI+1,Yi).

1.3.3 Example Demo

Use the Bresenham algorithm to draw the P0 (0,0), P1 (5,2) line.

The calculated slope k = DY/DX = 0.4.

The result is the following table:

I

X

Y

E

1

0

0

-0.5

2

1

0

-0.1

3

2

1

0.3

4

3

1

-0.3

5

4

2

0.1

6

5

2

-0.5

Coordinate representation:

the Bresenham algorithm uses decimal and division when calculating the slope and error terms of a line. You can use integers to avoid division. Because the algorithm uses only the symbol of the error term, it can be replaced as follows

So the initial value of E ' is -dx, because of k=dy/dx, so the increment of e ' becomes 2*dy.

Advantages of the 1.3.4 Bresenham algorithm
    • You do not have to calculate the slope of the line, so do not divide;
    • No floating-point numbers, only integers;
    • Only integer plus subtraction and multiply 2 operations, and multiply 2 operations can be implemented with hardware shift.
    • The Bresenham algorithm is fast and suitable for hardware implementations.
2. Arc Generation algorithm

Circle characteristics: Eight symmetry, as long as the scan conversion one-eighth arc, you can find the entire arc of the pixel set.

2.1 Midpoint Bresenham Drawing Circle method

Consider the midpoint at the origin, the radius of R for the second eight-minute circle, construct the discriminant (circle equation):

If d<0, then take P1 as the next pixel, and the discriminant of the next pixel is:

If d>=0, then take P2 as the next pixel, and the discriminant of the next pixel is:

The first pixel is (0,r), and the initial value of discriminant D is:

In order to further improve the efficiency of the algorithm, the floating-point number in the above algorithm can be rewritten into integers, and the multiplication operation is changed to the addition operation, that is, only integer is used to realize the midpoint circle method. Use e=d-0.25 instead of de0=1-r.

3. Polygon Filling algorithm

Polygons have two important representations: vertex notation and dot matrix notation.

Scan transformation of polygons: Transforms the vertex representation of a polygon into a lattice representation.

There are 4 kinds of methods: Point-by-spot method, scanning line method, edge filling method and seed filling method.

3.1 Points-by-point judging method

Determines, one by one, whether the pixel in the drawing window is within the polygon.

The method of judging whether the point is inside the polygon: Ray method, accumulative angle method.

3.1.1 Ray method

The k,k of the number of intersections determines the internal and external relations between the points and polygons from the point V to be judged:

If k is the base, the point is within the polygon.

If k is an even number, the point is outside the polygon.

3.1.2 Cumulative Angle method

Emits a ray from the V point to the polygon P vertex to form a angular angle.

Calculate the and of the angular, and draw a conclusion:

                  

  

3.2 Scanning Line method 3.2.1 Basic idea

There is a feature of coherence in the use of pixels on a scan line (regional coherence, scan line coherence, and polygon edge coherence). Avoid the calculation of pixel-by-point judgment and repeated intersection.

1) Regional coherence: For each of the adjacent two regions, one area is inside the polygon and the other area is outside the polygon.

2) The coherence of the scanning line: If the scanning line y=j with the polygon P, the intersection number is even. And on the scan line, only the section is within the polygon p.

3) Edge Coherence: If the edges of the polygon p intersect with the scan line y=k1 to Y=k2, then the edge has the following relationship: Xi+1 = xi + 1/m. where m= (y2-y1)/(X2-X1) is the slope of the segment of the polygon edge.

3.2.2 Algorithm Steps
    1. Intersection: Calculates the intersections between the scanned lines and the polygon edges;
    2. Sort: Sorts all intersections in ascending order of X-values;
    3. Pairing: The first with the second, the third and the fourth, and so on; each pair of intersections represents an intersection of a scan line and a polygon;
    4. Fill: The pixels in the intersection interval are placed into a polygon color, and the pixels outside the intersecting interval are placed into the background color.
3.3 Improved scan line algorithm (Y-X)

Scanning line (Y-X) algorithm in the scanning line to and all edges to intersection, the efficiency is low. Because a scan line often intersects only a few edges.

3.3.1 Algorithm Idea

Take advantage of two coherence:

1. Edge coherence: When an edge intersects the current scan line, it is likely to intersect the next scan line as well.

The x-coordinate of the intersection of the current scan line and one edge of the polygon is X, then the intersection of the next scan line and that edge is not recalculated, as long as an increment x is added.

The linear equation for this edge is: ax+by+c=0, if y=yi,x=x i; when y = y i+1, where x =-b/a is a constant.

2. Coherence of the scan line: the order of intersection of the current scan line and each edge, and the next scan line is likely to be the same or similar to the order of intersection of each edge.

The main idea of the Y-X algorithm:

1. The edges that intersect the current scan line are called active edges (active edge) and are stored in a linked list in the order in which they are incremented by the X coordinate of the scan line intersection, called the active edge table (AET, active edge table).

2. Just update the active edge table of the current scan line to get the activity side table of the next scan line.

3.3.2 Data structure

Active Edge Table (AET): the edges that intersect the current scan line are called active edges and are stored in a linked list in the order in which they are incremented by the X coordinate of the scan line intersection.

Node content:

X: Intersection coordinates of the current scan line and edge

X: The increment of x between the current scan line and the next scan line

Ymax: The highest scan line number to be handed on this side ymax

Side table Bucket (NET): The edge that is stored on the first occurrence of the scan line. If the lower endpoint of an edge is ymin, the edge is placed in the new edge table of the scan line ymin.

3.4 Edge padding algorithm

remainder Operation : Assuming A is a positive integer, the remainder of M is defined as A–m, which is recorded as. The largest integer represented by a is N potential energy in the computer. That is,a=0xffffffff.

3.4.1 Basic Ideas

For each scan line and the intersection of each polygon edge, take the remainder of all pixels to the right of the intersection on the scan line.

① the background color of the drawing window;

② each non-horizontal edge of the polygon, starting from each pixel on that edge to the right to find redundancy;

    • Suitable for graphics systems with frame caching. After processing, the contents of the frame cache are read out according to the scan line sequence, and sent to the display device.
    • Advantages: Simple algorithm
    • Cons: For complex graphs, each pixel can be accessed multiple times, and the input/output is much larger than the ordered edge table algorithm.
3.4.2 Fence Filling algorithm
    • Introducing fences to reduce the number of times the fill algorithm accesses pixels
    • Fence: A straight line perpendicular to the scan line, usually over a vertex, and dividing the polygon into two halves.
    • The basic idea: the scanning line and the polygon edge intersection, will be the point and the fence between the pixels to complement.
    • Reduced number of pixel repeat visits, but not outright.

3.4.3 Boundary Sign algorithm

Basic idea:

In the frame buffer, each edge of the polygon is scanned for a straight line, that is, the pixel that passes through the polygon boundary is marked, and then the method similar to the scan line algorithm will be the color required to be in each section of the polygon. Use a Boolean inside to indicate whether the current point is in the polygon state.

3.4.4 Region seed Filling algorithm

1. Definition of the area:

1) within the defined area: within the area with the same color, the outside has a color.

2) Boundary definition area: The boundary has a specific color with a new value (fill color) within the boundary.

3) Four connected areas: each pixel is connected in the horizontal and vertical four directions.

4) Eight connected areas: each pixel is connected in eight directions.

area fill refers to the process of assigning a point of a region to a specified color and then extending that color to the entire region. The area fill algorithm requires that the area be connected.

    • The area represented by the boundary point: the inner pixel value is the specified value.
    • The area represented by the inner point: The primary color is changed to a new colour.
    • The fill has a pixel known as a "seed"
4. Anti-aliasing

The phenomenon of distortion caused by the continuous amount of discrete quantities is called aliasing (aliasing), and the phenomena of grating graphics aliasing are:

N Ladder-shaped boundary

N Graphic Detail distortion (those in the graph are wider than the narrower details of the pixel)

n Small graphic loss and flicker of dynamic graphics

Techniques used to reduce or eliminate this effect become anti-aliasing (antialiasing)

4.1 Improve resolution

Increase the display resolution by one time: The line passes twice the pixel, the sawtooth also increases one times, but the width of each step is also reduced by one times. So the displayed line segment looks smoother.

Pros and Cons: Increased resolution is simple, but not an economic method; The display's horizontal and vertical resolution increases by one times, the display's point distance is reduced by one-fold, the frame buffer capacity is increased to the original four times times, while the scanning conversion of the same size of the entity will take 4 times times. And it can only alleviate and not eliminate aliasing.

4.2 Region Sampling 4.2.1 basic principle of simple region sampling

Two assumptions:

1. Pixels are mathematically abstract points with an area of 0, and its brightness is determined by the brightness of the graph covering the point;

2. The Straight line segment is a mathematically abstract straight line segment with a width of 0.

In reality, however, the pixel area is not 0, and the line segment width is at least one pixel. The contradiction between hypothesis and reality is one of the reasons leading to the phenomenon of trend.

Each pixel is a small area with a certain area, and the straight segment is considered a narrow rectangle with a certain width. When the line segment and the elephant are intersecting, the area of the intersection area is calculated, and then the luminance value of the pixel is determined according to the size of the area of the intersection.

Five situations in which 4.2.2 pixels intersect

4.2.3 Line Contour with width

4.2.4 Area Calculation
    • Situation (1) (5) Shadow area: d2/2m
    • Situation (2) (4) Shadow area: D-M/2
    • Situation (3) The shadow area is: 1-d2/m
    • The above shadow area is a positive number between 0-1, multiply it by the maximum gray value of the pixel, then take the whole, you can get the gray value of the pixel display. The anti-aliasing effect of this region sampling method is good.

To simplify the calculation, discrete methods can be used: n=9,k=3 approximate area of 1/3

First, the screen pixels are divided into n sub-pixel, and then the computing center points fall in the line segment of the number of sub-pixel K, the screen brightness of the pixel is set to the intersection area of the approximate value can be k/n.

There are two disadvantages of 4.2.5 non-weighted region sampling method
    • The luminance of a pixel is proportional to the area of the intersection, regardless of where the intersection falls within the pixel, which still leads to the sawtooth effect.
    • The adjacent two pixels along the ideal straight line of a straight line bar sometimes have a large gray-scale difference.
4.3 Weighted area sampling 4.3.1 basic idea

The contribution of the luminance of the object in the intersecting area depends on the distance between the region and the center of the pixel, and when the line passes through the pixel, the luminance F of the pixel is the integral value of the filter (function W) on the intersection of the two regions a ':

The filter function w can take a Gaussian filter:

4.3.2 Using discrete calculation method

For example: We divide the screen into n=3x3 pixels, and the weighted table can be taken as:

The Weight function w (x, y) is a function of the center distance d of the micro-surface da and the pixel, and then the sub-pixels of all the centers falling in the straight line segment are calculated, and the maximum gray value of the pixel is computed by multiplying all the sub-pixels ' contribution to the brightness of the original pixel.

In particular, weighted region sampling degrades to non-weighted region sampling when each weight value is equal.

5. Cutting 5.1 Concepts

Determines which parts of the drawing fall within the display area and which fall outside the display area, and displays only the graphics that fall within the display area. This process is called cropping.

5.2 Coding cutting 5.2.1 basic idea

For each line segment P1P2 is divided into three cases of processing:

(1) If the P1P2 is completely inside the window, the segment is displayed

(2) If the P1P2 is clearly outside the window, discard the segment

(3) If the line segment does not meet the "take" or "discard" conditions, at the intersection of the line segment is divided into two segments. One section is completely outside the window and can be discarded. Then repeat the above treatment for the other paragraph.

To quickly determine the encoding method: each region is given a 4-bit encoding:

If the p1p2 is completely code1=0 within the window, and code2=0, then "take"

If the P1P2 is clearly code1&code2≠0 outside the window, "Discard"

Divides the segment into two segments at the intersection point. One section is completely outside the window and can be discarded. Then repeat the above treatment for another paragraph

5.2.2 Area Code Table

5.2.3 Instances

The c1=c2=0 segment is within the window and the segment is received directly (b segment).

The c1&c2!=0 segment is outside the window and the segment is discarded directly (c segment).

C1&c2=0 segment has intersection with Window (a segment)

5.3.1 Basic idea of 5.3 midpoint segmentation cropping algorithm

When a line is not directly accepted and can not be directly discarded, then split its segment into half, and then test the segment separately, by means of a binary search method until the line segment is accepted:

A, B is the nearest visible point from P0, P1, and PM is the midpoint of P0P1.

The advantage of the midpoint cutting method is that the calculation speed is fast, because the algorithm is simple and the algorithm needs only the displacement calculation.

5.3.2 Algorithm Steps

From P0 to find the nearest visible point using Midpoint segmentation method

Find the midpoint PM of P0P1 First

If p0pm is not obviously invisible, and P0P1 has visible parts in the window, the closest visible point to P0 must fall on p0pm, so p0pm instead of P0P1

Otherwise take PmP1 instead of P0P1

Then ask for the midpoint PM for the new P0P1. Repeat the process until the PmP1 length is less than the given control constant, at which time the PM converges to the intersection point

Find the nearest visible point from P1 using the similar method above.

5.4 Liang Youdong-barskey algorithm 5.4.1 main idea

The segment p1p2 is represented as a parameter form:

Determine the beginning and end edges: Left → right, right → left, bottom → up, up → down.

two groups of intersections : 1, with the beginning of the intersection A and C, the parameters are R1 and r3;2, and the end edge of the intersection B and D, the parameters are R2 and R4.

The first part within the clipping rectangle is defined by the parameters U1 and u2: u1 = max (0,R1,R3); u2 = Max (1,R2,R4).

two questions : 1, how to determine the beginning and end of the edge? 2, how to beg R1,R2,R3,R4?

Workaround:

Parametric form of line segments

Clipping conditions in parametric form

5.4.2 Analysis

first question: Solve the intersection parameter of the line segment and the left, right, bottom, and top boundary R1 , R2 , R3 and R4 .

Can be uniformly expressed as:

which

The second question: How to determine the start and end edges?

Special cases:

When Pk=0 and Qk<0,

The segment is completely outside the bounds, qk≥0, and the segment is parallel to the clipping boundary and within the window

When pk≠0

    • When P1<0,p2 >0, then δx>0, line segments from the left edge to the right edge.
    • When P1 >0,p2<0, the line segment is δx<0 from the right to the left edge.
    • When P3<0,p4 >0, then δy>0, line segments from the bottom edge to the top boundary.
    • When P3 >0,p4<0, the line segment is δy<0 from the upper boundary to the lower boundary.
    • When pk<0, the segments extend from the outside of the clipping boundary extension to the interior. The boundary is used as the starting edge.
    • When pk>0, the segments extend from the inside of the clipping boundary extension line to the outside. The boundary is used as the end edge.

For each line, segment segments within the clipping rectangle are defined by the parameters U1 and U2

    • The value of the U1 is determined by the boundary of the beginning edge of the segment (that is, the rectangular boundary encountered from the outside to the inside) (p<0). Calculate RK on these boundaries. U1 0 and the maximum value of each RK value
    • The value of the U2 is determined by the end edge boundary of the segment (that is, the rectangle boundary that the segment encounters from inside to outside) (p>0). Calculate RK on these boundaries. U2 take 1 and the minimum value of each RK value
    • If U1>U2, the segment falls completely outside the cropping window and is discarded, otherwise the clipping line is calculated from the two values of the parameter U u1,u2

5.4 Edge-by-edge clipping algorithm 5.4.1 Polygon clipping principle

The polygon clipping result is still one or more polygons.

Polygon clipping focuses on the problem of correctly aligning the intersection of polygons that fall on the bounds of the window into a cropped polygon (which includes a trade-off between determining window boundaries and corners).

5.4.2 Basic Ideas

Each time a variable boundary of the window is clipped to the polygon to be cropped, the graph falling in the outside area of the window is removed, the shape of the inner area of the window is preserved, and it is used as the next polygon to be cropped.

5.4.3 Algorithm Implementation steps

1). Specify the vertex sequence of the polygon P

2). Take a window to crop the edges of the polygon e to check each vertex of the vertex sequence p[i] in turn.

and judged by the following:

A. The visible side of the e-side is included in the newly generated vertex sequence q[j].

B. Vertices on the invisible side of the e Edge are deleted.

3) Check whether the p[i] and p[i+1] are in the same, if not the intersection, the obtained intersection is included in the new polygon vertex sequence q[j]. :

4) The right edge of the clipping window is trimmed, and a new vertex sequence is made up of red dots.

5) Then use the bottom, the left and the top edge to cut separately. Finally, the cropped polygon is obtained.

5.4.4 Clipping Process

5.5 The basic ideas and steps of the algorithm of bilateral cutting algorithm

The user's multilateral type is the main polygon PS, while the cropping window is the clipping polygon pc.

The algorithm starts at any point of PS, each trace detects each edge of PS, when the PS and the effective edge of the PC intersect:

(1) If the edge of PS into the PC, then continue along the edge of the PS processing, and output

(2) If the edge of PS is out of the PC, start at that point (called the front intersection), detect the edge of the PC along the window edge, and find the new intersection of PS and PC closest to the front intersection. Simultaneously outputs the line segment from the front intersection to the new intersection.

(3) Return to the previous intersection and proceed to (1) ~ (3) steps.

The main polygon and clipping polygon divide the two-dimensional plane

inside cut : A∩B

outside cut : A-B

The boundary of the clipping result area is composed of part boundary of a and part boundary of B, alternating at the boundary of intersection, that is, the boundary of a is transferred to the boundary of B, or the boundary of B is transferred to the boundary of a.

Basic Graph Generation algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.