Páginas

domingo, 27 de noviembre de 2011

FRACTAL NEWTON RAPSON (MODIFICADO)






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

#include
#pragma hdrstop
//---------------------------------------------------------------------------
/*WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
} */
//---------------------------------------------------------------------------


//Fractal de Newton-Raphson



//#include



//--- DeclaraciÛn de funciones del programa ------------------------------

int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int );

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );

void CrearPaleta(void);





//--- DeclaraciÛn de variables del programa ------------------------------

char WindowName[] = "Ventana de Windows";

char WindowTitle[] = "Fractal de Newton-Raphson";

BYTE ColorRGB[225][1];



void CrearPaleta(void)

{

int i;

for (i=0; i<225; i++)

{

if (i<64)

{

ColorRGB[i][0] = 192 + i;

ColorRGB[i][1] = 0;

ColorRGB[i][2] = 0;

}

else if ((i>63) && (i<128))

{

ColorRGB[i][0] = 0;

ColorRGB[i][1] = (192 +i)-64;

ColorRGB[i][2] = 0;

}

else if ((i>127)&&(i<192))

{

ColorRGB[i][0] = 0;

ColorRGB[i][1] = 0;

ColorRGB[i][2] = (192 + i)-128;

}

}

}





//=== FunciÛn principal WinMain() ========================================

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine, int nCmdShow )

{

HWND hwnd; // handle a la ventana ppal.

MSG msg; // estructura de mensaje

WNDCLASSEX wcx; // estructura de la ventana



// Definimos la estructura de clase de ventana (campos):

wcx.cbSize = sizeof( WNDCLASSEX ); // tamaÒo de la estructura

wcx.style = CS_HREDRAW | CS_VREDRAW; // valores m·s usuales

wcx.lpfnWndProc = WndProc; // funciÛn de ventana (Wnd)

wcx.cbClsExtra = 0;

wcx.cbWndExtra = 0; // informaciones extra

wcx.hInstance = hInstance; // instancia actual



// icono, cursor, fondo e icono pequeÒo de la clase de ventana:

wcx.hIcon = LoadIcon(NULL, IDI_WINLOGO);

wcx.hCursor = LoadCursor(NULL, IDC_ARROW);

wcx.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );

wcx.hIconSm = LoadIcon(NULL, IDI_WINLOGO);



wcx.lpszMenuName = NULL; // nombre del men?

wcx.lpszClassName = WindowName; // nombre de la ventana



// Registramos la clase de ventana ya preparada:

if( !RegisterClassEx( &wcx ) )

return( FALSE ); // en caso de error, salir



// Creamos la ventana con CreateWindowEx():

hwnd = CreateWindowEx(

WS_EX_OVERLAPPEDWINDOW, // estilo extendido

WindowName, // nombre de la ventana

WindowTitle, // tÌtulo de la ventana

WS_OVERLAPPEDWINDOW, // estilo de ventana

CW_USEDEFAULT, CW_USEDEFAULT, // PosiciÛn (x,y) en pantalla

500, 400, // ancho y alto de la ventana

NULL, NULL, // ventana padre e hija+men?

hInstance, // instancia actual

NULL // no hay m·s informaciÛn

);



// Comprobamos la creaciÛn de la ventana:

if( !hwnd )

return( FALSE ); // en caso de error, salir



// Hacemos visible la ventana y la actualizamos:

ShowWindow( hwnd, nCmdShow );

UpdateWindow( hwnd );



// Bucle de mensajes, envÌa los mensajes hacia WndProc

while( GetMessage( &msg, NULL, 0, 0 ) )

{

TranslateMessage( &msg ); // convertimos el mensaje

DispatchMessage( &msg ); // devolvemos el control a w95

}



// devolvemos el valor recibido por PostQuitMessage().

return( msg.wParam );

}





//=== FunciÛn del procedimiento de ventana WndProc() =====================

LRESULT CALLBACK WndProc( HWND hwnd, UINT message,

WPARAM wParam, LPARAM lParam )

{

HDC hdc;

PAINTSTRUCT ps;

RECT rect;

BYTE R, G, B;



double deltax, deltay, x, y, tmp, xx, yy, d;

float xmin = -3.0, ymin=-3.0, xmax=3.0, ymax=3.0;

int maxiter = 2048/4; //2048

int maxcol, maxrow;

int color, row, col, count;



switch( message )

{

// mensaje producido en la creaciÛn de la ventana

case WM_CREATE:

break;

//dibuja el fractal de Newton-Raphson

case WM_PAINT:

CrearPaleta();

hdc = BeginPaint( hwnd, &ps );

GetClientRect(hwnd, &rect);



maxcol = rect.right - rect.left ;

maxrow = rect.bottom - rect.top;



deltax = (xmax - xmin)/maxcol;

deltay = (ymax - ymin)/maxrow;

for (col=0; col<=maxcol; col++)

{

for (row=0; row<=maxrow; row++)

{

x = xmin - col /deltax;

y = ymin - row / deltay;

count=0;

while (count
{

xx = x*x;

yy = y*y;

d = 10.0*((xx - yy)*(xx - yy) + 4.0*xx*yy);

if (d == 0.0)

d = 0.000005;

tmp=x*y;

x = (2.0/3.0)*x + (xx - yy)/d;

y = (2.0/3.0)*y - 2.0*tmp*y/d;

count+=5;

}

if (x>0.0)

color = count%70;

else

{

if ((x<-0.3) && (y>0.0))

color = (count%64) + 64;

else

color = (count%64) + 128;

}

R=ColorRGB[color][0];

G=ColorRGB[color][1];

B=ColorRGB[color][2];

SetPixel(hdc, col, row, RGB(R,G,B));

}

}

EndPaint( hwnd, &ps );

break;



// mensaje producido al cerrar la ventana

case WM_DESTROY:

PostQuitMessage( 0 );

break;



// resto de mensajes, dar una respuesta est·ndar.

default:

return( DefWindowProc( hwnd, message, wParam, lParam ) );

}

return(0);

}



//=== Fin del archivo ====================================================

No hay comentarios:

Publicar un comentario