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