中點畫圓算法


如同光柵畫線算法,每步都以間隔單位取樣並確定離指定圓最近的像素位置。為了減少計算量,可以將圓八分,根據Bresenham畫線算法。我們首先給出點位置函數:

   

即可得知:(1), 位於圓邊界內;(2)位於圓邊界上;(3), 位於圓邊界外。

第一象限中,假設在繪制了像素點,下一步需要確定繪制的位置是,還是更接近圓。

則決策參數的圓函數方程在這兩個像素的中點求值得到,

   

   

   

其中還是,取決於的符號。

算法過程:

1.輸入圓半徑和圓心,並得到圓周上的第一個點:

   

2.計算決策參數的初始值:

   

3.在每個位置,從開始,完成下列測試:假如,圓心在的圓的下一個點為,並且

   

否則,圓的下一個點是,並且

   

其中

4.確定在其他七個八分圓中的對稱點。

5.將每個計算出的像素位置移動到圓心在的圓路徑上,並畫坐標值:

    

6.重復步驟3到步驟5,直至

 1 class ScreenPt {
 2 private:
 3     GLint x, y;
 4 
 5 public:
 6     ScreenPt() {
 7         x = y = 0;
 8     }
 9     void setCoords(GLint xCoordValue, GLint yCoordValue) {
10         x = xCoordValue;
11         y = yCoordValue;
12     }
13     GLint getx() const {
14         return x;
15     }
16     GLint gety() const {
17         return y;
18     }
19     void incrementx() {
20         ++x;
21     }
22     void decrementy() {
23         --y;
24     }
25 };
26 
27 void setPixel(GLint xCoord, GLint yCoord)
28 {
29     glBegin(GL_POINTS);
30     glVertex2i(xCoord, yCoord);
31     glEnd();
32 }
33 
34 void circlePlotPoints(GLint xc, GLint yc, ScreenPt circPt)
35 {
36     setPixel(xc + circPt.getx(), yc + circPt.gety());
37     setPixel(xc - circPt.getx(), yc + circPt.gety());
38     setPixel(xc + circPt.getx(), yc - circPt.gety());
39     setPixel(xc - circPt.getx(), yc - circPt.gety());
40     setPixel(xc + circPt.gety(), yc + circPt.getx());
41     setPixel(xc - circPt.gety(), yc + circPt.getx());
42     setPixel(xc + circPt.gety(), yc - circPt.getx());
43     setPixel(xc - circPt.gety(), yc - circPt.getx());
44 }
45 
46 void circleMidpoint(GLint xc, GLint yc, GLint radius)
47 {
48     ScreenPt circPt;
49 
50     GLint p = 1 - radius;   //Initial value for    midpoint parameter
51 
52     circPt.setCoords(0, radius);  //Set coords for top point of circle.
53     // Plot the initial point in each circle quadrant
54     circlePlotPoints(xc, yc, circPt);
55     // Calculate next point and polt in each octant
56     while (circPt.getx() < circPt.gety()) {
57         circPt.incrementx();
58         if (p < 0) {
59             p += 2 * circPt.getx() + 1;
60         }
61         else {
62             circPt.decrementy();
63             p += 2 * (circPt.getx() - circPt.gety()) + 1;
64         }
65         circlePlotPoints(xc, yc, circPt);
66     }
67 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM