SHOW:
|
|
- or go back to the newest paste.
| 1 | /** | |
| 2 | - | SineWaver.cpp |
| 2 | + | SineWaver.cpp - now with a #define! |
| 3 | ||
| 4 | This program will draw sine waves that look like this: | |
| 5 | ||
| 6 | ,-'`'-, ,-'`'-, | |
| 7 | / \ / | |
| 8 | / \ / | |
| 9 | / \ / | |
| 10 | `'-.-'` `'-.-'` | |
| 11 | ||
| 12 | ,-'`'-, | |
| 13 | / \ | |
| 14 | `'-.-'` `'-.-'` | |
| 15 | ||
| 16 | The program requires two arguments: how many periods to | |
| 17 | draw and the height of the wave. I might program in a | |
| 18 | "cosine" option to change whether the wave draws up | |
| 19 | or down to begin with. Ooh, and perhaps cross them too! | |
| 20 | */ | |
| 21 | #include <stdio.h> | |
| 22 | #include <stdlib.h> | |
| 23 | #include <iostream> | |
| 24 | #include <string> | |
| 25 | ||
| 26 | #define CURVEOFFSET 2 | |
| 27 | ||
| 28 | char upCurve='/'; | |
| 29 | char dnCurve='\\'; | |
| 30 | std::string peakPattern=",-'`'-,"; | |
| 31 | std::string valiPattern="`'-.-'`"; | |
| 32 | ||
| 33 | class Wave | |
| 34 | {
| |
| 35 | private: | |
| 36 | int qPeriod; | |
| 37 | int height; | |
| 38 | int waveX; | |
| 39 | int waveY; | |
| 40 | public: | |
| 41 | Wave(int qperFeed, int hiFeed) : qPeriod(qperFeed), height(hiFeed), waveX(0), waveY(hiFeed) | |
| 42 | {
| |
| 43 | // derive the dimensions of the wave which will result | |
| 44 | ||
| 45 | // we need to check if the lengths of the peak and valley are equal | |
| 46 | int peakLen=peakPattern.length(); | |
| 47 | int valiLen=valiPattern.length(); | |
| 48 | if (!(peakLen==valiLen)) {
| |
| 49 | fprintf(stderr,"Internal error: mismatch between peak and valley string patterns\n"); | |
| 50 | exit(4); | |
| 51 | } | |
| 52 | ||
| 53 | // error checking passed. let's find out how wide our curve will be. | |
| 54 | for (int i=0;i<qPeriod;i++) | |
| 55 | {
| |
| 56 | switch (i%4) | |
| 57 | - | case 1: waveX+=(height-2); break; |
| 57 | + | |
| 58 | case 0: waveX+=valiLen; break; | |
| 59 | - | case 3: waveX+=(height-2); break; |
| 59 | + | case 1: waveX+=(height-CURVEOFFSET); break; |
| 60 | case 2: waveX+=peakLen; break; | |
| 61 | case 3: waveX+=(height-CURVEOFFSET); break; | |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 | //printf("waveX = %d\nwaveY = %d\n",waveX,waveY);
| |
| 66 | } | |
| 67 | ||
| 68 | void draw() | |
| 69 | {
| |
| 70 | std::string thisRow; | |
| 71 | char thisChar; | |
| 72 | for (int q=1; q<=waveY; q++) | |
| 73 | {
| |
| 74 | thisRow=""; | |
| 75 | - | int charPos = r%(peakPattern.length()+2*(height-2)+valiPattern.length())+1; |
| 75 | + | |
| 76 | {
| |
| 77 | int charPos = r%(peakPattern.length()+2*(height-CURVEOFFSET)+valiPattern.length())+1; | |
| 78 | //fprintf(stderr,"%dx%d: %d\n",q,r,charPos); | |
| 79 | - | if ((charPos > valiPattern.length() + (height-2)) && |
| 79 | + | |
| 80 | - | (charPos <= valiPattern.length() + (height-2) + peakPattern.length())) |
| 80 | + | |
| 81 | - | thisChar=peakPattern[(charPos-peakPattern.length()-(height-2))-1]; |
| 81 | + | if ((charPos > valiPattern.length() + (height-CURVEOFFSET)) && |
| 82 | (charPos <= valiPattern.length() + (height-CURVEOFFSET) + peakPattern.length())) | |
| 83 | thisChar=peakPattern[(charPos-peakPattern.length()-(height-CURVEOFFSET))-1]; | |
| 84 | else | |
| 85 | thisChar=' '; | |
| 86 | } | |
| 87 | else if (q==waveY) | |
| 88 | {
| |
| 89 | if (charPos <= valiPattern.length()) | |
| 90 | thisChar=valiPattern[charPos-1]; | |
| 91 | else | |
| 92 | thisChar=' '; | |
| 93 | } | |
| 94 | else | |
| 95 | {
| |
| 96 | if ((charPos > valiPattern.length()) && (charPos <= (valiPattern.length() + height-2)) && | |
| 97 | - | else if (charPos > (valiPattern.length() + (height-2) + peakPattern.length()) && |
| 97 | + | |
| 98 | - | (charPos - (valiPattern.length() + (height-2) + peakPattern.length()) == (q-1))) |
| 98 | + | |
| 99 | else if (charPos > (valiPattern.length() + (height-CURVEOFFSET) + peakPattern.length()) && | |
| 100 | (charPos - (valiPattern.length() + (height-CURVEOFFSET) + peakPattern.length()) == (q-1))) | |
| 101 | thisChar=dnCurve; | |
| 102 | else | |
| 103 | thisChar=' '; | |
| 104 | } | |
| 105 | thisRow+=thisChar; | |
| 106 | } | |
| 107 | std::cout << thisRow << std::endl; | |
| 108 | } | |
| 109 | } | |
| 110 | ||
| 111 | int GetQPeriod() { return qPeriod; }
| |
| 112 | int GetHeight() { return height; }
| |
| 113 | }; | |
| 114 | ||
| 115 | int main(int argc, char* argv[]) | |
| 116 | {
| |
| 117 | int noQuarterPeriod; | |
| 118 | int waveHeight; | |
| 119 | if (argc < 3) | |
| 120 | {
| |
| 121 | - | "Usage: SineWaver #-of-quarter-periods wave-height\n"); |
| 121 | + | |
| 122 | "This program requires at least two arguments.\n" | |
| 123 | "Usage: SineWaver [#-of-quarter-periods] [wave-height]\n"); | |
| 124 | exit(1); | |
| 125 | } | |
| 126 | else | |
| 127 | {
| |
| 128 | noQuarterPeriod = atoi(argv[1]); | |
| 129 | if (noQuarterPeriod < 1) | |
| 130 | {
| |
| 131 | fprintf(stderr, | |
| 132 | "The number of quarter periods must be a positive number!\n" | |
| 133 | "You gave me %d instead.\n",noQuarterPeriod); | |
| 134 | exit(2); | |
| 135 | } | |
| 136 | waveHeight = atoi(argv[2]); | |
| 137 | if (waveHeight < 2) | |
| 138 | {
| |
| 139 | fprintf(stderr, | |
| 140 | "The wave height is too short! The minimum is 2.\n" | |
| 141 | "You gave me a height of %d instead.\n",waveHeight); | |
| 142 | exit(3); | |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | // We'll make a pretty message by smartly analyzing how many periods we have. | |
| 147 | - | std::string initPattern="Generating wave with "; |
| 147 | + | |
| 148 | int nQPM=(noQuarterPeriod%4); | |
| 149 | std::string initPattern="Generating wave with a height of %d and "; | |
| 150 | if (noQuarterPeriod < 4) | |
| 151 | {
| |
| 152 | noFullPeriod=0; | |
| 153 | initPattern+="\%d/\%d period"; | |
| 154 | } | |
| 155 | else if (nQPM) | |
| 156 | {
| |
| 157 | noFullPeriod=noQuarterPeriod/4; | |
| 158 | initPattern+="\%d \%d/\%d periods"; | |
| 159 | } | |
| 160 | else {
| |
| 161 | noFullPeriod=noQuarterPeriod/4; | |
| 162 | initPattern+="\%d period"; | |
| 163 | if (noFullPeriod>1) { initPattern+="s";}
| |
| 164 | - | printf(initPattern.c_str(), |
| 164 | + | |
| 165 | initPattern+="...\n"; | |
| 166 | printf(initPattern.c_str(),waveHeight, | |
| 167 | (noFullPeriod)?noFullPeriod:(nQPM==2)?1:nQPM, | |
| 168 | (noFullPeriod)?((nQPM==2)?1:nQPM):(nQPM==2)?2:4, | |
| 169 | (nQPM==2)?2:4); | |
| 170 | Wave sineWave(noQuarterPeriod,waveHeight); | |
| 171 | sineWave.draw(); | |
| 172 | ||
| 173 | return 0; | |
| 174 | } |