[Computer graphics] simulation software design of Bresenham linear scanning algorithm, seed filling method and scanning line filling method based on C # window (i.)

Source: Internet
Author: User

First, explain:

  • What is this? -- This is a use of c#form written to demonstrate the ①bresenham linear scanning algorithm in computer graphics (that is, connecting dots into lines); ② seed filling method (i.e., filling polygon); ③ Scanning Line Filling method
  • What's the use? -- Whether a point-to-line or area-fill is essentially a very efficient library function to invoke in advanced programming. These algorithms are used to understand that the underlying functions of the encapsulated function are realized, and the other is to facilitate the development of the embedded TFT screen at the bottom of the drive.
  • What is it like? - as the following operation, self-evident.

Second, enter the topic:

 scanning conversion of 2-1 and lines

The scanning and transformation of graphs is essentially the process of determining the best approximation of a pixel set in a graph on a digital device such as a grating.

For a straight-line scan conversion, it is to determine the best approximation to the line of a group of pixels, and then follow the order of the scan lines to write these pixels. Three commonly used algorithms are: ① numerical differential method (DDA), ② midpoint line method, ③bresenham algorithm. This is mainly about the third algorithm because it is efficient and easy to implement in hardware.

Bresenham algorithm: Because the pixel point of the display line can only take integer value coordinates, it can be assumed that the first pixel point coordinates of the line (xi,yi), which is the best approximation of the point (Xi,yi) in the line, and xi=xi ( Assume M<1), as shown in. Then, the possible position of the next pixel point in the line is (xi+1,yi) or (xi+1,yi+1).

And the actual point is a red point, you can calculate the difference between the possible point and the actual point deviation, namely: d1-d2=2m (xi+1)-2yi+ 2b-1. This makes it possible to determine the coordinates of the next point based on this difference and then move forward to find all the points. In addition, due to the fact that the speed of the processing of floating-point numbers is much slower than that of integer processing, some transformations are made in the algorithm (in fact, amplification makes them into integers, and the next point is correct). Because there is a lot of information on the Internet, it is not detailed here ~

Bresenham algorithm pseudo code: [Condition: 0<=m<=1 and X1<X2]

① two endpoint coordinates and line color of the input segment: X1,y1,x2,y2,color;
② set the initial value of pixel coordinates: x=x1,y=y1;
③ Set initial error discriminant value: p=2 δy-δx;
④ calculated separately: δx=x2-x1, δy=y2-y1;
The ⑤ loop enables the generation of straight lines:

1  for(x=x1;x<=x2;x++)2 { 3 Putpixel (x,y,color);4     if(p>=0)5     { 6y=y+1;7p=p+2* (δy-Δx);8     }9     ElseTen     {  Onep=p+2*Δy; A     } -}

Obviously, the above algorithm is with certain constraints, we will extend it to all slope conditions, to achieve a variety of line drawing. It is easy to prove that when the segments are in the ①, ④, ⑧, ⑤ regions, |δx| and |δy| are substituted for the Δx and Δy in the preceding formula, and ② and ③ are swapped when the segments are in ⑥, ⑦, |δx|, |δy| areas. The following is a Bresenham algorithm-based function for connecting point lines in the project:

  

1 /// <summary>2 ///Bresenham draw the line algorithm, give two points, a, B3 /// </summary>4 ///PS: Here Xiangsu is the minimum distance of the grating, because I am in the high-resolution window to simulate the low-resolution effect,5 ///so use this XIANGSU to control the size of the edge of the smallest cell of the simulated grating, the actual use of the xiangsu=16 /// <param name= "a" ></param>7 /// <param name= "B" ></param>8 voidBresenhamline (Point A, point B)9 {Ten     intx, y, dx, dy, s1, S2, p, temp, interchange, I; Onex =a.x; Ay =a.y; -DX = Math.Abs (b.x-a.x); -DY = Math.Abs (B.y-a.y); the     if(B.x >a.x) -S1 =Xiangsu; -     Else -S1 =-Xiangsu; +     if(B.y >a.y) -S2 =Xiangsu; +     Else AS2 =-Xiangsu; at     if(Dy >DX) -     { -temp =DX; -DX =dy; -DY =temp; -Interchange =1; in     } -     Else toInterchange =0; +p =2* DY-DX; -      for(i =1; I <= DX; i + =Xiangsu) the     { *Tempp. X =x; $Tempp. Y =y;Panax Notoginseng Tempp.add (TEMPP); -Vram[x + y * -] =true;//set the memory of the slice as a boundary marker the         if(P >=0) +         { A             if(Interchange = =0) they = y +S2; +             Else -x = x +S1; $p = P-2*DX; $         } -         if(Interchange = =0) -x = x +S1; the         Else -y = y +S2;Wuyip = p +2*dy; the     } -}

because the layout is limited, please link to the second section, more exciting, haha:http://www.cnblogs.com/zjutlitao/p/4117223.html

Two links:

[Computer graphics] simulation software design of Bresenham linear scanning algorithm, seed filling method and scanning line filling method based on C # window (i.)

[Computer graphics] simulation software design of Bresenham linear scanning algorithm, seed filling method and scanning line filling method based on C # window (II.)

[Computer graphics] simulation software design of Bresenham linear scanning algorithm, seed filling method and scanning line filling method based on C # window (i.)

Related Article

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.