The Fractal Fern
Site Map Feedback
Fractal Fern
Up Fern Mandelbrot Parity SeedHead
To draw the Fractal Fern on the PixelBlock, mix in the CFernStencil class below and define the BlendPixel abstract method to use CPixelBlock::BlendPixel:
This class uses Fractal principles to create a picture of a Fern Leaf.
Though far from an optimum way of drawing a leaf it's fascinating for its mystery!
All it's doing is firing pseudo-random pixels with a probability distribution specifying more or less likely areas to effect.
The loop counter can be increased for a denser leaf. Try playing with the other values to see the effects...
class CFernStencil {
  virtual void BlendPixel(WORD x, WORD y, DWORD Colour, BYTE Transparency) =0;
public:
  CFernStencil() {}
  void Fern(WORD x0=0, WORD y0=0, int Scale=0) {
    if(x0==0) x0=GetWidth()/2;
    if(y0==0) y0=GetHeight();
    if(Scale==0) Scale=y0/10;
    double x=0,y=0,t;
    long LastRand=1;
    for(long g=100*Scale*Scale; g; --g) {
      int n=(int)((LastRand=LastRand*214013L+2531011L) & 0x7FFF); // Random number generator.
           if(n< 328) {t= 0            ; y=        0.16*y     ;} // 1% Stem
      else if(n<2621) {t= 0.2 *x-0.26*y; y= 0.28*x+0.22*y+1.6 ;} // 7% Left Frond
      else if(n<4915) {t=-0.15*x+0.28*y; y= 0.26*x+0.24*y+0.44;} // 7% Right Frond
      else            {t= 0.85*x+0.04*y; y=-0.04*x+0.85*y+1.6 ;} // 85% Next Position
      BlendPixel((WORD)(x0+(x=t)*Scale),(WORD)(y0-y*Scale), 0x00D000, 64);
  } }
};
You can mix in as many 'Stencils' as you like to provide all the primitives you need.

The Colours, Drawing and Effects sections have further extensions.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.