算法_分形技术

#include "iostream.h"
#include "fstream.h"
#define max 512


int map[max][max] = { 0 };

struct mRect
{
 int left;
 int top;
 int right;
 int bottom;
};

bool divbox( int deep , mRect rect );
bool drawRect( mRect rect );
void drawMap( mRect rect );

void main()
{
 int deep = 3;
 mRect rect;
 rect.top = 0;
 rect.left = 0;
 rect.right = 1;
 rect.bottom = 1;
 for( int i = 0 ; i < deep ; i++ )
 {
  rect.right *= 3;
  rect.bottom *= 3;
 }
 rect.right -= 1;
 rect.bottom -= 1;

 divbox( deep , rect );
 drawMap( rect );
}

void drawMap( mRect rect )
{
 ofstream fout("divmap.txt");
 int i , j;
 for( i = rect.top ; i <= rect.bottom ; i++ )
 {
  for( j = rect.left ; j <= rect.right ; j++ )
  {
   if( map[i][j] == 0 )
    fout<<" ";
   else if( map[i][j] == 1 )
    fout<<"*";
  }
  fout<<endl;
 }
}

bool drawRect( mRect rect )
{
 //if size of rect is not equal to 3, return false
 if( rect.right - rect.left != 2 || rect.bottom - rect.top != 2 )
  return false;

 int top = rect.top;
 int left = rect.left;
 int bottom = rect.bottom;
 int right = rect.right;

 map[top][left] = 1;
 map[top][right] = 1;
 map[bottom][left] = 1;
 map[bottom][right] = 1;
 map[(top+bottom)/2][(left+right)/2] = 1;

 return true;
}


bool divbox( int deep , mRect rect )
{
 if( deep == 1 )
 {
  return drawRect( rect );
 }
 else
 {
  bool flag = true;
  mRect rects[9];
  int i , j;
  for( i = 0 ; i < 3 ; i++ )
  {
   for( j = 0 ; j < 3 ; j++ )
   {
    int size = ( rect.right - rect.left + 1 ) / 3;
    rects[3*i+j].left = rect.left + j * size;
    rects[3*i+j].right = rect.left + ( j+1 ) * size - 1;
    rects[3*i+j].top = rect.top + i * size;
    rects[3*i+j].bottom = rect.top + ( i+1 ) * size - 1;
   }
  }
  for( i = 0 ; i < 10 ; i += 2 )
  {
   flag = divbox( deep - 1 , rects[i] );
   if( !flag )
    return false;
  }
  return true;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值