neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
bresenham_counter.hpp
Go to the documentation of this file.
1// bresenham_counter.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39
40namespace neolib
41{
42 template <typename T>
44 {
45 /* operator T() returns x[0..N-1] = 0 .. R, i.e. x[n] = (R / (N-1)) * n, without
46 using floating point or multiplication/division each iteration */
47 public:
49 bresenham_counter(T range, T number) :
50 dx(number-1),
51 dy(dx > 0 ? range % dx : 0),
52 d(2*dy - dx), incrE(2*dy), incrNE(2*(dy-dx)),
53 incrCounter(dx > 0 ? range / dx : 0),
54 incrCounterPlus1(dx > 0 ? incrCounter+1 : 0),
55 counter(0) {}
56 bresenham_counter(T rangeStart, T rangeEnd, T number) :
57 dx(number-1),
58 dy(dx > 0 ? rangeEnd > rangeStart ?
59 (rangeEnd - rangeStart) % dx : (rangeStart - rangeEnd) % dx : 0),
60 d(2*dy - dx), incrE(2*dy), incrNE(2*(dy-dx)),
61 incrCounter(dx > 0 ? (rangeEnd - rangeStart) / dx : 0),
62 incrCounterPlus1(dx > 0 ? rangeEnd > rangeStart ?
63 incrCounter+1 : incrCounter-1 : 0),
64 counter(rangeStart) {}
65 void init(T range, T number)
66 {
67 dx = number-1;
68 dy = dx > 0 ? range % dx : 0;
69 d = 2*dy - dx;
70 incrE = 2*dy;
71 incrNE = 2*(dy-dx);
72 incrCounter = dx > 0 ? range / dx : 0;
73 incrCounterPlus1 = dx > 0 ? incrCounter+1 : 0;
74 counter = 0;
75 }
76 void init(T rangeStart, T rangeEnd, T number)
77 {
78 dx = number-1;
79 dy = dx > 0 ? rangeEnd > rangeStart ?
80 (rangeEnd - rangeStart) % dx : (rangeStart - rangeEnd) % dx : 0;
81 d = 2*dy - dx;
82 incrE = 2*dy;
83 incrNE = 2*(dy-dx);
84 incrCounter = dx > 0 ? (rangeEnd - rangeStart) / dx : 0;
85 incrCounterPlus1 = dx > 0 ? rangeEnd > rangeStart ?
86 incrCounter+1 : incrCounter-1 : 0;
87 counter = rangeStart;
88 }
89 operator T()
90 {
91 if (d <= 0)
92 {
93 d += incrE;
94 T v = counter;
95 counter += incrCounter;
96 return v;
97 }
98 else
99 {
100 d += incrNE;
101 T v = counter;
102 counter += incrCounterPlus1;
103 return v;
104 }
105 }
107 {
108 return operator T();
109 }
110 private:
111 T dx, dy, d, incrE, incrNE;
112 T incrCounter;
113 T incrCounterPlus1;
114 T counter;
115 };
116
117 template <typename T>
119 {
120 /* operator T() returns x[0..N-1] = 0 .. R, i.e. x[n] = (R / (N-1)) * n, without
121 using floating point or multiplication/division each iteration */
122 public:
124 bresenham_counter_alt(T range, T number) :
125 n(number-1),
126 partInt(n > 0 ? range / n : 0),
127 partFract(n > 0 ? range % n : 0),
128 e(0),
129 incrCounter(partInt),
130 incrCounterPlus1(n > 0 ? incrCounter+1 : 0),
131 counter(0) {}
132 bresenham_counter_alt(T rangeStart, T rangeEnd, T number) :
133 n(number-1),
134 partInt(n > 0 ? rangeEnd > rangeStart ?
135 (rangeEnd - rangeStart) / n : (rangeStart - rangeEnd) / n : 0),
136 partFract(n > 0 ? rangeEnd > rangeStart ?
137 (rangeEnd - rangeStart) % n : (rangeStart - rangeEnd) % n: 0),
138 e(0),
139 incrCounter(rangeEnd > rangeStart ? partInt : -partInt),
140 incrCounterPlus1(n > 0 ? rangeEnd > rangeStart ? incrCounter+1 : incrCounter-1 : 0),
141 counter(rangeStart) {}
142 void init(T range, T number)
143 {
144 n = number-1;
145 partInt = n > 0 ? range / n : 0;
146 partFract = n > 0 ? range % n : 0;
147 e = 0;
148 incrCounter = partInt;
149 incrCounterPlus1 = n > 0 ? incrCounter+1 : 0;
150 counter = 0;
151 }
152 void init(T rangeStart, T rangeEnd, T number)
153 {
154 n = number-1;
155 partInt = n > 0 ? rangeEnd > rangeStart ?
156 (rangeEnd - rangeStart) / n : (rangeStart - rangeEnd) / n : 0;
157 partFract = n > 0 ? rangeEnd > rangeStart ?
158 (rangeEnd - rangeStart) % n : (rangeStart - rangeEnd) % n: 0;
159 e = 0;
160 incrCounter = rangeEnd > rangeStart ? partInt : -partInt;
161 incrCounterPlus1 = n > 0 ? rangeEnd > rangeStart ? incrCounter+1 : incrCounter-1 : 0;
162 counter = rangeStart;
163 }
164 operator T()
165 {
166 e += partFract;
167 if (e < n)
168 {
169 T v = counter;
170 counter += incrCounter;
171 return v;
172 }
173 else
174 {
175 e -= n;
176 T v = counter;
177 counter += incrCounterPlus1;
178 return v;
179 }
180 }
182 {
183 return operator T();
184 }
185 private:
186 T n, partInt, partFract, e;
187 T incrCounter;
188 T incrCounterPlus1;
189 T counter;
190 };
191}
bresenham_counter_alt(T rangeStart, T rangeEnd, T number)
void init(T rangeStart, T rangeEnd, T number)
void init(T range, T number)
bresenham_counter(T range, T number)
bresenham_counter(T rangeStart, T rangeEnd, T number)
void init(T rangeStart, T rangeEnd, T number)