How to realize Wanhua Simulator with C++
This article introduces the knowledge of "how to use C++ to achieve Wanhua Simulator". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Do you still remember the ten thousand flower ruler you played when you were a child? Have fun, a variety of different point distances can draw a variety of shapes.
C language program ten thousand flower ruler simulation
Function function: randomly generate Wanhua graphics every 5 seconds and automatically save drawing parameters and graphics to the current directory, the image format is jpg
Tip:
The range of relevant parameters can be set by yourself, and the drawing size range can be set with the custom mySetRand function.
Note:
The inner circle radius cannot be larger than the outer circle radius, and the drawing point of the inner circle cannot be larger than the inner circle radius.
/ * the following is the complete code, which can be customized to change parameters * /
# include # include int mySetRand (int num, int min, int max); # define PI 3.1415926536 int main () {int k = 0; wchar_t s [255] = {NULL}; int R; / / static circle radius int r; / / moving circle radius int dr / / the distance from the drawing point on the moving circle to the center of the circle while (1) {/ / generate a random number between 100,395 R = mySetRand (1,100,395); r = R-mySetRand (1,5,95); dr = r-mySetRand (1m ~ 2, r ~ m ~ 5); / / calculate the number of execution (minimum common multiple / static circle radius) int m = R, n = r; int tmp; while (n! = 0) {tmp = m% n% M = n; n = tmp;} double maxdegree = r / m * 2 * PI; / / screen clearing / / cleardevice (); system ("cls"); initgraph (800,610); setbkcolor (RGB (200,200,200)); / / set the drawing origin coordinates to draw setorigin (400,300) in the center of the window; / / start drawing int x, y; for (double degree = 0; degree)
< maxdegree; degree += 0.01) { x = (int)(dr * cos(degree*(double(R) / r - 1)) + (R - r) * cos(degree)); y = (int)(dr * sin(degree*(double(R) / r - 1)) - (R - r) * sin(degree)); putpixel(x, y, RGB(255,255,0)); } //归位原点坐标 setorigin(0, 0); _stprintf_s(s, _T("作图参数:外圆半径%.3d像素\\内圆半径%.3d像素\\作图点到圆心距离%.3d像素"), R, r, dr); settextstyle(20, 0, _T("黑体")); settextcolor(RED); outtextxy(60, 587, s); //保存图形到当前目录 k++; _stprintf_s(s, _T("%.6d.jpg"), k); saveimage(s); //画面停留5秒后,清屏重新画另一个图 Sleep(5000); closegraph(); } return 0; } //指定范围随机数产生 //num产生个数,产生范围min到max(包括min和max) int mySetRand(int num, int min, int max) { int *p_num = # int *p_min = &min; int *p_max = &max; int a = 0; int p1 = 0, p2 = 0; if ((*p_num)*p_max)) { printf("最大值与最小值反过来了?\n元素个数为负数了?"); getchar(); return -1; } srand((unsigned int)time(0)); for (size_t i = 0; i < *p_num; i++) { a = rand(); if (*p_min == 0) { p2 = a % (*p_max + 1); } else if (*p_min>0) {p1 = a / (* p_max-* p_min + 1); p2 = a-p1* (* p_max-* p_min + 1) + * pmin;} else {printf ("processing error!"); getchar (); return-2;} if (p2 > * p_max | | p2