Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Центр помощи > [C++ Builder]Кривая коха


Автор: mego4el 10.7.2011, 13:23
Доброго дня. Никак не получается нормально построить кривую Коха, и вывести её графически на экран на форме (C++ Builder). Есть кусок кода, но как я его не крутил, ничего не получилось((, помогите пожалуйста!

Код
#include <math.h>

  double t, x, y, p;
  long k  ;
  int mx, my, rad  ;
  long int itter=50000;
  mx = 10;
  my = 200;
  rad =600;
  Randomize;
  x=0.0;
  y=0.0;

   for(k=1; k<=itter; k++)
    {
     p = random(2);
     t = x;
    if (p <= 0.5)
      {
      x =  0.5 * x + 1/(2*pow(3,0.5)) * y;
      y =  1/(2*pow(3,0.5)) * t - 0.5 * y;
      }
     else
     {
        x =  0.5 * x - 1/(2*sqrt(3)) * y +0.5;
        y =  -1/(2*sqrt(3)) * t - 0.5 * y + 1/(2*sqrt(3));
        }
        pole->Canvas->Pixels[mx + int(rad * x)][my - int(rad * y)]=RGB(25,25,0);
     }



Автор: UniBomb 11.7.2011, 08:42
mego4el, а в чём проблема? Код абсолютно рабочий  smile Если без формошлёпства, то вот:

main.h
Код

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private:    // User declarations
public:        // User declarations
    __fastcall TForm1(TComponent* Owner);
    TImage *pole;
    TButton *start;
    void __fastcall StartButtonClick(TObject *Sender);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


main.cpp
Код

//---------------------------------------------------------------------------

#include <vcl.h>
#include <math.h>
#pragma hdrstop

#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    Form1->Width = 660;
    Form1->Height = 320;
    Form1->Caption = "Кривая Коха";

    pole = new TImage(Form1);
    pole->Left = 5;
    pole->Top = 5;
    pole->Width = 640;
    pole->Height = 240;
    pole->Parent = Form1;

    start = new TButton(Form1);
    start->Left = 5;
    start->Top = 255;
    start->Width = 640;
    start->Parent = Form1;    
    start->Caption = "Построить";
    start->OnClick = StartButtonClick;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StartButtonClick(TObject *Sender)
{
    double t, x, y, p;
    long k  ;
    int mx, my, rad  ;
    long int itter=50000;
    mx = 10;
    my = 200;
    rad =600;
    Randomize;
    x=0.0;
    y=0.0;
    for(k=1; k<=itter; k++)
    {
        p = random(2);
        t = x;
        if (p <= 0.5)
        {
            x =  0.5 * x + 1/(2*pow(3,0.5)) * y;
            y =  1/(2*pow(3,0.5)) * t - 0.5 * y;
        }
        else
        {
            x =  0.5 * x - 1/(2*sqrt(3)) * y +0.5;
            y =  -1/(2*sqrt(3)) * t - 0.5 * y + 1/(2*sqrt(3));
        }
        pole->Canvas->Pixels[mx + int(rad * x)][my - int(rad * y)]=RGB(25,25,0);
    }    
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
    delete start;
    delete pole;    
}
//---------------------------------------------------------------------------


user posted image


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)