summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2017-03-22 15:25:32 +0100
committerEdward Welbourne <edward.welbourne@qt.io>2017-07-14 13:36:46 +0000
commit29af390e3f3503d8f57380823a6122ca259ed971 (patch)
tree5c35e4bf532ab23b24bbfe9a7b67a9c289ba26f7 /examples
parentfdfa3b1141a08aface04333e594213efe681ef6e (diff)
Clean up some arithmetic code in an example
The square root of a sum of squares is easier to read and should be computed more accurately if done for us by hypot(). Variables set only once should be set as an initializer and declared const, to make clear this is what's happening. Loop variables can be local to loops. Adding a value to, or subtracting one from, a multiple of itself just multiplies it be one plus (or minus) the multiplier; assigning the result to the same variable is clearer as a *= (especially when the factors are now overt numeric constants). An array of 16k floats all updated in locksteck to the same value can be replaced by a single float that holds that value. Simple things should not be needlessly made more complicated - especially in example code, which should be pedagogic. Change-Id: Idab585cd7df1399c250d4b9f1396a085ae8f3864 Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/opengl/legacy/pbuffers2/glwidget.cpp30
1 files changed, 13 insertions, 17 deletions
diff --git a/examples/opengl/legacy/pbuffers2/glwidget.cpp b/examples/opengl/legacy/pbuffers2/glwidget.cpp
index 447461dbb3..c710d03cab 100644
--- a/examples/opengl/legacy/pbuffers2/glwidget.cpp
+++ b/examples/opengl/legacy/pbuffers2/glwidget.cpp
@@ -302,31 +302,27 @@ void GLWidget::timerEvent(QTimerEvent *)
else if (!scale_in && scale < .5f)
scale_in = true;
- scale = scale_in ? scale + scale*0.01f : scale-scale*0.01f;
+ scale *= scale_in ? 1.01f : 0.99f;
rot_z += 0.3f;
rot_x += 0.1f;
- int dx, dy; // disturbance point
- float s, v, W;
- int i, j;
- static float wt[128][128];
+ static float wt = 0.0;
+ wt += 0.1f;
+
const int width = logo.width();
+ const int dx = width >> 1, dy = dx; // disturbance point
+ const float v = -4; // wave speed
+ const float W = .3f;
const int AMP = 5;
- dx = dy = width >> 1;
-
- W = .3f;
- v = -4; // wave speed
-
- for (i = 0; i < width; ++i) {
- for ( j = 0; j < width; ++j) {
- s = sqrt((double) ((j - dx) * (j - dx) + (i - dy) * (i - dy)));
- wt[i][j] += 0.1f;
- const double angle = 2 * M_PI * W * (wt[i][j] + s / v);
+ for (int i = 0; i < width; ++i) {
+ for (int j = 0; j < width; ++j) {
+ const float s = hypot(j - dx, i - dy);
+ const double raw = AMP * sin(2 * M_PI * W * (wt + s / v));
if (s != 0)
- wave[i * width + j] = AMP * sin(angle) / (0.2 * (s + 2));
+ wave[i * width + j] = raw / (0.2 * (s + 2));
else
- wave[i * width + j] = AMP * sin(angle);
+ wave[i * width + j] = raw;
}
}
}