OpenGL Research 2.0 Calculate Circle

Source: Internet
Author: User

OpenGL Research 2.0 Calculate Circle

Dionysoslai2014-06-18

In the game. There are often some areas involving circular trajectory calculations, such as some rotary axis games, where characters generally move above the character's hinges. At this point, we need to calculate the position of the character.

Analyze it. General algorithm for round position.

First of all. For example with a circle:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvrglvbnlzb3nfbgfp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Figure 1

Based on the principle of symmetry, we just need to calculate the point set of the black area. At the same time, because the tangent slope of the circle in the black area is significantly 1 to 0, this can be combined with the calculation method of the previous segment.

The point calculation method of the circle:

1. Direct calculation method

1). Discrete point calculation method

Using the implicit function formula method:;

2). Trigonometric methods

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvrglvbnlzb3nfbgfp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

These two methods. Obviously involves the trigonometric functions, the open square root and so on some relatively complex calculation method, the computation amount is bigger.

2. Midpoint Operation method

Thought: The positive and negative division of a circle: the point (x, y) is brought into F (x, y). The

F (x, y) >0----> dots outside the circle. f (x, y) =0----> dots on the Circle, f (x, y) <0----> dots inside the circle.

Operation:

In Figure 1, the black area has an X-value change rate greater than Y, and the tangent slope is between 1 and 0, and the x increments by 1. The

And the relationship for example with what is seen:


For points. Either at point e or at Point SE.

Basic principle:

For the midpoint of the line between the next point of the point m= (xi+1,yi-0.5), the point M is brought into the F (x, y) equation, assuming F (m) <=0, stating that point M is in the circle, point to point E, and vice versa.

Mathematical calculation:

If Di=f (M), then the D value of the next point is seen in the following example:

1) assuming di<=0, the

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvrglvbnlzb3nfbgfp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "> Take e point, i.e.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvrglvbnlzb3nfbgfp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">. So


2) assuming di>0, the

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvrglvbnlzb3nfbgfp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "> Take se point, i.e.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvrglvbnlzb3nfbgfp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ";

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvrglvbnlzb3nfbgfp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

3) What is the value for D0? There are computational formulas to know:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvrglvbnlzb3nfbgfp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

4) Comprehensive. We have for example the following relationship:


The code is as follows:

void midcircle (int x0, int y0, int R) {/* just draw 1/8 round */int D0, d1, d2,x,y,xend;d0 = 1.25-r;x = X0;y = Y0-r;xend = X0+sqrt (2)/2.f *r; SetPixel (x, y), while (X<xend) {if (d0<=0) {d0 = d0+x+x+3;} Else{y = Y-1;d0 = d0+x+x-y-y+5;} x = x+1;}}

3. Positive inner Polygon approximation method

Rationale: A circle is constantly approached by Nes:


Calculation formula:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvrglvbnlzb3nfbgfp/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

So so. Just want you to Nes the number of sides the bigger. The closer the circle is. I tried 100 multi-side shape, in the same round of demand line.



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

OpenGL Research 2.0 Calculate Circle

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.