All pastes #437767 Raw Edit

Mine

public text v1 · immutable
·published 2007-04-13 07:50 UTC
rendered paste body
#include <cstdio>
#include <conio.h>
#include <complex>
using std::complex;

//corners of calculated part of fractal
//initial values
complex<double> LTop(-2.0, 1.3);
complex<double> RBot(0.6, -1.3);

//when user moves the screen
//this is the "speed"
const double dRealOnMove = 0.1;
const double dImagOnMove = 0.1;

//when user zooms in/out
//the "speed", as above
const double dZoom = 0.04;

//default num of iterations
unsigned int IterCount = 50;


//size of displayed fractal, in characters
const unsigned int Width = 79;
const unsigned int Height = 69;

//used to draw fractal
const char CharTable[] = " --===++++******************************#";
const unsigned int CharCount = strlen(CharTable)-1;


void DrawMandelbrotSet()
{
     unsigned int i, j, k;
     complex<double> CurrentPoint, Iteration;
     const double dReal = (RBot.real() - LTop.real()) / (double)(Width-1);
     const double dImag = (RBot.imag() - LTop.imag()) / (double)(Height-1);
     
     char Output[Height*(Width+1)+1];

     
     for(i=0; i<Height; ++i) {
        for(j=0; j<Width; ++j) {
            CurrentPoint.real() = LTop.real() + dReal*(double)j;
            CurrentPoint.imag() = LTop.imag() + dImag*(double)i;
            Iteration.real() = 0.0;
            Iteration.imag() = 0.0;
            for(k=0; k<IterCount; ++k) {
               Iteration *= Iteration;
               Iteration += CurrentPoint;
               if (Iteration.real()*Iteration.real()+Iteration.imag()*Iteration.imag() >= 4.0)
                  break;
            }
            Output[i*(Width+1)+j] = CharTable[k*CharCount/IterCount];
        }
        Output[i*(Width+1)+Width] = '\n';
     }
     Output[Height*(Width+1)] = '\0';
     puts(Output);
}


int main()
{
    printf("Program rysuje zbior mandelbrota.\n"
           "Ilosc iteracji (im wiecej tym dokladniej, ale dluzej\n"
           "trwaja obliczenia. %u powinno wystarczyc) ", IterCount);
    
    IterCount = 0;
    while (IterCount == 0) //to prevent user from typing 0
        while (scanf("%u", &IterCount) != 1)
             getc(stdin);
    char c;
    printf("Maksymalizuj okno konsoli i wsisnij dowolny klawisz");
    getch();
    do {
      system("cls");
      DrawMandelbrotSet();
      //printf("%d", c);
      printf("Sterowanie:\n"
             "kursory - przesuwanie\n"
             "+, -    - przyblizenie, oddalenie\n"
             "[esc]   - wyjscie\n"
             "\n"
             "Aktualne narozniki:\n"
             "%.4lf + %.4lfi\n"
             "%.4lf + %.4lfi\n", LTop.real(), LTop.imag(), RBot.real(), RBot.imag());
      c=getch();
      if (c==-32) {
         double temp;
         c=getch();
         switch(c) {
            case 75: //left arrow
                 temp = RBot.real() - LTop.real();
                 LTop.real() -= dRealOnMove*temp;
                 RBot.real() -= dRealOnMove*temp;
                 break;
            case 77: //right arrow
                 temp = RBot.real() - LTop.real();
                 LTop.real() += dRealOnMove*temp;
                 RBot.real() += dRealOnMove*temp;
                 break;
            case 72: //up arrow
                 temp = RBot.imag() - LTop.imag();
                 LTop.imag() -= dImagOnMove*temp;
                 RBot.imag() -= dImagOnMove*temp;
                 break;
            case 80: //down arrow
                 temp = RBot.imag() - LTop.imag();
                 LTop.imag() += dImagOnMove*temp;
                 RBot.imag() += dImagOnMove*temp;
                 break;
         }
      } else {
         double temp;
         switch(c) {
             case '-':
                  temp = RBot.real() - LTop.real();
                  LTop.real() -= dZoom*temp;
                  RBot.real() += dZoom*temp;
                  temp = RBot.imag() - LTop.imag();
                  LTop.imag() -= dZoom*temp;
                  RBot.imag() += dZoom*temp;
                  break;
             case '+':
                  temp = RBot.real() - LTop.real();
                  LTop.real() += dZoom*temp;
                  RBot.real() -= dZoom*temp;
                  temp = RBot.imag() - LTop.imag();
                  LTop.imag() += dZoom*temp;
                  RBot.imag() -= dZoom*temp;
                  break;
         }
      }
    }
    while(c!=27); //until esc pressed
    
    return 0;
}