垂直三角波绘制代码求助:修改振幅致图案错乱
Fixing Vertical Triangle Wave Distortion When Changing Amplitude
Hey there! Let's dig into why adjusting the amplitude is breaking your vertical triangle wave. The issue almost certainly comes from hardcoded values tied to the original amplitude of 5 in your drawWave function—when you change the amplitude, those fixed values don't scale with it, leading to misaligned rows and messed-up symmetry.
Common Causes of the Problem
Here are the most likely issues in your original code:
- You're using a fixed number of rows (like 9, which is
2*5-1) instead of calculating rows based on the input amplitude. - The spacing or star count calculations rely on the constant 5 instead of the passed amplitude parameter.
- The rise/fall phases of the triangle aren't scaled dynamically with the amplitude.
Corrected Code Example
Let's rewrite the functions to use the amplitude parameter everywhere, so the wave scales correctly no matter what value you pass:
#include <iostream> using namespace std; // Draws a single line of the vertical triangle wave void drawWaveLine(int amplitude, int currentRow) { int spaces, stars; // Rise phase: from bottom (or top) to peak if (currentRow < amplitude) { spaces = amplitude - currentRow - 1; stars = currentRow * 2 + 1; } // Fall phase: from peak back down else { spaces = currentRow - amplitude + 1; stars = (2 * amplitude - 1) - (currentRow - amplitude + 1) * 2; } // Print leading spaces for (int i = 0; i < spaces; ++i) { cout << " "; } // Print the wave segment for (int i = 0; i < stars; ++i) { cout << "*"; } cout << endl; } // Draws one full period of the vertical triangle wave void drawOnePeriod(int amplitude) { // Total rows in one period: 2*amplitude - 1 (rise takes amplitude rows, fall takes amplitude-1) const int totalRows = 2 * amplitude - 1; for (int row = 0; row < totalRows; ++row) { drawWaveLine(amplitude, row); } } int main() { int userAmplitude; cout << "Enter amplitude value: "; cin >> userAmplitude; drawOnePeriod(userAmplitude); return 0; }
How This Works
- Dynamic Row Calculation:
totalRowsis computed as2*amplitude -1—this ensures the wave has the right height for any amplitude (e.g., amplitude 3 gives 5 rows, amplitude 4 gives 7 rows). - Scaled Spacing & Stars: For each row, we calculate spaces and stars based on whether we're in the rise or fall phase, using the input amplitude instead of a fixed 5. This keeps the wave symmetric at any size.
- Flexible Amplitude: You can now pass any positive integer to
drawOnePeriod(e.g.,drawOnePeriod(3)ordrawOnePeriod(7)) and get a clean, correctly shaped vertical triangle wave.
Quick Check for Your Original Code
If you want to fix your existing code instead of rewriting, look for these fixes:
- Replace every hardcoded
5indrawWavewith the amplitude parameter passed to it. - Update the loop in
drawOnePeriodto run for2*amplitude -1rows instead of a fixed number. - Verify that the rise phase runs for
amplituderows and the fall phase runs foramplitude-1rows.
内容的提问来源于stack exchange,提问作者Ahmed Mahmoud
相关产品推荐
相关产品推荐

