Java控制台显示sin(x)波形及沿Y轴垂直旋转90度的实现问询
Hey there! Let's tackle this problem of rendering sine waves in the Java console — both the standard horizontal version and the vertically rotated 90° variant. I'll break this down with practical, runnable code and explain how each part works.
1. Standard Horizontal Sine Wave
The core idea here is to calculate the sine value for a range of x-values, map that value (which sits between -1 and 1) to a column position in the console, then print a marker (like *) at that position with spaces filling the rest of the line.
Here's the code:
public class HorizontalSineWave { public static void main(String[] args) { int totalSteps = 100; // Controls the length of the wave (more steps = smoother) int consoleWidth = 80; // Width of our console output area int midpoint = consoleWidth / 2; // Baseline for the sine wave to center it for (int step = 0; step < totalSteps; step++) { // Calculate x from 0 to 4π (two full sine cycles) double x = step * (Math.PI * 4) / totalSteps; // Get the sine value (-1 to 1) double sinValue = Math.sin(x); // Map the sine value to a column position in the console int columnPosition = (int) (midpoint + sinValue * (midpoint - 1)); // Build each line: spaces up to the column, then our marker, then remaining spaces StringBuilder line = new StringBuilder(); for (int col = 0; col < consoleWidth; col++) { line.append(col == columnPosition ? '*' : ' '); } System.out.println(line); } } }
Quick breakdown:
- We loop through 100 steps to generate two full sine cycles (0 to 4π).
- The sine value is scaled to fit within our console width, centered at the midpoint so the wave doesn't hug one side.
- Each line represents a single x-value, with
*marking where the sine wave falls.
2. Vertically Rotated Sine Wave (90°)
Rotating the wave 90° means we're flipping the axes: now each column represents an x-value, and the row position corresponds to the sine value. We'll use a 2D grid to track where to place our markers, then print each row to see the vertical wave.
Here's the code:
public class RotatedSineWave { public static void main(String[] args) { int totalSteps = 100; // Length of the wave (columns in our grid) int consoleHeight = 20; // Height of our console output area int midpoint = consoleHeight / 2; // Baseline for vertical centering // Initialize a grid filled with spaces to represent our console area char[][] waveGrid = new char[consoleHeight][totalSteps]; for (int row = 0; row < consoleHeight; row++) { for (int col = 0; col < totalSteps; col++) { waveGrid[row][col] = ' '; } } for (int col = 0; col < totalSteps; col++) { double x = col * (Math.PI * 4) / totalSteps; double sinValue = Math.sin(x); // Map sine value to row position (flip the sign to get the correct rotation direction) int rowPosition = (int) (midpoint - sinValue * (midpoint - 1)); // Ensure we don't go out of grid bounds (prevents index errors from rounding) rowPosition = Math.max(0, Math.min(rowPosition, consoleHeight - 1)); // Place our marker at the calculated position waveGrid[rowPosition][col] = '*'; } // Print each row of the grid to render the rotated wave for (char[] row : waveGrid) { System.out.println(new String(row)); } } }
Quick breakdown:
- We use a 2D character grid to plan out where each
*should go before printing. - For each column (x-value), we calculate which row the sine value maps to, flipping the sign so the wave rotates correctly (without this, it would be mirrored).
- Finally, we print each row of the grid to see the vertical sine wave.
内容的提问来源于stack exchange,提问作者Peter

