Ce traceur de fractales utilise la récursivité. Vous pourrez choisir le type de fractale à tracer, l'ordre, couleur, longueur... bref tout est paramétrable !!!!
Source / Exemple :
// fractalesDlg.cpp : implementation file
//
#include "stdafx.h"
#include "fractales.h"
#include "fractalesDlg.h"
#include "math.h"
#include "ShellApi.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFractalesDlg dialog
CFractalesDlg::CFractalesDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFractalesDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFractalesDlg)
m_eordre = 0;
m_multi = FALSE;
m_a = 0.0;
m_l = 0.0;
m_x = 0.0;
m_y = 0.0;
m_rang = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFractalesDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFractalesDlg)
DDX_Control(pDX, IDC_JAUGE, m_jauge);
DDX_Text(pDX, IDC_EORDRE, m_eordre);
DDX_Check(pDX, IDC_CHECK1, m_multi);
DDX_Text(pDX, IDC_A, m_a);
DDX_Text(pDX, IDC_L, m_l);
DDX_Text(pDX, IDC_X, m_x);
DDX_Text(pDX, IDC_Y, m_y);
DDX_Text(pDX, IDC_RG, m_rang);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFractalesDlg, CDialog)
//{{AFX_MSG_MAP(CFractalesDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnVonKoch)
ON_BN_CLICKED(IDC_BUTTON2, OnRest)
ON_BN_CLICKED(IDC_BUTTON3, OnRosace)
ON_BN_CLICKED(IDC_BUTTON4, OnEffaceDessin)
ON_BN_CLICKED(IDC_BUTTON5, OnJrassic)
ON_BN_CLICKED(IDC_BUTTON6, OnSierpinski)
ON_BN_CLICKED(IDC_BUTTON7, OnChooseCouleur)
ON_BN_CLICKED(IDC_SITE, OnSite)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFractalesDlg message handlers
BOOL CFractalesDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CFractalesDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CFractalesDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//Variables globales
double pi=3.14159;
COLORREF col=RGB(0,0,255);
CPen penb(PS_SOLID,1,col);
//appel du dessin
void CFractalesDlg::OnVonKoch()
{
UpdateData(TRUE);
int o=m_eordre;
if(o>=0 && o<100)
{
CFractalesDlg::RedrawWindow();
double x=50;
double y=400;
double l=100;
double a=0;
UpdateData(TRUE);
x=m_x;
y=m_y;
l=m_l;
a=m_a;
//Fonction triangle (dans l'appel)
Koch(x,y,l,a,o);
Koch(
x + l * cos (a),
y + l * sin (a),
l,
a - 2* pi /3,
o);
Koch(
(x + l * cos (a)) + l * cos (a-2* pi /3),
(y + l * sin (a)) + l * sin (a-2* pi /3),
l,
-4*pi/3+a,
o);;
}
}
//Fonction Koch
void CFractalesDlg::Koch(double x, double y, double l, double a, int ordre)
{
CPen pena(PS_SOLID,2,RGB(rand()%250+2,rand()%250+2,rand()%250)+2);
UpdateData(TRUE);
CClientDC tr(this);
if (ordre ==0)
{
if(m_multi==TRUE)
{tr.SelectObject(&pena);}
else
{tr.SelectObject(&penb);}
tr.MoveTo(x,y);
tr.LineTo((x+l*cos(a)),(y+l*sin(a)));
}
else
{
Koch (x, y, (l/3), a, (ordre-1) );
Koch (
(x + (l*cos (a))/3),
(y + ((l*sin (a))/3)),
(l/3),
(a + (pi / 3)),
(ordre- 1)
);
Koch (
(x + ((l * cos (a))/3) + ( (l/3)* cos (a+pi/3) )),
(y + ((l * sin (a))/3) + ( (l/3)* sin (a+pi/3) )),
(l/3),
(a - pi/3),
(ordre-1)
);
Koch (
(x + 2* ((l*cos (a))/3)),
(y + 2* ((l*sin (a))/3)),
(l/3),
a,
(ordre-1)
);
}
}
//fin de Koch
void CFractalesDlg::OnRest()
{
CRect rect;
GetClientRect(rect);
int a=rect.right;
int b=rect.bottom;
m_x=a/3;
m_y=b/2;
m_l=100;
m_a=0;
m_eordre=0;
m_multi=FALSE;
m_rang=1;
UpdateData(FALSE);
}
void CFractalesDlg::OnRosace()
{
//mettre la jauge en palce
GetDlgItem(IDC_JAUGE)->ShowWindow(TRUE);
//focntion prépar. des variables
UpdateData(TRUE);
int o=m_eordre;
if(o>=0 && o<50)
{
CFractalesDlg::RedrawWindow();
double x=50;
double y=400;
double l=100;
double a=0;
double rang=1;
UpdateData(TRUE);
x=m_x;
y=m_y;
l=m_l;
a=m_a;
rang=m_rang;
m_jauge.SetRange32(0,rang);
m_jauge.SetPos(0);
//Prévention d'un ordre trop haut
int result;
if (o>=10)
{
result=MessageBox("L'ordre que vous avez choisi risque de nécessiter un temps de calcul considérable. Poursuivre ?",
"Ordre très élevé pour une rosace",MB_YESNO | MB_ICONEXCLAMATION);
}
if(o< 10 || result==IDYES)
{
//début du calcul
for (int i=0;i<rang;i++)
{
m_jauge.OffsetPos(1);
Koch(x,y,l,a,o);
Koch(
x + l * cos (a),
y + l * sin (a),
l,
a - 2* pi /3,
o);
Koch(
(x + l * cos (a)) + l * cos (a-2* pi /3),
(y + l * sin (a)) + l * sin (a-2* pi /3),
l,
-4*pi/3+a,
o);
a +=(2*3.14159)/(rang);
}
}
GetDlgItem(IDC_JAUGE)->ShowWindow(FALSE);
}
}
//effacer dessin
void CFractalesDlg::OnEffaceDessin()
{
CFractalesDlg::RedrawWindow();
}
//quitter l'application
void CFractalesDlg::OnOK()
{
AfxMessageBox("See you later...\n;-)");
CDialog::OnOK();
}
///////Fonction Jurassic Curve/////
void CFractalesDlg::OnJrassic()
{
UpdateData(TRUE);
double x=m_x;
double y=m_y;
double l=m_l;
double a=m_a;
int ordre=m_eordre;
CFractalesDlg::RedrawWindow();
if(ordre>=0) {CFractalesDlg::Curve(x,y,l,a,ordre);}
else {AfxMessageBox ("Entrez un ordre positif (ou nul) svp");}
}
void CFractalesDlg::Curve(double x, double y, double l, double a, int ordre)
{
CPen pena(PS_SOLID,2,RGB(rand()%250+2,rand()%250+2,rand()%250)+2);
UpdateData(TRUE);
CClientDC tr(this);
if (ordre ==0)
{
if(m_multi==TRUE)
{tr.SelectObject(&pena);}
else
{tr.SelectObject(&penb);}
//traceur de Jurassic Curve
tr.MoveTo(x,y);
tr.LineTo((x+l*cos(a)),(y+l*sin(a)));
//fin du traceur
}
else
{
Curve(x,y,l/sqrt(2),(a+pi/4),ordre-1);
Curve(
(x+ l*cos (a)),
(y+ l*sin (a)),
l/sqrt(2),
(a+3*pi/4),
ordre-1
);
}
}
void CFractalesDlg::OnSierpinski()
{
UpdateData(TRUE);
double x=m_x;
double y=m_y;
double l=m_l;
int ordre=m_eordre;
CFractalesDlg::RedrawWindow();
if(ordre>=0) {CFractalesDlg::Sierpin(x,y,l,ordre);}
else {AfxMessageBox ("Entrez un ordre positif (ou nul) svp");}
}
void CFractalesDlg::Sierpin(double x, double y, double l, int ordre)
{
CPen pena(PS_SOLID,2,RGB(rand()%250+2,rand()%250+2,rand()%250)+2);
UpdateData(TRUE);
CClientDC tr(this);
if (ordre ==0)
{
if(m_multi==TRUE)
{tr.SelectObject(&pena);}
else
{tr.SelectObject(&penb);}
//traceur de Sierpinski
tr.MoveTo(x,y);
tr.LineTo( (x+l*cos(pi/3)),(y-l*sin(pi/3)) );
tr.LineTo(x+l,y);
tr.LineTo(x,y);
//fin du traceur
}
else
{
//itérateur de Sierpinski
Sierpin(x,y,l/2,ordre-1);
Sierpin(
(x+(l*cos (pi/3))/2),
(y-(l*sin (pi/3))/2),
l/2,
ordre-1
);
Sierpin(
x+l/2,
y,
l/2,
ordre-1
);
/////////
}
}
//en travaux-couleurs///////
void CFractalesDlg::OnChooseCouleur()
{
CColorDialog m_color;
if(m_color.DoModal()==IDOK)
{
col=m_color.GetColor();
UpdateData(FALSE);
if(penb.Detach())
{
penb.DeleteObject();
penb.CreatePen(PS_SOLID,1,col);
}
}
}
void CFractalesDlg::OnSite()
{
ShellExecute(0,"open","http://www.aranor.fr.st",0,0,SW_NORMAL);
}
Conclusion :
Programme fait en 2002, par AranoR
"It is pointless to resist"
www.aranor.fr.st (dernières mises à jour, exécutables, et des bonus !)
:-)
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.