[C++] 几行代码生成漂亮图片,数学家就是牛!
信息获得处:http://news.cnblogs.com/n/501488/
分形:http://baike.baidu.com/subview/83243/11213590.htm?fr=aladdin
代码:[采用宏定义的来选择绘制不同的图,所以把最后两个给注释掉了,因为宏冲突,想自己试试可以单独编译运行]
PS:生成的图片是ppm格式的要用格式工厂之类的软件进行转换,也可以自己在程序里直接把图片生成bmp或者其他格式的~
// NOTE: compile with g++ filename.cpp -std=c++11
#include <iostream>
#include <cmath>
#include <cstdlib>
#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
#define F unsigned char GR (int,int);
unsigned char BL (int,int); unsigned char RD (int i,int j){
#ifdef A
return (char)(_sq(cos(atan2(j-,i-)/))*);
#endif #ifdef B
#define r(n) (rand ()%n)
static char c[][];
return!c[i][j]?c[i][j]=!r()?r():RD ((i+r())%,(j+r())%):c[i][j];
#endif #ifdef C
float x=,y=;
int k;
for(k=;k++<;){
float a=x*x-y*y+ (i-768.0)/;
y=*x*y+ (j-512.0)/;
x=a;
if(x*x+y*y>)break;
}
return log (k)*;
#endif #ifdef D
double a=,b=,c,d,n=;
while((c=a*a)+(d=b*b)<&&n++<){
b=*a*b+j*8e--.;
a=c-d+i*8e-+.;
}
return *pow ((n-)/,.);
#endif #ifdef E
static double k;
k+=rand ()/./RAND_MAX;
int l=k;
l%=;
return l>?-l:l;
#endif #ifdef F
float s=./(j+);
float y=(j+sin ((i*i+_sq (j-)*)/./DIM)*)*s;
return (int((i+DIM)*s+y)%+int((DIM*-i)*s+y)%)*;
#endif
/*
#ifdef G
#define D DIM
#define M m[(x+D+ (d==0)-(d==2))%D][(y+D+ (d==1)-(d==3))%D]
#define R rand ()%D
#define B m[x][y]
return(i+j)?256-(BL (i,j))/2:0;
#endif
*/
/*
#ifdef H
#define A float a=0,b,k,r,x
#define B int e,o
#define C (x) x>255?255:x
#define R return #define D DIM
R BL (i,j)*(D-i)/D;
#endif
*/
return ;
}
unsigned char GR (int i,int j){
#ifdef A
return (char)(_sq (cos (atan2(j-,i-)/-*acos (-)/))*);
#endif #ifdef B
static char c[][];
return!c[i][j]?c[i][j]=!r()?r():GR ((i+r())%,(j+r())%):c[i][j];
#endif #ifdef C
float x=,y=;
int k;
for(k=;k++<;){
float a=x*x-y*y+ (i-768.0)/;
y=*x*y+ (j-512.0)/;
x=a;
if(x*x+y*y>)break;
}
return log (k)*;
#endif #ifdef D
double a=,b=,c,d,n=;
while((c=a*a)+(d=b*b)<&&n++<){
b=*a*b+j*8e--.;
a=c-d+i*8e-+.;
}
return *pow ((n-)/,.);
#endif #ifdef E
static double k;
k+=rand ()/./RAND_MAX;
int l=k;
l%=;
return l>?-l:l;
#endif #ifdef F
float s=./(j+);
float y=(j+sin ((i*i+_sq (j-)*)/./DIM)*)*s;
return (int(*((i+DIM)*s+y))%+int(*((DIM*-i)*s+y))%)*;
#endif
/*
#ifdef G
#define A
static int m[D][D],e,x,y,d,c[4],f,n;
if (i+j<1){
for (d=D*D;d;d--){
m[d%D][d/D]=d%6?0:rand ()%2000?1:255;
}
for (n=1 return RD (i,j);
#endif
*/
/*
#ifdef H
#define E DM1
#define F static float
#define G for (
#define H r=a*1.6/D+2.4;x=1.0001*b/D
R BL (i,j)*(D-j/2)/D;
#endif
*/
return ;
}
unsigned char BL (int i,int j){
#ifdef A
return (char)(_sq (cos (atan2(j-,i-)/+*acos (-)/))*);
#endif #ifdef B
static char c[][];
return!c[i][j]?c[i][j]=!r()?r():BL((i+r())%,(j+r())%):c[i][j];
#endif #ifdef C
float x=,y=;
int k;
for(k=;k++<;){
float a=x*x-y*y+ (i-768.0)/;
y=*x*y+ (j-512.0)/;
x=a;
if(x*x+y*y>)break;
}
return -log (k)*;
#endif #ifdef D
double a=,b=,c,d,n=;
while((c=a*a)+(d=b*b)<&&n++<){
b=*a*b+j*8e--.;
a=c-d+i*8e-+.;
}
return *pow ((n-)/,.);
#endif #ifdef E
static double k;
k+=rand ()/./RAND_MAX;
int l=k;
l%=;
return l>?-l:l;
#endif #ifdef F
float s=./(j+);
float y=(j+sin ((i*i+_sq (j-)*)/./DIM)*)*s;
return (int(*((i+DIM)*s+y))%+int(*((DIM*-i)*s+y))%)*;
#endif
/*
#ifdef G
A;n;n++){
x=R;y=R;
if(B==1){
f=1;
for(d=0;d<4;d++){
c[d]=M;
f=f<c[d]?c[d]:f;
}
if(f>2){B=f-1;}
else{
++e%=4;
d=e;
if(!c[e]){
B=0;M=1;
}}}}}
return m[i][j];
#endif
*/
/*
#ifdef H
F c[D][D];
if(i+j<1){A;B;G;a<D;a+=0.1){G b=0;b<D;b++){H;G k=0;k<D;k++){x=r*x*(1-x);
if(k>D/2){e=a;o=(E*x);c[e][o]+=0.01;}}}}}
R C (c[j][i])*i/D;
#endif
*/ return ;
} void pixel_write (int,int);
FILE *fp;
int main (){
fp = fopen ("MathPic.ppm","wb");
fprintf (fp, "P6\n%d %d\n255\n", DIM, DIM);
for(int j=;j<DIM;j++)
for(int i=;i<DIM;i++)
pixel_write (i,j);
fclose (fp);
return ;
}
void pixel_write (int i, int j){
static unsigned char color[];
color[] = RD (i,j)&;
color[] = GR (i,j)&;
color[] = BL (i,j)&;
fwrite (color, , , fp);
}
[C++] 几行代码生成漂亮图片,数学家就是牛!的更多相关文章
- line-height行高与图片的隐匿文本间隙消除方法
(标注 文章来源 http://www.zhangxinxu.com/wordpress/2015/08/css-deep-understand-vertical-align-and-line-hei ...
- Bootstrap 3之美03-独立行,文字环绕,图片自适应,隐藏元素
本篇主要包括: ■ 添加独立的一行■ 文字环绕■ 图片自适应■ 隐藏元素 添加独立的一行 在id为body的section和id为main的section之间,添加2张图片. 我们发现,新加的 ...
- 利用iterm2,在命令行预览图片,服务器也是可以的
1.首先你本地电脑上要安装iterm2软件,我们这里使用brew安装 这个是一定要装的,因为能在命令行渲染出图片文件全靠它,其实不是服务器渲染出来的,而是iterm2 官方网站:https://www ...
- C语言乱谈(一) 20行代码生成BMP
在学习图形图像的过程中,最简单和常见的格式是BMP和PPM.下面将给出生成BMP的极度精简代码,然后讲解BMP格式. #include <stdio.h> #include <std ...
- 微信小程序rich-text 文本首行缩进和图片居中
微信小程序开发使用rich-text组件渲染html格式的代码,常常因为不能自定义css导致文本不能缩进,以及图片不能居中等问题,这里可以考虑使用js的replace方法,替换字符串,然后在渲染的同时 ...
- 使用命令行生成 APNG 图片
使用 apngasm 工具 下载地址 https://sourceforge.net/projects/apngasm/files/2.91/ 本地源码编译 make 或者 下载对应环境的可执行程序 ...
- python学习---50行代码实现图片转字符画1
转自:https://blog.csdn.net/mm1030533738/article/details/78447714 项目链接: https://www.shiyanlou.com/cours ...
- Activiti开发案例之代码生成工作流图片
图例 环境 软件 版本 SpringBoot 1.5.10 activiti-spring-boot-starter-basic 6.0 生成代码 以下是简化代码: /** * 查看实例流程图,根据流 ...
- [测试篇]MarkDown之代码块行号+折叠图片
对比测试代码编号 $(function(){ $('pre code').each(function(){ texts = $(this).text().replace(/&(?!#?[a-z ...
随机推荐
- Python全栈--7.3--模块补充configparser--logging--subprocess--os.system--shutil
模块补充: 一.configparser用于处理特定格式的文件,其本质是利用open来操作文件 继承到2版本 configparser 实现了更多智能特征,更有壳预见性,新的应用更偏好这个版本, 处理 ...
- Missing letters
function fearNotLetter(str) { //return str; var arr = str.split(''); var temp = []; var start = str. ...
- Amr and Chemistry CodeForces 558C(BFS)
http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...
- HttpURLConnection请求网络数据的Post请求
//--------全局变量----------- //注册Url private String urlPath="http://101.200.142.201:8080/VideoP ...
- coredump调试的使用
一,什么是coredump 跑程序的时候经常碰到SIGNAL 或者 call trace的问题,需要定位解决,这里说的大部分是指对应程序由于各种异常或者bug导致在运行过程中异常退出或者中止,并且在满 ...
- apache端口的修改
apache 这个web服务默认在80端口监听...如果你访问一个网站 http://www.baidu.com 则默认一个端口是80 1. 一台机器可以有 1-65535 号端口 2. ...
- java 开发常用的Linux命令
1.查找文件 find / -name filename.txt 根据名称查找/目录下的filename.txt文件. find . -name "*.xml" 递归查找所有的xm ...
- 【jquery】:表单返回信息
第一个页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- MapReduce几个简单的例子
文件合并和去重: 可以把每一行文本作为key,value为随意值. 数字排序: MapReduce过程中就有排序,它是按照key值进行排序的,如果key为封装int的IntWritable类型,那么M ...
- MongoDB介绍与windows下安装
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类 似json的bjson格式,因此可以存储比较复杂的数据类型. ...