当前位置:网站首页>Programmers, what are your skills in code writing?
Programmers, what are your skills in code writing?
2022-07-06 16:04:00 【Programming fish 66】
Manuel Kasten Also made a Mandelbrot Set of pictures , What's different from just now is , The picture depicts Mandelbrot The result of local amplification set somewhere : We know , The color to be displayed in the computer , It's usually used R、G、B Three 0-255 An integer within a range to describe .
This point , Even if you're not in the front end 、 client These development work related to interface interaction , I should know .
in other words , The color of any pixel you see on the screen now , Both can be used. RGB Three integer values to represent .
Then there is an interesting question : If you let the program automatically fill in each pixel , What will be the last picture ?
Recently, I saw such an interesting topic in Zhihu , It's really amazing after reading it , It is much joyful to share the joy than enjoy alone. , Share with you .
There is a big man abroad StackExchange
There's a program called Tweetable Mathematical Art
The game .
Contestants need to use C++
Write a code representing the three primary colors RD、GR、BL Three functions , Each function cannot exceed 140 Characters . Every function receives i and j Two integer parameters (0 ≤ i, j ≤ 1023), Then you need to go back to a 0 To 255 Integer between , Means located at (i, j) The color value of the pixel .
for instance , If RD(0, 0) and GR(0, 0) All of them 0 , but BL(0, 0) The return is 255 , Then the pixel at the top left corner of the image is blue . The code written by the contestant will be inserted into the following program ( I made some minor changes ), Finally, a size of 1024×1024 Pictures of the .
// 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
unsigned char GR(int,int);
unsigned char BL(int,int);
unsigned char RD(int i,int j){
// YOUR CODE HERE
}
unsigned char GR(int i,int j){
// YOUR CODE HERE
}
unsigned char BL(int i,int j){
// YOUR CODE HERE
}
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=0;j<DIM;j++)
for(int i=0;i<DIM;i++)
pixel_write(i,j);
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);
}
I chose some of my favorite works , Put it below to share with you . The first is one from Martin Büttner The works of :
The code is as follows :
unsigned char RD(int i,int j){
return (char)(_sq(cos(atan2(j-512,i-512)/2))*255);
}
unsigned char GR(int i,int j){
return (char)(_sq(cos(atan2(j-512,i-512)/2-2*acos(-1)/3))*255);
}
unsigned char BL(int i,int j){
return (char)(_sq(cos(atan2(j-512,i-512)/2+2*acos(-1)/3))*255);
}
Also from Martin Büttner The works of :
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];
}
The following picture is still from Martin Büttner In the hands of :
unimaginable , Mandelbrot Fractal graphics can be drawn with only such a little code
nsigned char RD(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 GR(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 BL(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;
}
Manuel Kasten Also made a Mandelbrot Set of pictures , What's different from just now is , The picture depicts Mandelbrot The result of local amplification set somewhere :
The code is as follows :
unsigned char RD(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 GR(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 BL(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);
}
This is a Manuel Kasten Another work of :
The code for generating this picture is very interesting : The function depends on static Variable to control the process of painting , It's not used at all i and j These two parameters !
unsigned char RD(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 GR(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 BL(int i,int j){
static double k;k+=rand()/1./RAND_MAX;int l=k;l%=512;return l>255?511-l:l;
}
This is from githubphagocyte The works of :
The code is as follows :
unsigned char RD(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 GR(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 BL(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;
}
This is from githubphagocyte Another piece of work of :
This is a picture using diffusion-limited aggregation The picture from the model , The program takes a lot of time to run . The code is very interesting : Clever use of macro definitions , Breaking the boundary between functions , The word limit of the three pieces of code can be used together .
unsigned char RD(int i,int j){
#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;
}
unsigned char GR(int i,int j){
#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);
}
unsigned char BL(int i,int j){
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];
}
The last picture comes from Eric Tressler:
This is from logistic Mapped Feigenbaum Bifurcation diagram . Just like before , The corresponding code also makes clever use of Macro definition To save characters :
unsigned char RD(int i,int j){
#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;
}
unsigned char GR(int i,int j){
#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;
}
unsigned char BL(int i,int j){
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;
}
What about? , Just a few lines of code , Can draw such a gorgeous image , Do you have any idea of a big hole in your brain , You can copy the above code to have a try !
resources & More in-depth readings :
Open the mysterious channel to get information, click the link to join the group chat 【C Language /C++ Programming learning base 】:828339809
边栏推荐
- [analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
- Alice and Bob (2021牛客暑期多校训练营1)
- VS2019初步使用
- 【练习-10】 Unread Messages(未读消息)
- E. Breaking the Wall
- 信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
- 0-1 knapsack problem (I)
- Analysis of protobuf format of real-time barrage and historical barrage at station B
- Borg maze (bfs+ minimum spanning tree) (problem solving report)
- Borg Maze (BFS+最小生成树)(解题报告)
猜你喜欢
用C语言写网页游戏
洛谷P1102 A-B数对(二分,map,双指针)
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
基于web的照片数码冲印网站
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
Gartner:关于零信任网络访问最佳实践的五个建议
Nodejs+vue网上鲜花店销售信息系统express+mysql
随机推荐
Opencv learning log 33 Gaussian mean filtering
STM32 learning record: LED light flashes (register version)
【练习-2】(Uva 712) S-Trees (S树)
最全编程语言在线 API 文档
D - function (HDU - 6546) girls' competition
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Research Report of peripheral venous catheter (pivc) industry - market status analysis and development prospect prediction
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
信息安全-威胁检测引擎-常见规则引擎底座性能比较
New to redis
[exercise-1] (UVA 673) parentheses balance/ balanced brackets (stack)
X-forwarded-for details, how to get the client IP
TCP的三次握手与四次挥手
Information security - threat detection engine - common rule engine base performance comparison
Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
Research Report on market supply and demand and strategy of China's land incineration plant industry
1010 things that college students majoring in it must do before graduation
CEP used by Flink
【高老师软件需求分析】20级云班课习题答案合集
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool