You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

垂直三角波绘制代码求助:修改振幅致图案错乱

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: totalRows is computed as 2*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) or drawOnePeriod(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 5 in drawWave with the amplitude parameter passed to it.
  • Update the loop in drawOnePeriod to run for 2*amplitude -1 rows instead of a fixed number.
  • Verify that the rise phase runs for amplitude rows and the fall phase runs for amplitude-1 rows.

内容的提问来源于stack exchange,提问作者Ahmed Mahmoud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:17:27