慢羊羊的空间

无为,无我,无欲,居下,清虚,自然

做个纪念:椭圆组合而成的抽象图案 铜牌收录

每次看到这个程序我就会想起高中时曾那么痴迷编程。当时看到一本书的封面有这个图案,就用那个时候学的 QuickBasic 写了这个程序。现在移植到 VC 上,做个纪念吧。

执行效果如下:

代码如下:

// 程序名称:椭圆组合而成的抽象图案
// 编译环境:Visual C++ 6.0/2010,EasyX_20210730
// 最初编写:1998-2-21,by yw80@qq.com(QuickBasic 版本)
// 最后修改:2011-3-23,by yw80@qq.com
//
// 高中时候看到一本书的封面有这个图案,就用当时学的 QuickBasic 写了这个程序。
// 现在移植到 VC6 上,做个纪念。
//
#include <graphics.h>
#include <conio.h>
#include <math.h>

const double PI = 3.1415926536;

// 四舍五入
int Round(double x)
{
	return (int)(x < 0 ? x - 0.5 : x + 0.5);
}

// 主函数
int main()
{
	// 初始化
	initgraph(640, 480);		// 创建绘图窗口
	setorigin(320, 240);		// 设置原点为屏幕中央

	double r = 58;
	double csin = sin(2 * PI / 200);
	double ccos = cos(2 * PI / 200);
	for (int j = 0; j < 100; j++, r -= 0.9)
	{
		double tsin = sin(2 * PI * j / 100);
		double tcos = cos(2 * PI * j / 100);
		double x = 0;
		double y = r;
		for (int i = 0; i < 200; i++)
		{
			double temp = x;
			x = x * ccos + y * csin;
			y = y * ccos - temp * csin;
			int scrx = Round(x * tcos + y * 4 * tsin);
			int scry = Round(y * 4 * tcos - x * tsin);
			putpixel(scrx, -scry, GREEN);
		}

		Sleep(20);
	}

	_getch();
	closegraph();
	return 0;
}

添加评论