1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include <iostream> #include <vector> #include <cmath> #include <iomanip> // Constants const int GRID_SIZE = 20; // Size of the grid (GRID_SIZE x GRID_SIZE) const double DAMPING_FACTOR = 0.98; // Damping factor for ripple effect const double RIPPLE_STRENGTH = 10.0; // Initial strength of the ripple const int RIPPLE_RADIUS = 5; // Radius of the ripple effect const int NUM_STEPS = 30; // Number of simulation steps // Function to initialize the grid void initializeGrid(std::vector<std::vector<double>>& grid) { for (int i = 0; i < GRID_SIZE; ++i) { for (int j = 0; j < GRID_SIZE; ++j) { grid[i][j] = 0.0; } } } // Function to create a ripple effect void createRipple(std::vector<std::vector<double>>& grid, int centerX, int centerY, double strength) { for (int i = 0; i < GRID_SIZE; ++i) { for (int j = 0; j < GRID_SIZE; ++j) { double distance = std::sqrt((i - centerX) * (i - centerX) + (j - centerY) * (j - centerY)); if (distance < RIPPLE_RADIUS) { grid[i][j] += strength * (1.0 - distance / RIPPLE_RADIUS); } } } } // Function to update the grid void updateGrid(std::vector<std::vector<double>>& grid) { std::vector<std::vector<double>> newGrid = grid; for (int i = 1; i < GRID_SIZE - 1; ++i) { for (int j = 1; j < GRID_SIZE - 1; ++j) { newGrid[i][j] = (grid[i - 1][j] + grid[i + 1][j] + grid[i][j - 1] + grid[i][j + 1]) / 4.0; newGrid[i][j] *= DAMPING_FACTOR; } } grid = newGrid; } // Function to print the grid void printGrid(const std::vector<std::vector<double>>& grid) { for (const auto& row : grid) { for (double value : row) { std::cout << std::setw(6) << std::fixed << std::setprecision(2) << value << " "; } std::cout << "\n"; } std::cout << "\n"; } int main() { // Create a grid for the simulation std::vector<std::vector<double>> grid(GRID_SIZE, std::vector<double>(GRID_SIZE, 0.0)); // Initialize the grid initializeGrid(grid); // Create an initial ripple effect createRipple(grid, GRID_SIZE / 2, GRID_SIZE / 2, RIPPLE_STRENGTH); std::cout << "Initial Ripple Effect:\n"; printGrid(grid); // Simulate the ripple effect over time for (int step = 0; step < NUM_STEPS; ++step) { updateGrid(grid); std::cout << "Step " << step + 1 << ":\n"; printGrid(grid); } return 0; } |
Explanation
- Constants:
GRID_SIZE
: Size of the grid representing the water surface.DAMPING_FACTOR
: Factor by which the ripple strength decreases each step.RIPPLE_STRENGTH
: Initial strength of the ripple.RIPPLE_RADIUS
: Radius of the ripple effect.NUM_STEPS
: Number of simulation steps to run.
- initializeGrid Function:
- Initializes the grid with all zero values, representing a flat water surface.
- createRipple Function:
- Generates a ripple effect centered at
(centerX, centerY)
with the givenstrength
. - The ripple strength decreases with distance from the center, within the specified
RIPPLE_RADIUS
.
- Generates a ripple effect centered at
- updateGrid Function:
- Updates the grid based on the average values of adjacent cells and applies the damping factor.
- Simulates the spreading of the ripple over time.
- printGrid Function:
- Prints the current state of the grid, formatting the values for readability.
- Main Function:
- Initializes the grid and creates an initial ripple effect at the center of the grid.
- Prints the initial state and simulates the ripple effect over a specified number of steps, updating and printing the grid at each step.
Usage
- Grid Size: Represents the water surface as a 2D grid of cells.
- Ripple Effect: Simulates the effect of a ripple starting at the center of the grid and spreading outwards.
- Damping: Applies a damping factor to simulate the gradual decrease in ripple strength over time.