纯C语言绘制动态三维圆

纯C语言绘制动态三维圆,第1张

纯C语言绘制动态三维圆

最近在网上翻到了有关用C语言在Windows环境下绘制动态圆的代码,很值得我们去借鉴。

众说周知,C/C++天生就是和黑窗口一对,只有字符。但运行下面的代码一定会惊艳到你。

注意:
        代码可能需要在编译选项中加入以下代码才能运行
-lgdi32
Code:
#include 
#include 
#define PI 3.1415926
#define SX 8
#define SY 16
#define DX PI / SX
#define DY PI * 2 / SY
#define X(a, b) (cx + v[a][b].x * r), (cy + v[a][b].y * r)
typedef struct { double x, y; } Vec;
void calc(double i, double j, double rot, Vec* v) {
    double x = sin(i) * cos(j), y = sin(i) * sin(j), z = cos(i),
        s = sin(rot), c = cos(rot), c1 = 1 - c, u = 1 / sqrt(3.0f), u2 = u * u;
    v->x = x * (c + u2 * c1) + y * (u2 * c1 - u * s) + z * (u2 * c1 + u * s);
    v->y = x * (u2 * c1 + u * s) + y * (c + u2 * c1) + z * (u2 * c1 - u * s);
}
int main() {
    HWND hwnd = GetConsoleWindow(); HDC hdc1 = GetDC(hwnd);
    double rot = 0;
    while(1) {
        RECT rect; GetClientRect(hwnd, &rect); int w = rect.right, h = rect.bottom, cx = w / 2, cy = h / 2, r = h * 0.375;
        HDC hdc2 = CreateCompatibleDC(hdc1); HBITMAP bmp = CreateCompatibleBitmap(hdc1, w, h); SelectObject(hdc2, bmp);
        SelectObject(hdc2, GetStockObject(WHITE_PEN));
        Vec v[SX + 1][SY + 1];
        for(int i = 0; i <= SX; ++i) for(int j = 0; j <= SY; ++j) calc(i * DX, j * DY, rot, &v[i][j]);
        for(int i = 0; i < SX; ++i) for(int j = 0; j < SY; ++j) {
            MoveToEx(hdc2, X(i, j), NULL); LineTo(hdc2, X(i + 1, j));
            MoveToEx(hdc2, X(i, j), NULL); LineTo(hdc2, X(i, j + 1));
        }
        BitBlt(hdc1, 0, 0, w, h, hdc2, 0, 0, SRCCOPY); DeleteObject(bmp); DeleteDC(hdc2);
        rot += 0.01; Sleep(10);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5713368.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存