duci.ro FORUM at forumco.com
duci.ro FORUM at forumco.com
Home | Profile | Register | Active Topics | Active Polls | Members | Private Messages | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?




 All Forums
 ProgrammingPool environment
 Functiile WinAPI32
 Exemplu WinApi32-Graficul unei functii-C++
 Forum Locked
 Send Topic to a Friend
 Printer Friendly
Author Previous Topic Topic Next Topic  

duci
Forum Admin


210 Posts

Posted - 08/09/2005 :  18:37:20  Show Profile  Email Poster Send duci a Private Message
#include <windows.h>
#include <math.h>

#define CM_OPEN 100
#define CM_SAVE 101

HMENU hMenu1=CreateMenu(),hMenu2=CreateMenu();
HDC hdc;/* "Device context" pentru desen */
PAINTSTRUCT ps;/* Structura pentru desen */
RECT rc;/* Dreptunghiul care da dimensiunea desenului*/

double y,a,b,h,may,miy, v[640];
int ii,i,j,k;
double Pi=4*atan(1.0);

double func(double x)
{
return sin(x*Pi/180+1e-3)/(x*Pi/180+1e-3)*1000;
}
double MAX(double a, double b)
{
if (a>b) return a; else return b;
}
double MIN(double a, double b)
{
if (a<b) return a; else return b;
}

LRESULT CALLBACK MainWndProc (HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
a=0.01; // valoarea inferioara pentru x
b=3000.; // valoarea superioara a lui x
h=(b-a)/640;// pasul in reprezentarea grafica
for (i=0;i<640;v[i]=func(a+i*h),i++);//vectorul datelor
may=v[0];//******** scalarea
miy=v[0];
for (i=1;i<640;may=MAX(may,v[i]),miy=MIN(miy,v[i]),i++);
switch (nMsg)// actualizarea ferestrei
{
case WM_DESTROY:
PostQuitMessage (0);
return 0;
break;

case WM_COMMAND:
if ( HIWORD(wParam) == BN_CLICKED )
{
hdc = GetDC (hwnd);
if(LOWORD(wParam)==100)//Primul buton
{//***** trasarea graficului propriu zis
k=(int)(300-(v[1]-miy)/(may-miy)*300);
MoveToEx(hdc,1,k,0);// originea
for(ii=1;ii<640;ii++)
{
k=(int)(300-(v[ii]-miy)/(may-miy)*300);
LineTo(hdc,ii,k);
}
} //grafic
if(LOWORD(wParam)==101)//al doilea buton
{// ***** Linie intrerupta la valoarea 0 a functiei
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
k=(int)(300+miy/(may-miy)*300);
MoveToEx(hdc,1,k,0);
for(i=1;i<640;i+=3)
{
MoveToEx(hdc,i,k,0);
LineTo(hdc,i+1,k);
}
}//linie la 0
ReleaseDC(hwnd,hdc);
}//if buton apasat
return 0;
break;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint (hwnd, &ps);
return 0;
break;
} //end switch
return DefWindowProc (hwnd, nMsg, wParam, lParam);
}

int STDCALL WinMain (HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpCmd,int nShow)
{
HWND hwndMain;/* "Handle", identificator al ferestrei principale*/
MSG msg;/* O structura a mesajelor Win32 */
WNDCLASSEX wndclass;/* O structura a clasei ferestrei */
char*szMainWndClass = "WinTestWin"; /* Eticheta cu numele ferestrei */
/* La inceput cream clasa aferenta ferestrei noastre */

/* Initializeaza la 0 intreaga structura */
memset (&wndclass, 0, sizeof(WNDCLASSEX));

/* Numele clasei este WinTestWin */
wndclass.lpszClassName = szMainWndClass;

/* cbSize da dimensiunea structurii pentru extensibilitate */
wndclass.cbSize = sizeof(WNDCLASSEX);

/* Toate ferestrele acestei clase sunt redesenate la redimensionare */
wndclass.style = CS_HREDRAW | CS_VREDRAW;// | CS_OWNDC|CS_SAVEBITS;

/* Toate ferestrele acestei clase folosesc functia
MainWndProc */
wndclass.lpfnWndProc = MainWndProc;

/* Clasa ferestrei este asociata acestei ferestre */
wndclass.hInstance = hInst;

/* Foloseste icoana si cursoarele standard
ale sistemului de operare */
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);

