程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 用RD,GR,BL三個方法內代碼生成一張圖片(非原創,我只是完整了代碼),grbl

用RD,GR,BL三個方法內代碼生成一張圖片(非原創,我只是完整了代碼),grbl

編輯:C++入門知識

用RD,GR,BL三個方法內代碼生成一張圖片(非原創,我只是完整了代碼),grbl


我公開以下圖片的源代碼,,是ppm格式的,,自己找到能打開的工具..

(非原創,我加工的代碼,可直接執行運行輸出,縮略圖能看到效果)  這是原博客 http://news.cnblogs.com/n/501488/   本人測試可在Visual Studio 2012 中運行

請教高手一個問題,,
就是如何 調用動態方面名,,方面名均為後綴+數字,
希望可以根據動態方面名來調用不同的方法...
我覺得就是太多重復的代碼了..我想用C++類泛型,,應該可以調用相應的方法.

 

 

 

以下是 C++ 的代碼,,,

#include "stdafx.h"

#include <iostream>
#include <cmath>
#include <cstdlib> 
#include<Windows.h>

#pragma warning(disable:4996) //全部關掉
#pragma warning(disable:4244)

#define DIM 1024 
#define DM1 (DIM-1) 
#define _sq(x) ((x)*(x)) // square 
#define _cb(x) abs ((x)*(x)*(x)) // absolute value of cube 
#define _cr(x) (unsigned char)(pow ((x),1.0/3.0)) // cube root


unsigned char GR (int ,int);
unsigned char BL (int ,int);

unsigned char RD1 (int i,int j){
	float x=0,y=0;
	int k;
	for(k=0;k++<256;)
	{
		float a=x*x-y*y+ (i-768.0)/512;
		y=2*x*y+ (j-512.0)/512;
		x=a;
		if(x*x+y*y>4)
			break;
	}
	return log (k)*47;
}

unsigned char GR1 (int i,int j){
	float x=0,y=0;
	int k;
	for(k=0;k++<256;)
	{
		float a=x*x-y*y+ (i-768.0)/512;y=2*x*y+ (j-512.0)/512;
		x=a;
		if(x*x+y*y>4)
			break;
	}
	return log (k)*47;
}

unsigned char BL1 (int i,int j){
	float x=0,y=0;
	int k;
	for(k=0;k++<256;)
	{
		float a=x*x-y*y+ (i-768.0)/512;
		y=2*x*y+ (j-512.0)/512;
		x=a;
		if(x*x+y*y>4)
			break;
	}
	return 128-log (k)*23;
}

unsigned char RD (int i,int j)
{
#define r(n) (rand ()%n) 
	static char c[1024][1024];
	return !c[i][j]?c[i][j]=!r(999)?r(256):RD ((i+r(2))%1024,(j+r(2))%1024):c[i][j];
}

unsigned char GR (int i,int j)
{
	static char c[1024][1024];
	return!c[i][j]?c[i][j]=!r(999)?r(256):GR ((i+r(2))%1024,(j+r(2))%1024):c[i][j];
}

unsigned char BL (int i,int j)
{
	static char c[1024][1024];
	return!c[i][j]?c[i][j]=!r(999)?r(256):BL ((i+r(2))%1024,(j+r(2))%1024):c[i][j];
}

unsigned char RD2 (int i,int j){
	double a=0,b=0,c,d,n=0;
	while((c=a*a)+(d=b*b)<4&&n++<880)
	{b=2*a*b+j*8e-9-.645411;a=c-d+i*8e-9+.356888;}
	return 255*pow ((n-80)/800,3.);
}

unsigned char GR2 (int i,int j){
	double a=0,b=0,c,d,n=0;
	while((c=a*a)+(d=b*b)<4&&n++<880)
	{b=2*a*b+j*8e-9-.645411;a=c-d+i*8e-9+.356888;}
	return 255*pow ((n-80)/800,.7);
}

