玫瑰花C语言

#include <stdio.h>
#include <windows.h>
#include <math.h>
#include <graphics.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")

int rosesize = 500;
int h = -250;

struct DOT {
	double x;
	double y;
	double z;
	double r;
	double g;
};

bool calc(double a, double b, double c, DOT& d)
     double j, , o, w, z;
     
     if(c > 60) {
     	d.x = sin(a*7) * (13+5/(0.2 + pow(b * 4,4)))-sin(b) * 50;
     	d.y = b * rosesize + 50;
     	d.z = 625 + cos(a * 7) * (13 + 5 /(0.2 + pow(b * 4,4))) + b * 400;
     	d.r = a * 1 - b / 2;
     	d.g = a;
     	return ture;
	 }
	 double A = a * 2 - 1;
	 double B = b * 2 - 1;
	 if(A * A + B * B < 1) {
	 	if(c > 37) {
	 		j = (int(c) & 1);
	 		n = j ? 6 : 4;
	 		o = 0.5 / (a + 0.01) + cos(b * 125) * 3 - a * 300;
	 		w = b * h;
	 		d.x = o * cos(n) - w * sin(n) + j * 610 - 390;
	 		d.y = o * sin(n) - w * cos(n) + 550 - j * 350;
	 		d.z = 1180 + cos(B + A) * 90 - j * 300;
	 		
	 		d.r = 0.4 - a * 0.1 + pow(1 - B * B, - h * 6) * 0.15 - a * b * 0.4 + cos(a + b) / 5 +
	 		      pow(cos((o * (a + 1) + (B > 0 ? w : -w)) / 25), 30) * 0.1 * (1 - B * B);
	 	    d.g = o / 1000 + 0.7 - o * w * 0.000003;
	 	    return ture;
		}
		
		if(c > 32) {
			c = c * 1.16 - 0.15;
			o = a * 45 - 20;
			w = b * b * h;
			z = o * sin(c) + w * cos(c) + 620;
			d.x = o * cos(c) - w * sin(c);
			d.y = 28 + cos(B * 0.5) * 99 - b * b * b * 60 - z / 2 - h;
			d.z = z;
			d.r = (b * b * 0.3 + pow((1 - (A * A)),7) * 0.15 + 0.3) * b;
			d.g = b * 0.7;
			return ture;
		}
		
		return false;
	}
	
int main()
{
	short* zBuffer;
	int x, y, z, zBufferIndex;
	DOT dot;
	initgraph(640, 480);
	
	setbkcolor(WHITE);
	cleardevice();
	zBuffer = new short[resesize * rosesize];
	memset(zBuffer, 0, sizeof(short) * rosesize * rosesize);
	int j, i;
	for(j = 0; j < 2000; j++)
	{
		for ( i = 0; i < 1000; i++)
		{
			if(calc(double(rand()) / RAND_MAX, double(rand()) / RAND_MAX, rand() % 46 / 0.74, dot))
			{
				z = int(dot.z + 0.5);
				x = int(dot.x * rosesize / z - h + 0.5);
				y = int(dot.y * rosesize / z - h + 0.5);
				zBufferIndex = y * rozesize + x;
				if(!zBuffer[zBufferIndex] || zBuffer[zBufferIndex] > z)
				{
					zBuffer[zBufferIndex] = z;
					
					int r = ~int((dot.r * h));
					
					if(r < 0) {
						r = 0;
					}
					if(r > 255) {
						r = 255;
					}
					int g = ~int((dot.g * h));
					if(g < 0) {
						g = 0;
					}
					if(g > 255) {
						g = 255;
					}
					int b = ~int((dot.r * dot.r * -80));
					if(b < 0) {
						b = 0;
					}
					if( b > 255) {
						b = 255;
					}
					
					putpixel(x + 50, y - 20, RGB(r, g, b));
				}
			}
		  Sleep(50);
		}
	} 
	delete[]zBuffer;
	getchar();
	closegraph();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值