Midpoint Ellipse algorithm

Source: Internet
Author: User

An ellipse differs from a circle in that eight points can only be four points. The midpoint ellipse algorithm is divided into two parts to be applied to the first quadrant. The unit step is taken in the x direction in the area where the slope absolute value is less than 1, and the unit stride is taken in the Y direction in the area with the absolute slope greater than 1.

Can define the ellipse function as

That is, the decision parameters.

From the beginning, the unit step is taken in the direction until the boundary of Region 1 and Region 2, and then the unit step of the direction is transferred, and then the remaining curve segments in the first quadrant are overwritten. However, each step is to detect the slope value of the curve.

Slope equation:

At the junction of Region 1 and Zone 2, and

Therefore, the condition for removing zone 1 is

As with the midpoint drawing circle algorithm, the decision function is evaluated by the midpoint to determine the next position along the elliptical trajectory:

 

At the next sampling location, the decision parameters for Region 1 can be evaluated as

Or

Where, depending on the symbol, the value is or.

If the increment is incremented, if the increment increment is.

In Region 2, the unit step is sampled in the negative direction.

Evaluate the ellipse function at the next location:

Or

Where the setting is based on the symbol the desirable value is or.

Algorithm process:

1. Input, and Ellipse center, and get the first point on the ellipse:

2. Calculate the initial values for the decision parameters in Region 1:

3. At each position in zone 1, starting from, if, the next point along the center of the ellipse is, and

Otherwise, the next point along the Ellipse is, and

which

and until.

4. Use the last point calculated in area 1 to calculate the initial value of the parameter in Zone 2:

5. At each position of area 2, starting from, if, the next point of the ellipse along the center is, and

Otherwise, along the next point of the ellipse, and

Use the same and incremental calculations as in Zone 1 until.

6. Determine the symmetry points in the other three quadrants.

7. Move each calculated pixel position to the center of the ellipse trajectory and draw the points by coordinate values:

1InlineintRound (Const floatA) {returnstatic_cast<int> (A +0.5); }2 3 voidSetPixel (Glint Xcoord, glint Ycoord)4 {5 Glbegin (gl_points);6 glvertex2i (Xcoord, Ycoord);7 glend ();8 }9 Ten voidEllipseplotpoints (intXcenter,intYcenter,intXinty) One { ASetPixel (Xcenter + x, Ycenter +y); -SetPixel (xcenter-x, Ycenter +y); -SetPixel (Xcenter + x, Ycenter-y); theSetPixel (Xcenter-x, Ycenter-y); - } -  - voidEllipsemidpoint (intXcenter,intYcenter,intRxintRy) + { -     intRX2 = Rx *Rx; +     intRy2 = Ry *Ry; A     intTWORX2 =2*Rx2; at     intTwoRy2 =2*Ry2; -     intp; -     intx =0; -     inty =Ry; -     intPX =0; -     intPY = twoRx2 *y; in     //Plot The initial point in each quadrant - ellipseplotpoints (Xcenter, Ycenter, x, y); to     /*Region 1*/ +p = Round (Ry2-(RX2 * Ry) + (0.25*Rx2)); -      while(PX <py) { theX + +; *PX + =TwoRy2; $         if(P <0) {Panax Notoginsengp + = Ry2 +px; -         } the         Else { +y--; APY-=twoRx2; thep + = Ry2 + PX-py; +         } - ellipseplotpoints (Xcenter, Ycenter, x, y); $     } $     /*Region 2*/ -p = Round (Ry2 * (x +0.5) * (x +0.5) + Rx2 * (Y-1) * (Y-1)-RX2 *Ry2); -      while(Y >0) { they--; -PY-=twoRx2;Wuyi         if(P >0) { thep + = Rx2-py; -         } Wu         Else { -X + +; AboutPX + =twoRx2; $p + = Rx2-py +px; -         } - ellipseplotpoints (Xcenter, Ycenter, x, y); -     } A}

Midpoint Ellipse 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.