Skip to content

Instantly share code, notes, and snippets.

@cjxgm
Last active August 29, 2015 14:07
Show Gist options
  • Save cjxgm/2f0ca59a0c2db6bdd558 to your computer and use it in GitHub Desktop.
Save cjxgm/2f0ca59a0c2db6bdd558 to your computer and use it in GitHub Desktop.
a sine wave oscillator without a std::sin call nor a sine table
#include <cstdlib>
#include <thread>
#include <chrono>
#include <iostream>
using std::cout;
using std::endl;
static void rep(char ch, int n)
{
while (n--) cout << ch;
}
static void draw_bar(double x, int width=40)
{
x = (x + 1.0) / 2.0;
int pos = x * width;
if (pos < 0) pos = 0;
if (pos >= width) pos = width-1;
auto halfw = width >> 1;
auto pre = std::min(pos, halfw);
auto ndot = std::abs(pos - halfw) + 1;
auto post = width - std::max(pos, halfw) - 1;
rep(' ', pre );
rep('*', ndot);
rep(' ', post);
}
struct spring
{
spring(double k=0.5, double velocity={})
: k{k}, lx{-velocity} {}
double pos() const noexcept { return x; }
void pluck(double nx) noexcept { x = nx; }
void mass(double nk) noexcept { k = nk; }
void update() noexcept
{
auto nx = (2-k)*x-lx;
lx = x;
x = nx;
}
void operator()() noexcept { update(); }
private:
double x{};
double k;
double lx;
};
int main()
{
spring s{0.001, 0.03};
for (auto step=0; ; step++) {
draw_bar(s.pos(), 60);
cout << step << ": " << s.pos() << endl;
s();
using namespace std::literals;
std::this_thread::sleep_for(30ms);
}
}
* 0: 0
* 1: 0.03
** 2: 0.05997
*** 3: 0.08988
**** 4: 0.1197
***** 5: 0.149401
****** 6: 0.178952
******* 7: 0.208324
******** 8: 0.237488
******** 9: 0.266414
********* 10: 0.295074
********** 11: 0.323439
*********** 12: 0.35148
************ 13: 0.37917
************* 14: 0.40648
************** 15: 0.433385
************** 16: 0.459856
*************** 17: 0.485867
**************** 18: 0.511392
***************** 19: 0.536405
***************** 20: 0.560883
****************** 21: 0.584799
******************* 22: 0.608131
******************* 23: 0.630854
******************** 24: 0.652947
********************* 25: 0.674387
********************* 26: 0.695152
********************** 27: 0.715222
*********************** 28: 0.734577
*********************** 29: 0.753198
************************ 30: 0.771065
************************ 31: 0.788161
************************* 32: 0.804469
************************* 33: 0.819972
************************** 34: 0.834656
************************** 35: 0.848505
************************** 36: 0.861505
*************************** 37: 0.873644
*************************** 38: 0.884909
*************************** 39: 0.895289
**************************** 40: 0.904774
**************************** 41: 0.913355
**************************** 42: 0.921021
**************************** 43: 0.927767
***************************** 44: 0.933585
***************************** 45: 0.93847
***************************** 46: 0.942416
***************************** 47: 0.945419
***************************** 48: 0.947478
***************************** 49: 0.948588
***************************** 50: 0.948751
***************************** 51: 0.947964
***************************** 52: 0.946229
***************************** 53: 0.943548
***************************** 54: 0.939924
***************************** 55: 0.93536
**************************** 56: 0.92986
**************************** 57: 0.923431
**************************** 58: 0.916078
**************************** 59: 0.907809
*************************** 60: 0.898632
*************************** 61: 0.888556
*************************** 62: 0.877592
************************** 63: 0.865751
************************** 64: 0.853043
************************** 65: 0.839483
************************* 66: 0.825083
************************* 67: 0.809858
************************ 68: 0.793823
************************ 69: 0.776995
*********************** 70: 0.759389
*********************** 71: 0.741024
********************** 72: 0.721918
********************** 73: 0.70209
********************* 74: 0.68156
******************** 75: 0.660348
******************** 76: 0.638476
******************* 77: 0.615966
****************** 78: 0.59284
****************** 79: 0.56912
***************** 80: 0.544832
**************** 81: 0.519999
*************** 82: 0.494645
*************** 83: 0.468798
************** 84: 0.442481
************* 85: 0.415722
************ 86: 0.388547
*********** 87: 0.360984
********** 88: 0.333059
********** 89: 0.304802
********* 90: 0.27624
******** 91: 0.247401
******* 92: 0.218315
****** 93: 0.189011
***** 94: 0.159518
**** 95: 0.129865
**** 96: 0.100082
*** 97: 0.0701998
** 98: 0.0402469
* 99: 0.0102538
** 100: -0.0197496
*** 101: -0.0497332
**** 102: -0.0796671
***** 103: -0.109521
****** 104: -0.139266
******* 105: -0.168871
******* 106: -0.198308
******** 107: -0.227546
********* 108: -0.256557
********** 109: -0.285311
*********** 110: -0.31378
************ 111: -0.341935
************* 112: -0.369748
************* 113: -0.397192
************** 114: -0.424238
*************** 115: -0.45086
**************** 116: -0.477031
***************** 117: -0.502725
***************** 118: -0.527916
****************** 119: -0.55258
******************* 120: -0.57669
******************** 121: -0.600225
******************** 122: -0.623159
********************* 123: -0.645469
********************** 124: -0.667135
********************** 125: -0.688133
*********************** 126: -0.708443
*********************** 127: -0.728044
************************ 128: -0.746918
************************ 129: -0.765045
************************* 130: -0.782406
************************* 131: -0.798985
************************** 132: -0.814765
************************** 133: -0.829731
*************************** 134: -0.843867
*************************** 135: -0.857158
**************************** 136: -0.869593
**************************** 137: -0.881158
**************************** 138: -0.891842
***************************** 139: -0.901634
***************************** 140: -0.910525
***************************** 141: -0.918505
***************************** 142: -0.925566
***************************** 143: -0.931702
****************************** 144: -0.936906
****************************** 145: -0.941173
****************************** 146: -0.944499
****************************** 147: -0.946881
****************************** 148: -0.948315
****************************** 149: -0.948802
****************************** 150: -0.948339
****************************** 151: -0.946929
****************************** 152: -0.944571
****************************** 153: -0.941269
****************************** 154: -0.937025
***************************** 155: -0.931845
***************************** 156: -0.925732
***************************** 157: -0.918694
***************************** 158: -0.910737
***************************** 159: -0.90187
**************************** 160: -0.8921
**************************** 161: -0.881439
**************************** 162: -0.869896
*************************** 163: -0.857483
*************************** 164: -0.844212
************************** 165: -0.830098
************************** 166: -0.815153
************************* 167: -0.799393
************************* 168: -0.782834
************************ 169: -0.765492
************************ 170: -0.747384
*********************** 171: -0.72853
*********************** 172: -0.708946
********************** 173: -0.688654
********************** 174: -0.667673
********************* 175: -0.646024
******************** 176: -0.623729
******************** 177: -0.600811
******************* 178: -0.577291
****************** 179: -0.553195
***************** 180: -0.528545
***************** 181: -0.503367
**************** 182: -0.477685
*************** 183: -0.451526
************** 184: -0.424915
************* 185: -0.397879
************* 186: -0.370445
************ 187: -0.342641
*********** 188: -0.314494
********** 189: -0.286033
********* 190: -0.257286
******** 191: -0.228281
******* 192: -0.199048
******* 193: -0.169616
****** 194: -0.140015
***** 195: -0.110273
**** 196: -0.0804214
*** 197: -0.0504891
** 198: -0.0205064
* 199: 0.00949685
** 200: 0.0394906
*** 201: 0.0694449
*** 202: 0.0993297
**** 203: 0.129115
***** 204: 0.158772
****** 205: 0.188269
******* 206: 0.217578
******** 207: 0.24667
********* 208: 0.275515
********** 209: 0.304085
********** 210: 0.33235
*********** 211: 0.360283
************ 212: 0.387856
************* 213: 0.415041
************** 214: 0.441811
*************** 215: 0.468139
*************** 216: 0.493999
**************** 217: 0.519365
***************** 218: 0.544212
****************** 219: 0.568514
****************** 220: 0.592248
******************* 221: 0.61539
******************** 222: 0.637916
******************** 223: 0.659805
********************* 224: 0.681033
********************** 225: 0.701581
********************** 226: 0.721427
*********************** 227: 0.740551
*********************** 228: 0.758935
************************ 229: 0.77656
************************ 230: 0.793409
************************* 231: 0.809464
************************* 232: 0.824709
************************** 233: 0.83913
************************** 234: 0.852712
************************** 235: 0.865441
*************************** 236: 0.877304
*************************** 237: 0.888291
*************************** 238: 0.898389
**************************** 239: 0.907588
**************************** 240: 0.91588
**************************** 241: 0.923257
**************************** 242: 0.929709
***************************** 243: 0.935233
***************************** 244: 0.939821
***************************** 245: 0.943469
***************************** 246: 0.946173
***************************** 247: 0.947932
***************************** 248: 0.948742
***************************** 249: 0.948604
***************************** 250: 0.947517
***************************** 251: 0.945483
***************************** 252: 0.942503
***************************** 253: 0.938581
***************************** 254: 0.93372
**************************** 255: 0.927925
**************************** 256: 0.921203
**************************** 257: 0.913559
**************************** 258: 0.905002
*************************** 259: 0.89554
*************************** 260: 0.885182
*************************** 261: 0.873939
************************** 262: 0.861822
************************** 263: 0.848843
************************** 264: 0.835015
************************* 265: 0.820353
************************* 266: 0.80487
************************ 267: 0.788582
************************ 268: 0.771506
*********************** 269: 0.753658
*********************** 270: 0.735056
********************** 271: 0.715719
********************* 272: 0.695667
********************* 273: 0.674919
******************** 274: 0.653496
******************* 275: 0.63142
******************* 276: 0.608712
****************** 277: 0.585395
***************** 278: 0.561493
***************** 279: 0.53703
**************** 280: 0.512029
*************** 281: 0.486517
************** 282: 0.460518
************** 283: 0.434058
************* 284: 0.407164
************ 285: 0.379864
*********** 286: 0.352183
********** 287: 0.32415
********* 288: 0.295793
********* 289: 0.26714
******** 290: 0.23822
******* 291: 0.209062
****** 292: 0.179695
***** 293: 0.150148
**** 294: 0.120451
*** 295: 0.0906336
** 296: 0.0607255
* 297: 0.0307566
* 298: 0.000756992
** 299: -0.0292434
*** 300: -0.0592145
**** 301: -0.0891264
***** 302: -0.118949
****** 303: -0.148653
******* 304: -0.178208
******** 305: -0.207585
********* 306: -0.236755
********* 307: -0.265687
********** 308: -0.294354
*********** 309: -0.322727
************ 310: -0.350777
************* 311: -0.378476
************** 312: -0.405796
************** 313: -0.432711
*************** 314: -0.459193
**************** 315: -0.485216
***************** 316: -0.510754
****************** 317: -0.535781
****************** 318: -0.560272
******************* 319: -0.584203
******************** 320: -0.60755
******************** 321: -0.630289
********************* 322: -0.652398
********************** 323: -0.673854
********************** 324: -0.694637
*********************** 325: -0.714725
************************ 326: -0.734098
************************ 327: -0.752737
************************* 328: -0.770623
************************* 329: -0.787739
************************** 330: -0.804067
************************** 331: -0.819591
*************************** 332: -0.834295
*************************** 333: -0.848166
*************************** 334: -0.861187
**************************** 335: -0.873348
**************************** 336: -0.884636
**************************** 337: -0.895038
***************************** 338: -0.904546
***************************** 339: -0.913149
***************************** 340: -0.920839
***************************** 341: -0.927608
****************************** 342: -0.93345
****************************** 343: -0.938358
****************************** 344: -0.942328
****************************** 345: -0.945355
****************************** 346: -0.947437
****************************** 347: -0.948572
****************************** 348: -0.948758
****************************** 349: -0.947995
****************************** 350: -0.946285
****************************** 351: -0.943628
****************************** 352: -0.940027
****************************** 353: -0.935487
***************************** 354: -0.93001
***************************** 355: -0.923604
***************************** 356: -0.916275
***************************** 357: -0.908029
**************************** 358: -0.898875
**************************** 359: -0.888822
**************************** 360: -0.87788
*************************** 361: -0.86606
*************************** 362: -0.853375
*************************** 363: -0.839836
************************** 364: -0.825457
************************** 365: -0.810252
************************* 366: -0.794238
************************* 367: -0.777429
************************ 368: -0.759843
************************ 369: -0.741497
*********************** 370: -0.722409
*********************** 371: -0.702599
********************** 372: -0.682086
********************* 373: -0.660892
********************* 374: -0.639036
******************** 375: -0.616542
******************* 376: -0.59343
******************* 377: -0.569726
****************** 378: -0.545451
***************** 379: -0.520632
**************** 380: -0.495291
**************** 381: -0.469456
*************** 382: -0.44315
************** 383: -0.416402
************* 384: -0.389238
************ 385: -0.361684
************ 386: -0.333768
*********** 387: -0.305519
********** 388: -0.276964
********* 389: -0.248132
******** 390: -0.219052
******* 391: -0.189753
****** 392: -0.160264
***** 393: -0.130615
***** 394: -0.100835
**** 395: -0.0709547
*** 396: -0.0410032
** 397: -0.0110107
* 398: 0.0189927
** 399: 0.0489772
*** 400: 0.0789128
**** 401: 0.108769
***** 402: 0.138517
****** 403: 0.168127
****** 404: 0.197568
******* 405: 0.226811
******** 406: 0.255828
********* 407: 0.284589
********** 408: 0.313065
*********** 409: 0.341229
************ 410: 0.369051
************ 411: 0.396504
************* 412: 0.423561
************** 413: 0.450194
*************** 414: 0.476376
^C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment