#include <windows.h>
#include <iostream>
#include <stdio.h>

HBITMAP g_hbmBall = NULL;


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

char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil){
    HWND hwnd;               
    MSG messages;            
    WNDCLASSEX wincl;        
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;     
    wincl.style = CS_DBLCLKS;                
    wincl.cbSize = sizeof (WNDCLASSEX);
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL; 
    wincl.cbClsExtra = 0;      
    wincl.cbWndExtra = 0;      
    wincl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);


    if (!RegisterClassEx (&wincl))
        return 0;

    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Logistische Gleichung - http://hanno-rein.de",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           800,                 /* The programs width */
           600,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    ShowWindow (hwnd, nFunsterStil);

    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}




void drawit(HDC hdc,  RECT * pRect){
            int x0 = pRect->left;
        	int y0 = pRect->top;
        	int x1 = pRect->right;
        	int y1 = pRect->bottom-15;
        	int cy = y1 - y0;
        	MoveToEx(hdc, x0+20, y0, NULL); 
            LineTo(hdc, x0+20, y1);
            LineTo(hdc, x1, y1);
  
            for(int i=0;i<=10;i++){
                     char my_string[3]; //To hold . and null
                     sprintf(my_string,"%1.1f",1-i/10.);
                     if (i!=10){
                     TextOut(hdc,x0,(int)((cy)/10.*i),my_string,3);
                     }
                     MoveToEx(hdc, x0,(int)((cy)/10.*i), NULL);                  
                     LineTo(hdc, x0+20,(int)((cy)/10.*i));
            }
        	x0+=20;
        	int cx = x1 - x0;
        	
            double a_min =    2.8;
            double a_max   =    4;
            double a_delta   =    (a_max-a_min)/(double)cx;
            double x_delta =   1/(double)cy;
            int    iter    =    500;
            for(double a=a_min;a<a_max;a=a+a_delta){
                       int xprint=x0+(int)((double)cx*(a-a_min)/(a_max-a_min));
                       for(double x_start = x_delta;x_start<=1;x_start+=x_delta){
                                  double x_temp = x_start;
                                  for(int i=0;i<iter;i++){
                                          // Logistische Gleichung:
                                          x_temp=a*x_temp*(1-x_temp);
                                  }
                                  int colorit = GetRValue(GetPixel(hdc,xprint,y0+(int)(cy-cy*x_temp)));
                                  if (colorit >=50){
                                              colorit-=50;
                                              }
                                  SetPixelV(hdc,xprint,y0+(int)(cy-cy*x_temp),RGB(colorit,colorit,colorit));
                       }
                       char my_string[22]; 
                       sprintf(my_string,"a=%2.3f",a);
                       TextOut(hdc,xprint,y1+1,my_string,7);
            }                       
}


LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC         hdc;
    
            
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);     
        break;
            
        case WM_PAINT:
             InvalidateRect(hwnd,NULL,TRUE);
		     hdc = BeginPaint(hwnd, &ps);
			 RECT rt;
           	 GetClientRect(hwnd, &rt);
             drawit(hdc, &rt);
			 EndPaint(hwnd, &ps);
		break;
          
        default:                     
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}






        

