OpenGL: Basic algorithm

Source: Internet
Author: User

One, linear scanning algorithm: Through the known two points can be obtained linear equation:
y = kx + B;
K = (y1-y0)/(x1-x0);
b = y0-kx0;
1. Numerical differential method (DDA)
The straight segment X1 = kx + B, which is known over the endpoint P0 (x0, y0), P1 (Y1, l:y), is easy to know that the line slope is: k = (y1-y0)/(x1-x0), (assuming x1≠x0).
We assume that |k|≤1, so that x each additional 1,y will increase the k, and ensure that x increments of 1,y per increment cannot be greater than 1; if |k| > 1, you should swap x and Y. Because K is a floating-point number, the algorithm needs to round the Y to int and round to the nearest position.
The DDA algorithm in each iteration of x, Y value is the value of the previous step plus an increment obtained, so it is an incremental algorithm. However, this method is intuitive but inefficient because each step requires a floating-point multiplication and a rounding operation.
2. Midpoint Drawing Line Method
In the case where the linear slope is 0~1 Direct, the current pixel is set to (x, y), then its next pixel is P1 (x+1,y) or P2 (x+1,y+1), if the midpoint m (p1) of P2 and px+1,y+0.5, Q is the intersection of the ideal line and the x+1 perpendicular, When Q is below M, P1 is the next pixel, otherwise the P2 is the next pixel point.
3. Bresenham algorithm
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 pixel closest to this intersection in the column pixel. The trick of the algorithm is to use incremental calculation, so that for each column, just check the symbol of an error term, you can determine the pixel of the column.
Principle:

Steps:

Second, the region filling algorithm
1. Recursive algorithm
Starts from the specified seed point, searches in all directions, processes pixels by pixel, until a boundary is encountered, and the various seed-filling algorithms differ only in how colors and boundaries are handled.
2. Scan line algorithm
The basic process of the scan line seed filling algorithm is as follows: When a given seed point (x, y) is first filled to the left and right in two directions, the seed point is located on the scan line in a section of a given region, and note the range of this section [Xleft, Xright], and then determine the link to this section of the upper, The section of the next two scan lines in a given area, and then save it in turn. Repeat this process until the fill is finished.
The scan line seed fill algorithm can be implemented in the following four steps:
(1) Initialize an empty stack to hold the seed point, and the seed point (x, y) into the stack;
(2) Determine whether the stack is empty, if the stack is empty to end the algorithm, otherwise remove the top element of the stack as the current scan line seed point (x, y), Y is the current scan line;
(3) from the seed point (x, y), fill in the left and right two directions of the current scan line until the boundary. The coordinates of the left and right endpoints of each marked section are Xleft and xright;
(4) respectively check the y-1 and y + 12 scan lines adjacent to the current scan line in the interval [xleft,xright] pixels, starting from xleft to xright direction search, if there are non-boundary and unfilled pixels, then find these adjacent pixels in the rightmost one, and press it as a seed point into the stack, and then return to step (2);

Three, Circle generation algorithm: Draw a circle by clicking the circle radius given the circle center.
1. Through the definition of the circle: (X-XC) ^2 + (Y-YC) ^2 = r^2;

2. Assuming that the equation for the circle is: X2+Y2=R2, then there is a function: F (x, y) =x2+y2-r2
When f (x, y) >0, the corresponding point (x, y) is outside the circle;
When f (x, y) <0, the corresponding point (x, y) is outside the circle;
When f (x, y) =0, the corresponding point (x, Y) is on the circle.

In the first quadrant a half 1/8 arc, if the pixel point P (XP,YP) of the arc is obtained, then the next possible pixel point is P1 (XP+1,YP) and
P2 (xp+1,yp-1). Whether it is P1 or P2, it is determined by the function value F (xp+1,yp-0.5) of the midpoint m (xm,ym) of P1 and P2:

If f (xm,ym) <0, the arc between P1 and M is taken, the P1 point is the next pixel.
And the next pixel of F (xp+2,yp-0.5) =f (xp+1,yp-0.5) +2xp+3
If f (xm,ym) ≥0, indicating that the arc is between P2 and M, the P2 is the next pixel.
And the next pixel F (xp+2,yp-1.5) =f (xp+1,yp-0.5) +2 (XP-YP) +5
and f (1,r-0.5) = 1.25-r

To split a concave polygon:

Determine if a point is inside a polygon:
Parity rule: Draw a ray from any point p to a distance beyond the object range, and count the number of intersections of the line to the polygon, when the estimate is odd, the internal point of P, otherwise it is an external point.
Non-0 surround number rule:

OpenGL: Basic 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.