/* Culoarea de fond este alb */
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);

/* Inregistreaza fereastra */
RegisterClassEx (&wndclass);

/* Creaza fereastra principala */
hwndMain = CreateWindow (
szMainWndClass,/* Numele ferestrei (clasei)*/
"Graficul unei functii",/* Eticheta ferestrei */
WS_OVERLAPPEDWINDOW,/* Stiul */
CW_USEDEFAULT,/* x initial (foloseste valoarea implicita) */
CW_USEDEFAULT,/* y initial (foloseste valoarea implicita) */
CW_USEDEFAULT,/* dimensiunea x initiala (foloseste valoarea implicita) */
CW_USEDEFAULT,/* dimensiunea y initiala (foloseste valoarea implicita) */
NULL,/* Nu avem fereastra parinte */
NULL,/* Fara meniu */
hInst,/* "Instanta" acestui program */
NULL/* Parametri de creatie */
);

AppendMenu(hMenu2, MF_STRING, CM_OPEN, "&Grafic");
AppendMenu(hMenu2, MF_SEPARATOR, 0, NULL);
AppendMenu(hMenu2, MF_STRING, CM_SAVE, "Linie la &y=0");
SetMenu (hwndMain, hMenu2);

/* Afiseaza fereastra creata, folosind nShow, care permite pornirea cu fereastra minimizata */
ShowWindow (hwndMain, nShow);
UpdateWindow (hwndMain);
/* Principala bucla de mesaje. Toate mesajele trimise catre
sistemul de operare sunt receptionate prin apelarea functiei
GetMessage care sunt apoi retranmise procedurii corespunzatoare
din programul utilizatorului. Aceasta este cea mai simpla bucla
de mesaj. Cand una dintre ferestre cheama PostQuitMessage,
functia GetMessage returneaza 0 si parametrul wParam al mesajului
va contine argumentul dat de PostQuitMessage. Bucla va fi terminata
iar aplicatia inchisa */
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}


Prof.Dr. D. Ciurchea

Google AdSense

USA
Mountain View


duci
Forum Admin



210 Posts

Posted - 05/29/2007 :  08:36:11  Show Profile  Email Poster Send duci a Private Message
#include <windows.h>
#include <math.h>
#include <stdlib.h>
#define N 640
double y,a,b,h,may,miy, v[N],v1[N],
v2[N],ss1[N],
r1=pow(2,15),cc[N],ss[N],ss0[N];
double d[N],d1[N],c,s,c1,s1,q,da,mic,mac;
int ii,i,j,k,scy=200,scalay=500, ifil=50;
double pi=4*atan(1.0);
double func(double x)
{
double a,b;
//a=(x)/30; b=1000*exp(-a*a);
// return b+2*(rand()/r1-0.5)*sqrt(b);
if(x<100)return 1;else return 0;
}
double MAX(double a, double b)
{
if (a>b) return a; else return b;
}
double MIN(double a, double b)
{
if (a<b) return a; else return b;
}

LRESULT CALLBACK
MainWndProc (HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
/* Identificatorii "handle" al butoanelor. */

static HWND hwndButton = 0;
static HWND hwndButton1 = 0;
static HWND hwndButton2 = 0;
static int cx, cy;/* Inaltimea si latimea butoanelor. */
int apasat1=0,apasat2=0;
HDC hdc;/* "Device context" pentru desen */
PAINTSTRUCT ps;/* Structura pentru desen */
RECT rc;/* Dreptunghiul care da dimensiunea desenului*/
TEXTMETRIC tm;//Valorile sistemului pentru dimensiunea caracterelor
HBRUSH hBrush;
/***********************/
switch (nMsg)// actualizarea ferestrei
{
case WM_CREATE:
hdc = GetDC (hwnd);
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
GetTextMetrics (hdc, &tm);
cx = tm.tmAveCharWidth * 10;
cy = (tm.tmHeight + tm.tmExternalLeading) * 2;
ReleaseDC (hwnd, hdc);
/* Cream butoanele*/
hwndButton = CreateWindow (
"button",// Cuvantul cheie pentru BUTON
"Original",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0,0, cx, cy,/*pozitie initiala-neimportant*/
hwnd,// Fereastra parinte.
(HMENU) 1,// Numarul de identificare al butonului: 1
((LPCREATESTRUCT) lParam)->hInstance,
NULL );
hwndButton1 = CreateWindow (
"button",// Cuvantul cheie pentru BUTON
"Fara filtraj",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0,0, cx, cy,
hwnd,// Fereastra parinte.
(HMENU) 2,// Numarul de identificare al butonului: 2
((LPCREATESTRUCT) lParam)->hInstance,
NULL);
hwndButton2 = CreateWindow (
"button",// Cuvantul cheie pentru BUTON
"Trunchiere",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0,0, cx, cy,/*pozitie initiala-neimportant*/
hwnd,// Fereastra parinte.
(HMENU) 3,// Numarul de identificare al butonului: 3
((LPCREATESTRUCT) lParam)->hInstance,
NULL );



// sfarsitul secventei de constructie a butoanelor
return 0;
break;

case WM_DESTROY:
PostQuitMessage (0);
return 0;
break;
case WM_COMMAND:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rc);
if ( HIWORD(wParam) == BN_CLICKED )
{
if(LOWORD(wParam)==1)//Primul buton
{
hdc = GetDC (hwnd);
SelectObject (hdc, CreatePen(PS_SOLID, 1, RGB(0,255, 0))) ;
k=(int)(scalay-(v[0]-miy)/(may-miy)*scy);
MoveToEx(hdc,0,k,0);// originea
for(ii=0;ii<N;ii++)
{
k=(int)(scalay-(v[ii]-miy)/(may-miy)*scy);
Ellipse(hdc,ii-5,k-5,ii+5,k+5);
}
// SelectObject (hdc, CreatePen(PS_SOLID, 1, RGB(110,0,110))) ;
k=(int)(scy-(cc[0]-mic)/(mac-mic)*scy);
MoveToEx(hdc,0,k,0);// originea
for(ii=0;ii<N;ii++)
{
k=(int)(scy-(cc[ii]-mic)/(mac-mic)*scy);
Rectangle(hdc,ii-5,k-5,ii+5,k+5);
}
ReleaseDC (hwnd, hdc);
ValidateRect (hwnd, &rc) ;
} //grafic

if(LOWORD(wParam)==2)//al doilea buton
{
hdc = GetDC (hwnd);
SelectObject (hdc, CreatePen(PS_SOLID, 1, RGB(255,0, 0))) ;
k=(int)(scalay-(v1[0]-miy)/(may-miy)*scy);
MoveToEx(hdc,0,k,0);// originea
for(ii=0;ii<N;ii++)
{
k=(int)(scalay-(v1[ii]-miy)/(may-miy)*scy);
LineTo(hdc,ii,k);
}
k=(int)(scy-(ss[0]-mic)/(mac-mic)*scy);
MoveToEx(hdc,0,k,0);// originea
for(ii=0;ii<N;ii++)
{
k=(int)(scy-(ss[ii]-mic)/(mac-mic)*scy);
LineTo(hdc,ii,k);
}
ReleaseDC (hwnd, hdc) ;
ValidateRect (hwnd, &rc) ;
}//linie la 0
}//if buton apasat
EndPaint (hwnd, &ps);
if(LOWORD(wParam)==3)//Al treilea buton
{
ifil--;
for(i=0;i<N;ss1[i]=ss0[i]=ss[i],v2[i]=0,i++);
for(i=ifil;i<=N/2;ss0[i]=ss0[N-i]=0,i++);//filtrajul propus
da=2*pi/N;
for(i=0;i<N;v2[i]=0,i++);
for(j=0;j<N;j++)
{
c1=1.0 ;
s1=0.0 ;
c=cos(j*da);
s=sin(j*da);
for(i=0;i<N;i++)
{
q=c*c1-s*s1;
s1=c1*s+s1*c;
c1=q;
v2[j]+=c1*ss0[i];
}}


hdc = GetDC (hwnd);
SelectObject (hdc, CreatePen(PS_SOLID, 1, RGB(0,0,255)));
k=(int)(scalay-(v2[0]-miy)/(may-miy)*scy);
MoveToEx(hdc,0,k,0);// originea
for(ii=0;ii<N;ii++)
{
k=(int)(scalay-(v2[ii]-miy)/(may-miy)*scy);
LineTo(hdc,ii,k);
}
k=(int)(scy-(ss0[0]-mic)/(mac-mic)*scy);
MoveToEx(hdc,0,k,0);// originea
for(ii=0;ii<N;ii++)
{
k=(int)(scy-(ss0[ii]-mic)/(mac-mic)*scy);
LineTo(hdc,ii,k);
}
ReleaseDC (hwnd, hdc);
ValidateRect (hwnd, &rc) ;
} //grafic

return 0;
break;

case WM_SIZE: // Schimbarea dimensiunii ferestrei
/* Plaseaza butoanele la dreapta */
if (
(hwndButton ||hwndButton1)
&&
(wParam == SIZEFULLSCREEN || wParam == SIZENORMAL)
)
{
// Dimensiunile dreptunghiului de referinta "rc"
rc.left =641;
rc.top = HIWORD (lParam)*3/4-cy/2;
MoveWindow (hwndButton,rc.left, rc.top, cx, cy, TRUE);
MoveWindow (hwndButton1,rc.left, rc.top-200, cx, cy, TRUE);
MoveWindow (hwndButton2,rc.left, rc.top-100, cx, cy, TRUE);
UpdateWindow(hwnd);
}
return 0;
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rc);
rc.top=scy-100;rc.left=(rc.right+rc.left)/2-250;
DrawText (hdc, "Functia ",-1, &rc,DT_SINGLELINE | DT_CENTER |DT_VCENTER);
rc.top-=450;
// rc.left=(rc.right+rc.left)/2-50;
DrawText (hdc, "Transformata Fourier ",-1, &rc,DT_SINGLELINE | DT_CENTER |DT_VCENTER);
/* ************************** */
EndPaint (hwnd, &ps);
return 0;
break;

} //end switch
return DefWindowProc (hwnd, nMsg, wParam, lParam);
}

int STDCALL WinMain (HINSTANCE hInst,
HINSTANCE hPrev,
LPSTR lpCmd,
int nShow)
{
HWND hwndMain;/* "Handle", identificator al ferestrei principale*/
MSG msg;/* O structura a mesajelor Win32 */
WNDCLASSEX wndclass;/* O structura a clasei ferestrei */
char*szMainWndClass = "WinTestWin"; /* Eticheta cu numele ferestrei */
/* La inceput cream clasa aferenta ferestrei noastre */

/* Initializeaza la 0 intreaga structura */
memset (&wndclass, 0, sizeof(WNDCLASSEX));

/* Numele clasei este WinTestWin */
wndclass.lpszClassName = szMainWndClass;

/* cbSize da dimensiunea structurii pentru extensibilitate */
wndclass.cbSize = sizeof(WNDCLASSEX);

/* Toate ferestrele acestei clase sunt redesenate
la redimensionare */
wndclass.style = CS_HREDRAW | CS_VREDRAW;

/* Toate ferestrele acestei clase folosesc functia
MainWndProc */
wndclass.lpfnWndProc = MainWndProc;

/* Clasa ferestrei este asociata acestei ferestre */
wndclass.hInstance = hInst;

/* Foloseste icoana si cursoarele standard
ale sistemului de operare */
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);

/* Culoarea de fond este alb */
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);

/* Inregistreaza fereastra */
RegisterClassEx (&wndclass);
/*
Aici calculam functia si transformatele Fourier.
In cazul unor interactiuni dinamice date de butoane,
aceste module trebuie mutate in modulul"case WM_COMMAND"
*/

a=0.0; // valoarea inferioara pentru x
b=N; // valoarea superioara a lui x
h=(b-a)/N;// pasul in reprezentarea grafica
for (i=0;i<=N/2;v[i]=v[N-i]=func(a+i*h),i++);//vectorul datelor
// for (i=0;i<N;v[i]=func(a+i*h),i++);//vectorul datelor

//******** scalarea

may=v[0];
miy=v[0];
for (i=1;i<N;may=MAX(may,v[i]),miy=MIN(miy,v[i]),i++);
/* Transformarea Fourier */
/* directa*/

da=-2*pi/N;
for(j=0;j<N;cc[j]=0,j++);
for(j=0;j<N;j++)
{
c=cos(da*j);
s=sin(da*j);
c1=1.0;
s1=0.0;
for(i=0;i<N;i++)
{
q=c*c1-s*s1 ;
s1=c1*s+s1*c;
c1=q;
cc[i]+=c1*v[j];
}
}
for(i=0;i<N;cc[i]/=N,ss[i]=cc[i],i++);
mac=cc[0];//scalare coef. Fourier
mic=cc[0];
for (i=1;i<N;mac=MAX(mac,cc[i]),mic=MIN(mic,cc[i]),i++);


/* inversa */
da=2*pi/N;
for(i=0;i<N;v1[i]=0,i++);
for(j=0;j<N;j++)
{
c1=1.0 ;
s1=0.0 ;
c=cos(j*da);
s=sin(j*da);
for(i=0;i<N;i++)
{
q=c*c1-s*s1;
s1=c1*s+s1*c;
c1=q;
v1[j]+=c1*ss[i];
}}


/* Creaza fereastra principala */

hwndMain = CreateWindow (
szMainWndClass,/* Numele ferestrei (clasei)*/
"Efectele trunchierii in transformata Fourier",/* Eticheta ferestrei */
WS_OVERLAPPEDWINDOW,/* Stiul */
CW_USEDEFAULT,/* x initial (foloseste valoarea implicita) */
CW_USEDEFAULT,/* y initial (foloseste valoarea implicita) */
750,//CW_USEDEFAULT,/* dimensiunea x initiala (foloseste valoarea implicita) */
550,//CW_USEDEFAULT,/* dimensiunea y initiala (foloseste valoarea implicita) */
NULL,/* Nu avem fereastra parinte */
NULL,/* Fara meniu */
hInst,/* "Instanta" acestui program */
NULL/* Parametri de creatie */
);
/* Afiseaza fereastra creata, folosind nShow,
care permite pornirea cu fereastra minimizata */
ShowWindow (hwndMain, nShow);
UpdateWindow (hwndMain);
/* Principala bucla de mesaje. Toate mesajele trimise catre
sistemul de operare sunt receptionate prin apelarea functiei
GetMessage care sunt apoi retranmise procedurii corespunzatoare
din programul utilizatorului. Aceasta este cea mai simpla bucla
de mesaj. Cand una dintre ferestre cheama PostQuitMessage,
functia GetMessage returneaza 0 si parametrul wParam al mesajului
va contine argumentul dat de PostQuitMessage. Bucla va fi terminata
iar aplicatia inchisa */
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}


Prof.Dr. D. Ciurchea
Go to Top of Page
  Previous Topic Topic Next Topic  
 Forum Locked
 Send Topic to a Friend
 Printer Friendly
Jump To:
duci.ro FORUM at forumco.com © 2000-05 ForumCo.com Go To Top Of Page
Generated in 0.27 seconds. Hello from Duci !!! Snitz Forums 2000
RSS Feed 1 RSS Feed 2
Powered by ForumCo 2000-2008
TOS - AUP - URA - Privacy Policy
ForumCo Free Blogs and Galleries
Signup for a free forum or Go Banner Free