//【c++的自由度高到足以进行语法扩展。】
//【下边对mandelbrot set进行语法扩展。】
//【这定义了一个特殊的【实数集下的bool数组。】】
//mandbrot m;
//m[i][j]=true/false。i,j是double值。
//true/false取决于复数点是否符合"属于mandelbrot集合"的判定条件。
//因此可以将m[i][j]总集看作一个double粒度(其实还受到浮点数的精度限制)的数组。而其中的语法也正是最常用的数组语法。
//注意"半参(half)"类的使用:它是一个函数的变量表赋值到一半的一种过渡状态,如果不声明为private的话,是可以赋值,传递的。m[i]实质上是【表征了m()函数引用了一半的一种中间态。】
class mandelbrot
{
struct complex
{
double re;
double im;
complex(double pre,double pim):re(pre),im(pim){}
complex& operator+=(const complex& adder)//只定义两个必要操作:+和*。
{
re += adder.re;
im += adder.im;
return *this;
}
complex& operator*=(const complex& muer)//支持自乘。
{
double retemp = re * muer.re - im * muer.im;
im = im * muer.re + re * muer.im;
re = retemp;
return *this;
}
};
class half//半参类。
{
const mandelbrot& reference;//引用是占字节的。
double re;
public:
half(const mandelbrot& man,double pre):reference(man),re(pre){}
bool operator[](double im)const
{
//sizeof(*this);//16。//64。
return reference.operator()(re, im);
}
};
int iterations;
public:
mandelbrot(int it):iterations(it){}
bool operator()(double re, double im)const
{
complex c(re,im),z(re, im);
for (int k = 0; k < iterations; k++)
{
if (z.re * z.re + z.im * z.im > 4.0)
return false;
(z *= z) += c;
}
return true;
}
const half operator[](double re)const
{
return { *this, re };
}
};
#include<stdio.h>
int main()
{
const mandelbrot man(128);
for (double y = 1.5; y >= -1.5; y -= 0.05, putchar('\n'))
for (double x = -2; x <= 1; x += 0.05)
printf("%s", man[x][y] == true ? "**" : " ");
}
c++: 符号重载:实现特定场合下的bool array[double][double]。
最新推荐文章于 2024-12-27 00:00:00 发布