C Language Animation: A Beam of Light to Illuminate You

Are the heights of the two rectangles the same? Please adjust your seating position and distance, and carefully look back and forth with the code.C Language Animation: A Beam of Light to Illuminate YouThe light source is fixed at a certain position on the left, uniformly shining to the right, with equal spacing between each pair of adjacent white light beams. Two rectangles are added in the middle to highlight the permeability of light and enhance the visual effect.C Language Program Design:

#include "stdio.h"
#include "graphics.h"
#include "conio.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 LIGHT_START_X -320 // X coordinate of the light beam start
#define LIGHT_END_X 320 // X coordinate of the light beam end

#define LIGHT_INTERVAL 15 // Light beam interval

#define LIGHT_COORDINATE_MIN -240 // Minimum light coordinate
#define LIGHT_COORDINATE_MAX 240 // Maximum light coordinate

#define COLOUR_RED RGB(255,0,0)
#define COLOUR_ORANGE RGB(255, 140, 0)

void Light()
{
    initgraph(GRAPH_WIDTH, GRAPH_HEIGHT);
    setorigin(COORDINATE_ORIGIN_X, COORDINATE_ORIGIN_Y);

    for (int i = LIGHT_COORDINATE_MIN; i < LIGHT_COORDINATE_MAX; i += LIGHT_INTERVAL)
    {
        line(LIGHT_START_X, 0, LIGHT_END_X, i);
    }

    setfillcolor(COLOUR_ORANGE);
    fillrectangle(-250, -100, -200, 100);
    fillrectangle(150, -100, 100, 100);

    // Pause to view the effect, can also change to while(1);
    _getch();
    closegraph();
}

int main()
{
    Light();
    return 0;
}

Development Environment: Windows + Visual Studio 2022This example is for learning reference and entertainment purposes, mainly related to simple applications of the EasyX graphics library. Each program can be modified by changing macro sizes, rectangle coordinates, colors, and other parameters to achieve various desired effects. For example, changing the direction of the light source from right to left, the spacing between each light beam, the position and color of the rectangles, etc. For instance:C Language Animation: A Beam of Light to Illuminate YouA beam of light, like a lonely star in the night sky, brilliant and steadfast, illuminating the vast darkness. It comes from afar, gently brushing past the heart, melting away all sadness and troubles.

Leave a Comment