我公开以下图片的源代码,,是ppm格式的,,自己找到能打开的工具..
(非原创,我加工的代码,可直接执行运行输出,缩略图能看到效果)
这是原博客 http://news.cnblogs.com/n/501488/
请教高手一个问题,,
就是如何 调用动态方面名,,方面名均为后缀+数字,
希望可以根据动态方面名来调用不同的方法...
我觉得就是太多重复的代码了..我想用C++类泛型,,应该可以调用相应的方法.
![](http://images.cnitblog.com/news/66372/201408/151845002647149.png)
![](http://images.cnitblog.com/news/66372/201408/151846378899207.png)
以下是 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);
}