一、分形(Fractal)
这个概念应该很多人听过,说到这个应该就会提到最著名的Mandelbrot Set与Julia Set,最近便着手写了个能画出两者图形的一个小程序,环境为 VS2013 + BCG + CxImage。
关于两者的概念,网上很多非常清晰的解释,这里便不再啰嗦,核心公式即
f(z) = z^2 + c
Mandelbrot Set是在z = 0处,对复平面上每一点c进行迭代计算,若每一个 |f(z)| 小于2,则其属于集合内,当然,我们在计算机计算时只能设定一个迭代次数,超过迭代上限仍然模小于2的认为属于集合。
Julia Set则是对于设定的一个常数c(其模假定小于2),对复平面内的每一点z进行迭代计算,若满足模小于2则属于集合,两者有差别,具体解释可以维基。
二、 画图实现
2.1 复数定义
class CComplex
{
public:
~CComplex(){};
CComplex(long double real = 0, long double imag = 0)
: m_real(real)
, m_imag(imag)
{}
CComplex& operator +=(const CComplex &rhs)
{
this->m_real += rhs.m_real;
this->m_imag += rhs.m_imag;
return *this;
}
CComplex& operator -=(const CComplex &rhs)
{
this->m_real -= rhs.m_real;
this->m_imag -= rhs.m_imag;
return *this;
}
friend CComplex operator +(const CComplex &lhs, const CComplex &rhs);
friend CComplex operator -(const CComplex &lhs, const CComplex &rhs);
friend CComplex operator *(const CComplex &lhs, const CComplex &rhs);
friend bool operator ==(const CComplex &lhs, const CComplex &rhs);
inline friend double modulus_square(const CComplex & c)
{
return c.m_real * c.m_real + c.m_imag * c.m_imag;
}
inline friend CComplex complex_power(const CComplex &c, unsigned int n)
{
CComplex result(1);
for