当前位置:网站首页>Drawing dynamic 3D circle with pure C language
Drawing dynamic 3D circle with pure C language
2022-07-05 05:20:00 【Korloa】
Recently, I found something about using C Language in Windows Environment to draw dynamic circle code , It is worth learning from .
Everyone knows ,C/C++ Born with a black window , Only characters . But running the following code will definitely surprise you .
Be careful :
The code may need to add the following code to the compilation options to run
-lgdi32
Code:
#include <math.h>
#include <windows.h>
#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);
}
}
边栏推荐
- 64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
- Introduction to tools in TF-A
- Optimization scheme of win10 virtual machine cluster
- cocos2dx_ Lua particle system
- Haut OJ 1241: League activities of class XXX
- Download and use of font icons
- [merge array] 88 merge two ordered arrays
- What is the agile proportion of PMP Exam? Dispel doubts
- 动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
- Programmers' experience of delivering takeout
猜你喜欢
第六章 数据流建模—课后习题
【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
[转]MySQL操作实战(一):关键字 & 函数
YOLOv5添加注意力機制
Web APIs DOM node
Heap sort summary
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Optimization scheme of win10 virtual machine cluster
Research on the value of background repeat of background tiling
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
随机推荐
嵌入式数据库开发编程(零)
A three-dimensional button
[depth first search] 695 Maximum area of the island
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
Introduction to tools in TF-A
Research on the value of background repeat of background tiling
[转]MySQL操作实战(三):表联结
Count sort
PMP考试敏捷占比有多少?解疑
记录QT内存泄漏的一种问题和解决方案
win下一键生成当日的时间戳文件
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
To the distance we have been looking for -- film review of "flying house journey"
GBase数据库助力湾区数字金融发展
A new micro ORM open source framework
Shell Sort
[speed pointer] 142 circular linked list II
[to be continued] [UE4 notes] L1 create and configure items
Simple modal box
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column