Drawing water ripples in C language: This is a static image captured during runtime, but it is actually animated. After running the code, each circle on the left and right continuously spins and expands, resembling the rippling effect of water. Please see the video effect at the bottom.Main idea: Set the origin coordinates, divide into left and right parts, and draw circles in two colors (here, red and blue are chosen for better visual layering, but it depends on personal preference). Draw 50 circles of each color, with the radius continuously expanding by a certain step size, creating a dynamic diffusion effect, and refresh the drawing interface in real-time.C language program design:
#include "stdio.h"
#include "graphics.h"
#include "conio.h"
#include "math.h"
#define GRAPH_WIDTH 640 // Width of the drawing window
#define GRAPH_HEIGHT 480 // Height of the drawing window
#define COORDINATE_ORIGIN_X 320 // X coordinate of the origin
#define COORDINATE_ORIGIN_Y 240 // Y coordinate of the origin
#define CENTER_CIRCLE_X1 120
#define CENTER_CIRCLE_X2 -120
#define CENTER_CIRCLE_Y 0
#define CIRCLES_NUM 50 // Number of spinning circles
#define RADIUS_STEPS 20 // Radius adjustment step size
#define RADIUS1(x,y) (RADIUS_STEPS * (y) + (x))
#define RADIUS2(x,y) (int)(fabs((RADIUS_STEPS * (y) - (x))))
void SpinInCircles()
{
initgraph(GRAPH_WIDTH, GRAPH_HEIGHT);
setorigin(COORDINATE_ORIGIN_X, COORDINATE_ORIGIN_Y);
setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 3);
BeginBatchDraw();
for (int i = 0;;i++)
{
setlinecolor(BLUE);
for (int j = 0; j < CIRCLES_NUM; j++) circle(CENTER_CIRCLE_X1, 0, RADIUS1(i, j));
setlinecolor(RED);
for (int j = 0; j < CIRCLES_NUM; j++) circle(CENTER_CIRCLE_X2, 0, RADIUS2(i, j));
if (0 == i % 20) i = 0;
Sleep(100);
FlushBatchDraw();
cleardevice();
}
EndBatchDraw();
closegraph();
}
int main()
{
SpinInCircles();
return 0;
}
Video:Operating environment: Windows Visual Studio 2022