Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
BresenhamCircle.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __BresenhamCircle_hh__
27 #define __BresenhamCircle_hh__
28 #include "bias_config.h"
29 
30 namespace BIAS {
31 
32  /** @brief Scans a circle using Bresenham's integer arithmetic algorithm.
33  *
34  * - Either Init() or the according constructor have to be called before
35  * GetNext()
36  * - Points on circle are not given in continous manner, i.e. subsequent
37  * calls to GetNext() return (x,y), (y,x), (x,-y), (y,-x),
38  * (-x,-y), (-y,-x), (-x,y), (-y,x)
39  * - Fast
40  * @ingroup g_image_other
41  * @author fw, clean-up + refactoring by rwulff 02/2010
42  */
44 
45  public:
46 
47  /**
48  * @brief Default constructor. You have to call Init() by yourself before
49  * calling GetNext().
50  */
51  inline BresenhamCircle() {
52  }
53 
54 
55  /**
56  * @brief Constructor.
57  *
58  * @param center
59  * the center for this BresenhamCircle
60  *
61  * @param radius
62  * the radius for this BresenhamCircle
63  */
64  inline BresenhamCircle(int center[2], int radius) {
65  Init(center, radius);
66  }
67 
68  /**
69  * @brief Destructor.
70  */
71  inline ~BresenhamCircle() {
72  }
73 
74 
75  /**
76  * @brief Initialises this BresenhamCircle with new center and radius.
77  *
78  * @param center
79  * the new center for this BresenhamCircle
80  *
81  * @param radius
82  * the new radius for this BresenhamCircle
83  */
84  inline void Init(int center[2], int radius) {
85  center_[0] = center[0];
86  center_[1] = center[1];
87  radius_ = radius;
88 
89  current_[0] = 0;
90  current_[1] = radius_;
91 
92  d_ = 1 - radius_;
93  deltaE_ = 3;
94  deltaSE_ = -2 * radius_+5;
95 
96  // let GetNext return the starting point first
97  current_[0]--;
98  if (d_ >= 0) {
99  current_[1]++;
100  }
101  count_ = 0;
102 
103 #ifdef BIAS_DEBUG
104  initialized_ = true;
105 #endif
106  }
107 
108 
109  /**
110  * @brief Returns the coordinate of the next point on the circle.
111  *
112  * Can be called in a while loop:
113  *
114  * int nextCoords[2];
115  * while (bresCirc.GetNext(nextCoords)){
116  * // draw nextCoords in image
117  * }
118  *
119  * @param next
120  * the returned coordinate of the next point on the circle
121  *
122  * @return true if there are still points left on the circle or
123  * false if the circle is finished
124  */
125  inline bool GetNext(int next[2]) {
126 #ifdef BIAS_DEBUG
127  if (!initialized_)
128  BIASERR("call Init() before GetNext()");
129 #endif
130 
131  bool result=true;
132 
133  if (count_ == 0){
134  if (current_[1] > current_[0]){
135  if (d_ < 0){
136  d_ += deltaE_;
137  deltaE_ += 2;
138  deltaSE_ += 2;
139  } else {
140  d_ += deltaSE_;
141  deltaE_ += 2;
142  deltaSE_ += 4;
143  current_[1]--;
144  }
145  current_[0]++;
146  next[0] = current_[0] + center_[0];
147  next[1] = current_[1] + center_[1];
148  count_ = 1;
149  }
150  } else {
151  next[0] = center_[0];
152  next[1] = center_[1];
153 
154  switch (count_){
155  case 1:
156  next[0] += current_[1];
157  next[1] += current_[0];
158  break;
159  case 2:
160  next[0] += current_[0];
161  next[1] -= current_[1];
162  break;
163  case 3:
164  next[0] += current_[1];
165  next[1] -= current_[0];
166  break;
167  case 4:
168  next[0] -= current_[0];
169  next[1] -= current_[1];
170  break;
171  case 5:
172  next[0] -= current_[1];
173  next[1] -= current_[0];
174  break;
175  case 6:
176  next[0] -= current_[0];
177  next[1] += current_[1];
178  break;
179  case 7:
180  next[0] -= current_[1];
181  next[1] += current_[0];
182  break;
183  }
184 
185  count_++;
186  if (count_ == 8) {
187  count_=0;
188  }
189 
190  if (count_ == 0 && current_[1] <= current_[0]) {
191 #ifdef BIAS_DEBUG
192  initialized_ = false;
193 #endif
194  result = false;
195  }
196  }
197  return result;
198  }
199 
200 
201  protected:
202 
203  int center_[2]; // center coo
204  int radius_;
205  int current_[2];
206  int d_;
207  int deltaE_;
208  int deltaSE_;
209  int count_;
210 
211 #ifdef BIAS_DEBUG
213 #endif
214  };
215 
216 } // namespace
217 
218 #endif // __BresenhamCircle_hh__
Scans a circle using Bresenham&#39;s integer arithmetic algorithm.
BresenhamCircle(int center[2], int radius)
Constructor.
bool GetNext(int next[2])
Returns the coordinate of the next point on the circle.
void Init(int center[2], int radius)
Initialises this BresenhamCircle with new center and radius.
~BresenhamCircle()
Destructor.
BresenhamCircle()
Default constructor.