Good luck! Encountering a rainbow signifies a turning point and rebirth after difficulties, and beauty is about to arrive. The seven colors of the rainbow, from the outside to the inside, are red, orange, yellow, green, blue, indigo, and violet. This order is fixed and is formed by the refraction and reflection of sunlight through water droplets in the air.
Now let’s implement this using the C language, as shown in the image above. The method is simple: draw 7 different colored semicircles (the seven colors of the rainbow from outside to inside), using the easyx graphics library, which is simple and convenient.
[C Language Program Design]:
#include "stdio.h"
#include "graphics.h"
#include "conio.h"
#define PI 3.1415926
#define GRAPH_WIDTH 640 // Window width
#define GRAPH_HEIGHT 480 // Window height
// Background color: sky blue
#define BACKGROUND_COLOUR (RGB(135, 206, 235))
#define COORDINATE_ORIGIN_X 320 // X coordinate of the origin
#define COORDINATE_ORIGIN_Y 240 // Y coordinate of the origin
#define RAINBOW_RADIUS_INIT_X -200
#define RAINBOW_RADIUS_INIT_Y 200
#define RADIUS_STEP 12
void Rainbow()
{
/* Color array: the 7 colors of the rainbow
* |0: Red |1: Orange |2: Yellow |3: Green |4: Blue
* |5: Indigo |6: Violet
*/
int color[7] = {RGB(255,0,0), RGB(255,128,0), RGB(255,255,0), RGB(0,255,0), RGB(0,0,255), RGB(75,0,130), RGB(128,0,128)};
initgraph(GRAPH_WIDTH, GRAPH_HEIGHT);
setorigin(COORDINATE_ORIGIN_X, COORDINATE_ORIGIN_Y);
setbkcolor(BACKGROUND_COLOUR);
cleardevice();
for (int i = RAINBOW_RADIUS_INIT_X, j = RAINBOW_RADIUS_INIT_Y, c = 0; c < 7; i += RADIUS_STEP, j -= RADIUS_STEP, c++)
{
setlinestyle(PS_SOLID, 13);
setlinecolor(color[c]);
arc(i, i, j, j, 0.0, PI);
}
// Pause to view the effect, can also use while(1);
_getch();
closegraph();
}
int main()
{
Rainbow();
return 0;
}
Development Environment: Windows + Visual Studio 2022
Seeing a rainbow sometimes reminds me of the line from “A Chinese Odyssey” where Zixia says, “My ideal man is a great hero, one day he will come riding on a rainbow to marry me. I guessed the beginning, but I couldn’t guess the ending.”