unsigned char BL2 (int i,int j){
	double a=0,b=0,c,d,n=0;
	while((c=a*a)+(d=b*b)<4&&n++<880)
	{b=2*a*b+j*8e-9-.645411;a=c-d+i*8e-9+.356888;}
	return 255*pow ((n-80)/800,.5);
}

unsigned char RD3 (int i,int j){
	static double k;k+=rand ()/1./RAND_MAX;int l=k;l%=512;return l>255?511-l:l;
}

unsigned char GR3 (int i,int j){
	static double k;k+=rand ()/1./RAND_MAX;int l=k;l%=512;return l>255?511-l:l;
}

unsigned char BL3 (int i,int j){
	static double k;k+=rand ()/1./RAND_MAX;int l=k;l%=512;return l>255?511-l:l;
}

unsigned char RD4 (int i,int j){
	float s=3./(j+99);
	float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s;
	return (int((i+DIM)*s+y)%2+int((DIM*2-i)*s+y)%2)*127;
}

unsigned char GR4 (int i,int j){
	float s=3./(j+99);
	float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s;
	return (int(5*((i+DIM)*s+y))%2+int(5*((DIM*2-i)*s+y))%2)*127;
}

unsigned char BL4 (int i,int j){
	float s=3./(j+99);
	float y=(j+sin ((i*i+_sq (j-700)*5)/100./DIM)*35)*s;
	return (int(29*((i+DIM)*s+y))%2+int(29*((DIM*2-i)*s+y))%2)*127;
}

void pixel_write (int,int);
FILE *fp;

void pixel_write1(int i, int j)
{
	static unsigned char color[3];
	color[0] = RD1 (i,j)&255;
	color[1] = GR1 (i,j)&255;
	color[2] = BL1 (i,j)&255;
	fwrite (color, 1, 3, fp);
}

void pixel_write2 (int i, int j)
{
	static unsigned char color[3];
	color[0] = RD2 (i,j)&255;
	color[1] = GR2 (i,j)&255;
	color[2] = BL2 (i,j)&255;
	fwrite (color, 1, 3, fp);
}

void pixel_write3(int i, int j)
{
	static unsigned char color[3];
	color[0] = RD3 (i,j)&255;
	color[1] = GR3 (i,j)&255;
	color[2] = BL3 (i,j)&255;
	fwrite (color, 1, 3, fp);
}

void pixel_write4(int i, int j)
{
	static unsigned char color[3];
	color[0] = RD4 (i,j)&255;
	color[1] = GR4 (i,j)&255;
	color[2] = BL4 (i,j)&255;
	fwrite (color, 1, 3, fp);
}

int main ()
{
	char t[256];
	char printfHistory[2048]="正寫入文件請稍等";
	printf_s("printfHistory");
	int num=0;
	for(int a=1;a<6;a++)
	{
		sprintf_s(t,"MathPic%d.ppm",a);
		fp = fopen (t,"wb");
		fprintf (fp, "P6\n%d %d\n255\n", DIM, DIM);
		for(int j=0;j<DIM;j++)
		{
			for(int i=0;i<DIM;i++)
			{
				switch (a)
				{
				case 1:
					pixel_write(i,j);
				case 2:
					pixel_write1(i,j);
				case 3:
					pixel_write2(i,j);
				case 4:
					pixel_write3(i,j);
				case 5:
					pixel_write4(i,j);
				default:
					break;
				}
			}
		}
		printf_s("第 %d 張圖片 寫入成功!\n",a);
	}
	printf_s("所有圖片已全部輸出,請打開程序目錄查看!");
	fclose (fp);
	return 0;
}

void pixel_write (int i, int j)
{
	static unsigned char color[3];
	color[0] = RD (i,j)&255;
	color[1] = GR (i,j)&255;
	color[2] = BL (i,j)&255;
	fwrite (color, 1, 3, fp);
}

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved