The Thirty-Six Questions of C Language: The Heavenly Fierce Star

Task: Draw a sine wave, a logarithmic spiral, and an arithmetic spiral.First, draw the mathematical two-dimensional coordinate axes and mark the scales accordingly on the axes.Then, draw a sine wave in red, a logarithmic spiral in green, and an arithmetic spiral in blue.EffectThe Thirty-Six Questions of C Language: The Heavenly Fierce StarSource Code

#define _CRT_SECURE_NO_WARNINGS#include <graphics.h>#include <math.h>#include <stdio.h>#include <conio.h>#define PI 3.1415926535
int main() {    initgraph(800, 600);    setbkcolor(BLACK);    cleardevice();
    // Draw coordinate axes    setlinecolor(YELLOW);    // x-axis    line(0, 300, 800, 300);    // y-axis    line(400, 0, 400, 600);
    // Draw arrows    line(795, 295, 800, 300);  // x-axis arrow    line(795, 305, 800, 300);    line(395, 5, 400, 0);      // y-axis arrow    line(405, 5, 400, 0);
    // Draw scales and labels    settextcolor(BLACK);    settextstyle(14, 0, _T("宋体"));
    // x-axis scales (mathematical range -10 to 10, 40 pixels per unit)    for (int x = -10; x <= 10; x += 2) {        if (x == 0) continue;        int screenX = 400 + x * 40;        line(screenX, 295, screenX, 305);        char text[10];        sprintf(text, "%d", x);        if (x > 0)            outtextxy(screenX - 5, 310, text);        else            outtextxy(screenX - 15, 310, text);    }
    // y-axis scales (mathematical range -7 to 7)    for (int y = -7; y <= 7; y += 2) {        if (y == 0) continue;        int screenY = 300 - y * 40;        line(395, screenY, 405, screenY);        char text[10];        sprintf(text, "%d", y);        outtextxy(410, screenY - 10, text);    }
    // Draw sine wave (red)    setlinecolor(WHITE);    for (double x = -10.0; x <= 10.0; x += 0.05) {        double y = 2 * sin(x * 2); // Adjust parameters to make the waveform visible        int screenX = 400 + (int)(x * 40);        int screenY = 300 - (int)(y * 40);        if (x == -10.0) moveto(screenX, screenY);        else lineto(screenX, screenY);    }
    // Draw logarithmic spiral (green)    setlinecolor(GREEN);    double r = 0.1;    double theta = 0.0;    bool first = true;    while (r < 15) { // Control the range of the spiral        double x = r * cos(theta);        double y = r * sin(theta);        int screenX = 400 + (int)(x * 40);        int screenY = 300 - (int)(y * 40);        if (first) {            moveto(screenX, screenY);            first = false;        }        else {            lineto(screenX, screenY);        }        r *= 1.03; // Geometric growth factor        theta += 0.1;    }
    // Draw arithmetic spiral (blue)    setlinecolor(BLUE);    r = 0.1;    theta = 0.0;    first = true;    while (r < 15) { // Control the range of the spiral        double x = r * cos(theta);        double y = r * sin(theta);        int screenX = 400 + (int)(x * 40);        int screenY = 300 - (int)(y * 40);        if (first) {            moveto(screenX, screenY);            first = false;        }        else {            lineto(screenX, screenY);        }        r += 0.08; // Arithmetic growth amount        theta += 0.1;    }
    _getch();    closegraph();    return 0;}

Leave a Comment