Добавил:
sergun
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:II курс - курсовые / Маятник с колеблющейся точкой подвеса / Program
.c //Including the Libraries Required For:
#include <windows.h> //Window Usage / Handing & Etc...
#include "resource.h" //Certain Runtime Functions & Commands
#include <math.h> //Mathematical Fucntions
#include <stdio.h> //Input / Output Functions
// -= THE CONSTANTS =-
#define PI 3.141592653589793238462643383279502884197169 //Constant Pi Defenition
#define alpha0 0 //The Starting Position (Angle)
#define Omega 1.56 //The Period for the Anker Modulation
#define delta 0.25 //The Constant Defining the Energy Loss
#define w0 1.0 //The So-called Self-Frequency
//The Difinition Of The Runtime Functions:
LRESULT CALLBACK WindowFunc(HWND,UINT,WPARAM,LPARAM); //Window 1: Program Controls
LRESULT CALLBACK WindowFunc2(HWND,UINT,WPARAM,LPARAM); //Window 2: Phase Plain
LRESULT CALLBACK WindowFunc3(HWND,UINT,WPARAM,LPARAM); //Window 3: Puan-Kare Plain
BOOL CALLBACK DialogProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
//Define Thread
DWORD WINAPI ThreadProc(LPVOID lpParameter); //Here it is
HWND hwnd,hwnd2,hwnd3; //Setting up The Stuff For This Program to Work
HDC memdc,memdc2,memdc3; //...Continued...
PAINTSTRUCT paintstruct; //...Continued...
HINSTANCE ThisInst; //...Continued...
HBRUSH hbrush; //...Continued...
HANDLE hSem; //The Semaphore
HPEN PenR,PenG,PenB,PenBl,PenW; //Brushes Defenitions / Creations
char szWinName[] = "Program Controls", //The Name of the Control Window
szWinName2[] = "Phase Plain", //The Name of the Phase Plain Window
szWinName3[] = "Poincare Plain"; //The Name of the Poincare Plain Window
//Define Variables To Their Specific Class:
int keybrd=0, //The Variable For Keyboard Input
count=0, //The Number of the Cycle
endth=1; //The Variable for Cycling Continue
Phmit=0; //The Variable Showing Phase Plain Type (Unlimited Default)
// -= THE VARIABLES =-
double alpha=PI/3., //The Angle
alphp=0., //The Angle @ Puan-Kare
helpt=0., //Runtime Additional Variable (Time)
help1, //Runtime Additional Variable (Angle)
velocity=0., //The Velocity
t=0., //Time Variable
dt=.01, //Procedure's Step
A0=1., //Anker Modulation Force Variable
Cx=300., //Axis Position (Center) X
Cy=190., //Axis Position (Center) Y
Kx=54., //Zoom Multiplier X
Ky=54.; //Zoom Multiplier Y
// -= THE PROCEDURES =-
void Print(void); //The Procedure Updating The Windows
void Draw2(void); //The Procedure Drawing The Phase Plain
void Draw3(void); //The Procedure Drawing The Puan-Kare Plain
void Grid1(void); //The Procedure Drawing The Axis (Phase Plain)
void Grid2(void); //The Procedure Drawing The Axis (Puan-Kare Plain)
// -= THE FUNCTIONS =-
double changpi(double); //Check for -pi to pi Angle For Puan-Kare
double f1(double,double,double); //
double f2(double,double,double); // The
double k11(void); // Runge - Kutte
double k21(void); // Algorithm
double k31(void); // Functions
double k41(void); //
double k12(void); //
double k22(void); //
double k32(void); //
double k42(void); //
/*
--=======INITIALIAL=====SETUP=====ENDS=======--
*/
//The Window Initilization:
int WINAPI WinMain (HINSTANCE hThisInst, //No Comments...
HINSTANCE hPrevInst,
LPSTR lpszArgs,
int nWinMode )
{
MSG msg;
WNDCLASS wcl,wcl2,wcl3;
ThisInst = hThisInst;
wcl.hInstance=hThisInst; //Define The Class Of The First Window (Control Panel)
wcl.lpszClassName=szWinName; //Name Of The Window's Class
wcl.lpfnWndProc=WindowFunc; //Window's Function
wcl.style=0; //Default Style (Window's Attributes)
wcl.hIcon=LoadIcon(NULL,IDI_APPLICATION); //Default Icon (Window's Attributes)
wcl.hCursor=LoadCursor(NULL,IDC_ARROW); //Default Cursor (Window's Attributes)
wcl.lpszMenuName= (LPSTR)MYMENU; //The Menu's Name
wcl.cbClsExtra=0; //
wcl.cbWndExtra=0; //
wcl.hbrBackground=(HBRUSH) GetStockObject(LTGRAY_BRUSH);//Fill The Window With a Solid Color
if (!RegisterClass (&wcl)) return 0;
hwnd = CreateWindow(szWinName, //The Window's Creation (Set It's Class)
"Pendulum (Controls)", //Set The Name
WS_OVERLAPPEDWINDOW, //Always On Top False
CW_USEDEFAULT, //Horisontal Window Position (Deafult)
CW_USEDEFAULT, //Vertical Window Position (Deafult)
250, //The Width of the Window
45, //The Hight of the Window
HWND_DESKTOP, //The Rest isn't Important, Take it as it is...
NULL,
hThisInst,
NULL );
ShowWindow(hwnd,nWinMode); //Show the Window and Draw it's Contents
UpdateWindow (hwnd);
wcl2.hInstance=hThisInst; //Define The Class Of The First Window (Control Panel)
wcl2.lpszClassName=szWinName2; //Name Of The Window's Class
wcl2.lpfnWndProc=WindowFunc2; //Window's Function
wcl2.style=0; //Default Style (Window's Attributes)
wcl2.hIcon=LoadIcon(NULL,IDI_APPLICATION); //Default Icon (Window's Attributes)
wcl2.hCursor=LoadCursor(NULL,IDC_ARROW); //Default Cursor (Window's Attributes)
wcl2.lpszMenuName= NULL; //The Window Has NO Menu
wcl2.cbClsExtra=0;
wcl2.cbWndExtra=0;
wcl2.hbrBackground=(HBRUSH) GetStockObject(BLACK_BRUSH);//Fill The Window With a Solid Color
if (!RegisterClass (&wcl2)) return 0;
hwnd2 = CreateWindow(szWinName2, //The Window's Creation (Set It's Class)
"Phase Plain", //Set The Name
WS_OVERLAPPEDWINDOW, //Always On Top False
480, //Horisontal Window Position
0, //Vertical Window Position
600, //The Width of the Window
400, //The Hight of the Window
hwnd, //The Rest isn't Important, Take it as it is...
NULL,
hThisInst,
NULL );
ShowWindow(hwnd2,SW_SHOWNA); //Show the Window and Draw it's Contents
UpdateWindow (hwnd2);
wcl3.hInstance=hThisInst; //Define The Class Of The First Window (Control Panel)
wcl3.lpszClassName=szWinName3; //Name Of The Window's Class
wcl3.lpfnWndProc=WindowFunc3; //Window's Function
wcl3.style=0; //Default Style (Window's Attributes)
wcl3.hIcon=LoadIcon(NULL,IDI_APPLICATION); //Default Icon (Window's Attributes)
wcl3.hCursor=LoadCursor(NULL,IDC_ARROW); //Default Cursor (Window's Attributes)
wcl3.lpszMenuName= NULL; //The Window Has NO Menu
wcl3.cbClsExtra=0;
wcl3.cbWndExtra=0;
wcl3.hbrBackground=(HBRUSH) GetStockObject(BLACK_BRUSH);//Fill The Window With a Solid Color
if (!RegisterClass (&wcl3)) return 0;
hwnd3 = CreateWindow(szWinName3, //The Window's Creation (Set It's Class)
"Poin-care Plain", //Set The Name
WS_OVERLAPPEDWINDOW, //Always On Top False
480, //Horisontal Window Position
400, //Vertical Window Position
600, //The Width of the Window
400, //The Hight of the Window
hwnd, //The Rest isn't Important, Take it as it is...
NULL,
hThisInst,
NULL );
ShowWindow(hwnd3,SW_SHOWNA); //Show the Window and Draw it's Contents
UpdateWindow (hwnd3);
while (GetMessage (&msg, NULL, 0, 0)) //The Message Handling Cycle...
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
DeleteDC (memdc);
return msg.wParam;
}
#define MAX_THREAD_NUM 20
LRESULT CALLBACK WindowFunc(HWND hwnd, //First Window Setup (Control Panel)
UINT message,
WPARAM wParam,
LPARAM lParam)
{
HDC hdc;
HBITMAP hbit;
DWORD Tid;
static HANDLE hct;
static int maxX, maxY;
switch (message)
{
case WM_CREATE: //Window's Initialization Setup
hSem = CreateSemaphore(NULL,1,1,"mysem"); //Create A Semaphore
maxX = GetSystemMetrics (SM_CXSCREEN); //Max X Size (Get The Display's Resolution)
maxY = GetSystemMetrics (SM_CYSCREEN); //Max Y Size (Get The Display's Resolution)
hdc = GetDC(hwnd); //Recieve The Context Of The Device
memdc = CreateCompatibleDC(hdc); //Compatibale Context In RAM
hbit = CreateCompatibleBitmap (hdc,maxX,maxY); //Memory Location
//For The Window's Contents:
PenR=CreatePen (PS_SOLID,1,RGB(255,15,15)); //Pens Colors Defenition: (Red)
PenG=CreatePen (PS_SOLID,1,RGB(15,255,15)); //Green
PenB=CreatePen (PS_SOLID,1,RGB(15,15,255)); //Blue
PenBl=CreatePen (PS_SOLID,1,RGB(0,0,0)); //Black
PenW=CreatePen (PS_SOLID,1,RGB(255,255,255)); //White
SelectObject (memdc, hbit); //
hbrush = GetStockObject (WHITE_BRUSH); //Get The Brush
SelectObject (memdc, hbrush); //Choose The Brush
PatBlt (memdc, 0,0, maxX,maxY,PATCOPY); //Fill With the Brush The Copy of the Window in RAM
ReleaseDC (hwnd, hdc); //Free The Context
hbrush = GetStockObject (WHITE_BRUSH);
EnableMenuItem(GetMenu(hwnd),ID_STOP,MF_ENABLED);
EnableMenuItem(GetMenu(hwnd),ID_RUN,MF_GRAYED);
break;
case WM_PAINT: //The Displaing of The Window's Contents (Copy From RAM to the Screen)
hdc = BeginPaint(hwnd,&paintstruct);
BitBlt (hdc, 0, 0, maxX,maxY, memdc,0,0,SRCCOPY);
EndPaint(hwnd,&paintstruct);
break;
case WM_COMMAND: //Menu Options Handling...
switch (LOWORD(wParam))
{
case ID_THREAD_START: //Start The Progam Option...
endth=1;
hct = CreateThread(NULL, 0, ThreadProc, NULL, 0, &Tid);
EnableMenuItem( GetMenu(hwnd), ID_THREAD_STOP, MF_ENABLED);
EnableMenuItem( GetMenu(hwnd), ID_THREAD_RUN, MF_GRAYED);
EnableMenuItem( GetMenu(hwnd), ID_THREAD_START, MF_GRAYED);
break;
case ID_THREAD_RUN: //Continue Paused Program Option...
ResumeThread(hct);
EnableMenuItem( GetMenu(hwnd), ID_THREAD_STOP, MF_ENABLED);
EnableMenuItem( GetMenu(hwnd), ID_THREAD_RUN, MF_GRAYED);
break;
case ID_THREAD_STOP: //Pause The Program Option...
SuspendThread(hct);
EnableMenuItem( GetMenu(hwnd), ID_THREAD_RUN, MF_ENABLED);
EnableMenuItem( GetMenu(hwnd), ID_THREAD_STOP, MF_GRAYED);
break;
case ID_FILE_CLEAR: //Clear The PPase Plain Option...
hbrush = GetStockObject (WHITE_BRUSH);
SelectObject (memdc3, hbrush);
PatBlt (memdc3, 0,0, maxX,maxY,PATCOPY);//Fill The Whole Window With The Selected Above Brush (White) (W3)
SelectObject (memdc2, hbrush);
PatBlt (memdc2, 0,0, maxX,maxY,PATCOPY);//Fill The Whole Window With The Selected Above Brush (White) (W2)
Grid1(); //Redraw The Axis (W2)
Grid2(); //Redraw The Axis (W3)
InvalidateRect(hwnd2,NULL,FALSE); //And Update The Screen...
UpdateWindow(hwnd2);
InvalidateRect(hwnd3,NULL,FALSE); //And Update The Screen...
UpdateWindow(hwnd3);
break;
case ID_FILE_TERMINATE: //Halt Program, and Clear All Windows / Data Option...
endth=0; //Stop Program
Sleep(50); //Delay
hbrush = GetStockObject (WHITE_BRUSH); //Clear The Phase Plain Contents...
SelectObject (memdc2, hbrush);
PatBlt (memdc2, 0,0, maxX,maxY,PATCOPY);
Grid1();
InvalidateRect(hwnd2,NULL,FALSE);
UpdateWindow(hwnd2);
PatBlt (memdc3, 0,0, maxX,maxY,PATCOPY);//Clear The Poin-Care Plain Contents...
Grid2();
InvalidateRect(hwnd3,NULL,FALSE);
UpdateWindow(hwnd3);
t=0; //Update The Variables (Set Time = 0)
alpha=PI/3.; //Set The Startup Angle
velocity=0.; //Set The Initial Velocity
helpt=0.; //Set The Additional Variable (Time)
count=0; //Set The Number Of The Cycle
EnableMenuItem( GetMenu(hwnd), ID_THREAD_START, MF_ENABLED);
break;
case ID_FILE_EXIT: //The Exit Option...
if (MessageBox(hwnd, "Are You Sure?",
"Exit?",MB_YESNO | MB_ICONQUESTION) == IDYES) SendMessage(hwnd,WM_CLOSE, wParam, lParam);
break;
case ID_DIALOG_INPUTVALUES: //Input Variables Option...
DialogBox(ThisInst, MAKEINTRESOURCE(MYDIALOG),hwnd, DialogProc);
break;
case ID_HELP_CONTENTS: //View Help Contents Option...
WinHelp(hwnd,"PEND.HLP",HELP_CONTENTS,0);
break;
case ID_HELP_ABOUT: //Program Info Option...
MessageBox(hwnd,"The Oscillation Of a Pendulum With An Oscillationg Anker","Program Description:",MB_OK);
break;
}
break;
case WM_CHAR: //Handling Keyboard Input...
keybrd=LOWORD(wParam);
if(keybrd==108) //The L Button Pressed (Limiter On)
Phmit=1; //L]imit Phase Plain to 2*Pi...
if(keybrd==109) //The M Button Pressed (Limiter Off)
Phmit=0; //Unlimit Phase Plain...
if(keybrd==115) //The S Button Pressed (Start)
{
SendMessage(hwnd,WM_COMMAND,ID_THREAD_START,0);
}
if(keybrd==114) //The R Button Pressed (Run)
{
SendMessage(hwnd,WM_COMMAND,ID_THREAD_RUN,0);
}
if(keybrd==116) //The T Button Pressed (Stop)
{
SendMessage(hwnd,WM_COMMAND,ID_THREAD_STOP,0);
}
if(keybrd==99) //The C Button Pressed (Clear)
{
SendMessage(hwnd,WM_COMMAND,ID_FILE_CLEAR,0);
}
if(keybrd==100) //The D Button Pressed (Input Values)
{
SendMessage(hwnd,WM_COMMAND,ID_DIALOG_INPUTVALUES,0);
}
break;
case WM_CLOSE: //Program Closing
DestroyWindow(hwnd);
break;
case WM_DESTROY: //Program Termination
CloseHandle(hSem);
PostQuitMessage(0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK WindowFunc2(HWND hwnd2, //Second Window Setup (Phase Plain)
UINT message,
WPARAM wParam,
LPARAM lParam)
{
HDC hdc2;
HBITMAP hbit;
static int maxX, maxY;
switch (message)
{
case WM_CREATE: //Window's Initialization Setup
maxX = GetSystemMetrics (SM_CXSCREEN); //Max X Size (Get The Display's Resolution)
maxY = GetSystemMetrics (SM_CYSCREEN); //Max Y Size (Get The Display's Resolution)
hdc2 = GetDC(hwnd2); //Recieve The Context Of The Device
memdc2 = CreateCompatibleDC(hdc2); //Compatibale Context In RAM
hbit = CreateCompatibleBitmap (hdc2,maxX,maxY);
//For The Window's Contents:
SelectObject (memdc2, hbit); //
hbrush = GetStockObject (WHITE_BRUSH); //Get The Brush
SelectObject (memdc2, hbrush); //Choose The Brush
PatBlt (memdc2, 0,0, maxX,maxY,PATCOPY); //Fill With the Brush The Copy of the Window in RAM
ReleaseDC (hwnd2, hdc2); //Free The Context
Grid1();
break;
case WM_PAINT: //Display The OCntents Of the Window On to The Screen
hdc2 = BeginPaint(hwnd2,&paintstruct);
BitBlt (hdc2, 0, 0, maxX,maxY, memdc2,0,0,SRCCOPY);
EndPaint(hwnd2,&paintstruct);
break;
case WM_CHAR: //Handling Keyboard Input...
keybrd=LOWORD(wParam);
if(keybrd==108) //The L Button Pressed (Limiter On)
Phmit=1; //Limit Phase Plain to 2*Pi...
if(keybrd==109) //The M Button Pressed (Limiter Off)
Phmit=0; //Unlimit Phase Plain...
if(keybrd==115) //The S Button Pressed (Start)
{
SendMessage(hwnd,WM_COMMAND,ID_THREAD_START,0);
}
if(keybrd==114) //The R Button Pressed (Run)
{
SendMessage(hwnd,WM_COMMAND,ID_THREAD_RUN,0);
}
if(keybrd==116) //The T Button Pressed (Stop)
{
SendMessage(hwnd,WM_COMMAND,ID_THREAD_STOP,0);
}
if(keybrd==99) //The C Button Pressed (Clear)
{
SendMessage(hwnd,WM_COMMAND,ID_FILE_CLEAR,0);
}
if(keybrd==100) //The D Button Pressed (Input Values)
{
SendMessage(hwnd,WM_COMMAND,ID_DIALOG_INPUTVALUES,0);
}
break;
case WM_DESTROY: //Quit Actions (None)
PostQuitMessage(0);
break;
default:
return DefWindowProc (hwnd2, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK WindowFunc3(HWND hwnd3, //Third Window's Functioning (Poin-Care Plain)
UINT message,
WPARAM wParam,
LPARAM lParam)
{
HDC hdc3;
HBITMAP hbit;
static int maxX, maxY;
switch (message)
{
case WM_CREATE: //Window's Initialization Setup
maxX = GetSystemMetrics (SM_CXSCREEN); //Max X Size (Get The Display's Resolution)
maxY = GetSystemMetrics (SM_CYSCREEN); //Max Y Size (Get The Display's Resolution)
hdc3 = GetDC(hwnd3); //Recieve The Context Of The Device
memdc3 = CreateCompatibleDC(hdc3); //Compatibale Context In RAM
hbit = CreateCompatibleBitmap (hdc3,maxX,maxY);
//For The Window's Contents:
SelectObject (memdc3, hbit); //
hbrush = GetStockObject (WHITE_BRUSH); //Get The Brush
SelectObject (memdc3, hbrush); //Choose The Brush
PatBlt (memdc3, 0,0, maxX,maxY,PATCOPY); //Fill With the Brush The Copy of the Window in RAM
ReleaseDC (hwnd3, hdc3); //Free The Context
Grid2();
break;
case WM_PAINT: //Refreshing The Screen Fucntion...
hdc3 = BeginPaint(hwnd3,&paintstruct);
BitBlt (hdc3, 0, 0, maxX,maxY, memdc3,0,0,SRCCOPY);
EndPaint(hwnd3,&paintstruct);
break;
case WM_CHAR: //Handling Keyboard Input...
keybrd=LOWORD(wParam);
if(keybrd==108) //The L Button Pressed (Limiter On)
Phmit=1; //L]imit Phase Plain to 2*Pi...
if(keybrd==109) //The M Button Pressed (Limiter Off)
Phmit=0; //Unlimit Phase Plain...
if(keybrd==115) //The S Button Pressed (Start)
{
SendMessage(hwnd,WM_COMMAND,ID_THREAD_START,0);
}
if(keybrd==114) //The R Button Pressed (Run)
{
SendMessage(hwnd,WM_COMMAND,ID_THREAD_RUN,0);
}
if(keybrd==116) //The T Button Pressed (Stop)
{
SendMessage(hwnd,WM_COMMAND,ID_THREAD_STOP,0);
}
if(keybrd==99) //The C Button Pressed (Clear)
{
SendMessage(hwnd,WM_COMMAND,ID_FILE_CLEAR,0);
}
if(keybrd==100) //The D Button Pressed (Input Values)
{
SendMessage(hwnd,WM_COMMAND,ID_DIALOG_INPUTVALUES,0);
}
break;
case WM_DESTROY: //Closing Procedures...
PostQuitMessage(0);
break;
default:
return DefWindowProc (hwnd3, message, wParam, lParam);
}
return 0;
}
//Initial Values Definition (Dialog) Processing:
BOOL CALLBACK DialogProc(HWND hDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
char str[80]; //The Variable Used For I/O
switch (uMsg)
{
case (WM_INITDIALOG): //The Input Value Class/Type/Other Assignment
sprintf(str,"%2.3f",A0); //The Anker Modulation Force Variable
SetDlgItemText(hDlg,IDC_FORCE,str);
sprintf(str,"%1.4f",dt); //Time Step
SetDlgItemText(hDlg,IDC_DT,str);
sprintf(str,"%3.2f",Cx); //Axis Position X
SetDlgItemText(hDlg,IDC_CX,str);
sprintf(str,"%3.2f",Cy); //Axis Position Y
SetDlgItemText(hDlg,IDC_CY,str);
sprintf(str,"%3.2f",Kx); //Axis Zoom X
SetDlgItemText(hDlg,IDC_KX,str);
sprintf(str,"%3.2f",Ky); //Axis Zoom Y
SetDlgItemText(hDlg,IDC_KY,str);
return TRUE;
case WM_COMMAND: //End of Inputing..
switch (LOWORD (wParam))
{
case IDOK: //Save The Inputed Numbers
GetDlgItemText(hDlg,IDC_FORCE,str,80); //The Force (Amplitude) of The Ancer Modualtion
A0 = (float)atof(str);
GetDlgItemText(hDlg,IDC_DT,str,80); //The Step (Sets Detail Quality)
dt = (float)atof(str);
GetDlgItemText(hDlg,IDC_CX,str,80); //Axis Position X
Cx = (float)atof(str);
GetDlgItemText(hDlg,IDC_CY,str,80); //Axis Position Y
Cy = (float)atof(str);
GetDlgItemText(hDlg,IDC_KX,str,80); //Axis Zoom X
Kx = (float)atof(str);
GetDlgItemText(hDlg,IDC_KY,str,80); //Axis Zoom Y
Ky = (float)atof(str);
EndDialog(hDlg, TRUE); //Close
SendMessage(hwnd,WM_COMMAND,ID_FILE_TERMINATE,0);
return TRUE;
case IDCANCEL: //Forget the Inputed Values
EndDialog(hDlg, TRUE);
return TRUE;
}
}
return FALSE;
}
/*
--=======THE===PROGRAM===CYCLE=========--
--==============ITSELF=================--
(Desribed Fully)
*/
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
double velocityprev=0.; //Runtime Variable (Velocity)
SetBkColor(memdc2,RGB(255,255,255)); //Set the Color of The Background (Phase Plain)
SetTextColor(memdc2,RGB(0,0,0)); //Set the Color of The Text (Phase Plain)
SetBkColor(memdc3,RGB(255,255,255)); //Set the Color of The Background (Poin-Care Plain)
SetTextColor(memdc3,RGB(0,0,0)); //Set the Color of The Text (Poin-Care Plain)
//End of Initial Valued Definition / Setup
while(endth==1) //Main Cycle (The Program Thread)
{
WaitForSingleObject(hSem, INFINITE); //Wait For Semaphore Green
help1=alpha; //Save Previous Angle
Draw2(); //Refresh Window 2 (Phase Plain)
if(t>helpt+2*PI/Omega) //Check for Poin-Care Plain Dot Set Conditions = True
{ //If Yes:
Draw3(); //Refresh Window 3 (Poin-Care Plain)
helpt+=2*PI/Omega; //Update Next Condition Value
} //Or Maybe Not.. :)
t+=dt; //Update Time (Step Identified By User)
help1+=1./6.*(k11()+2*k21()+2*k31()+k41()); //Calculate The Coordinate (Angle) Derrivative Of the Pendulum
if(delta==0) alpha=help1; //If There is No Loss of Energy Alogorthm Fix
velocity+=1./6.*(k12()+2*k22()+2*k32()+k42()); //Calculate the Acceleration Of the Pendulum
alpha=help1; //Define the New Angle
ReleaseSemaphore(hSem,1,NULL); //Set Semaphore Green
}
return 0;
}
void Draw2(void)
{
RECT rect;
if(Phmit==0) //If Limiter Turned Off
{
SetPixel(memdc2,(int)(Cx+Kx*velocity),(int)(Cy-Ky*alpha),RGB(255,15,15));
rect.top = (int)(Cy-Ky*alpha);
rect.bottom = (int)(Cy-Ky*alpha)+1;
}
if(Phmit==1) //If Limiter Turned On
{
alphp=alpha; //The Angle Puan-Kare Varies from the Angle Checking Variable
if(alphp<-PI||alphp>PI) //If it doesn't lie between -Pi and Pi
alphp=changpi(alphp); //Set the Angle for Puan-Kare between -Pi and Pi
SetPixel(memdc2,(int)(Cx+Kx*velocity),(int)(Cy-Ky*alphp),RGB(15,155,15));
rect.top = (int)(Cy-Ky*alphp);
rect.bottom = (int)(Cy-Ky*alphp)+1;
}
rect.left = (int)(Cx+Kx*velocity);
rect.right = (int)(Cx+Kx*velocity)+1 ;
InvalidateRect(hwnd2,&rect,FALSE);
UpdateWindow (hwnd2);
}
void Draw3(void)
{
RECT rect;
SelectObject(memdc3,PenB); //Red Dot (Circle) Chosen
alphp=alpha; //The Angle Puan-Kare Varies from the Angle Checking Variable
if(alphp<-PI||alphp>PI) //If it doesn't lie between -Pi and Pi
alphp=changpi(alphp); //Set the Angle for Puan-Kare between -Pi and Pi
Ellipse(memdc3,(int)(Cx+Kx*velocity)-2,(int)(Cy-Ky*alphp)-2,(int)(Cx+Kx*velocity)+2,(int)(Cy-Ky*alphp)+2);
rect.top = (int)(Cy-Ky*alphp)-2;
rect.bottom = (int)(Cy-Ky*alphp)+2;
rect.left = (int)(Cx+Kx*velocity)-2;
rect.right = (int)(Cx+Kx*velocity)+2 ;
InvalidateRect(hwnd3,&rect,FALSE);
UpdateWindow (hwnd3);
}
void Grid1(void) //Axis Drawing Function (Phase Plain):
{
int i;
SelectObject(memdc2,PenBl); //X Axis
MoveToEx(memdc2,0,(int)Cy,NULL);
LineTo(memdc2,1500,(int)Cy);
MoveToEx(memdc2,(int)Cx,0,NULL); //Y Axis
LineTo(memdc2,(int)Cx,1000);
for(i=1;i<=20;i++) //X Axis Numbers:
{
MoveToEx(memdc2,(int)(Cx+Kx*(.5*i+0.)),(int)Cy-2,NULL);
LineTo(memdc2,(int)(Cx+Kx*(.5*i+0.)),(int)Cy+2);
MoveToEx(memdc2,(int)(Cx+Kx*(-.5*i+0.)),(int)Cy-2,NULL);
LineTo(memdc2,(int)(Cx+Kx*(-.5*i+0.)),(int)Cy+2);
}
for(i=1;i<=16;i++) //Y Axis Numbers:
{
MoveToEx(memdc2,(int)Cx-2,(int)(Cy-Ky*(i*PI/3.)),NULL);
LineTo(memdc2,(int)Cx+2,(int)(Cy-Ky*i*PI/3.));
MoveToEx(memdc2,(int)Cx-2,(int)(Cy-Ky*(-i)*PI/3.),NULL);
LineTo(memdc2,(int)Cx+2,(int)(Cy-Ky*(-i)*PI/3.));
}
}
void Grid2(void) //Axis Drawing Function (Poin-Care Plain):
{
int i;
SelectObject(memdc3,PenBl); //X Axis
MoveToEx(memdc3,0,(int)Cy,NULL);
LineTo(memdc3,1500,(int)Cy);
MoveToEx(memdc3,(int)Cx,0,NULL); //Y Axis
LineTo(memdc3,(int)Cx,1000);
for(i=1;i<=20;i++) //X Axis Numbers:
{
MoveToEx(memdc3,(int)(Cx+Kx*(.5*i+0.)),(int)Cy-2,NULL);
LineTo(memdc3,(int)(Cx+Kx*(.5*i+0.)),(int)Cy+2);
MoveToEx(memdc3,(int)(Cx+Kx*(-.5*i+0.)),(int)Cy-2,NULL);
LineTo(memdc3,(int)(Cx+Kx*(-.5*i+0.)),(int)Cy+2);
}
for(i=1;i<=16;i++) //Y Axis Numbers:
{
MoveToEx(memdc3,(int)Cx-2,(int)(Cy-Ky*(i*PI/3.)),NULL);
LineTo(memdc3,(int)Cx+2,(int)(Cy-Ky*i*PI/3.));
MoveToEx(memdc3,(int)Cx-2,(int)(Cy-Ky*(-i)*PI/3.),NULL);
LineTo(memdc3,(int)Cx+2,(int)(Cy-Ky*(-i)*PI/3.));
}
}
double changpi(double agck) //Make The Angle For Puan-Kare Between -Pi and Pi
{
while(agck<-PI||agck>PI) //If it's Way out (I doubt it...)
{
if(agck<-PI) //When it's less -Pi
agck+=2*PI; //Icrease it
if(agck>PI) //When it's more Pi
agck+=-2*PI; //Decrease it
}
return agck; //Return the Resulting Angle
}
/*
--=======THE===FUNCTIONS===USED=======--
--========ALGORYTHEM'S===MATH=========--
*/
double f1(double ah,double vh,double th) //Derrivagance Fucnction, Calcualtion (Velocity)
{
return vh;
}
double f2(double ah,double vh,double th) //Derrivagance Fucnction, Calcualtion (Angle Accelaration)
{
return -w0*w0*sin(ah)-(delta*vh)-2*A0*cos(Omega*th)*sin(ah);//My Equation
}
double k11(void) //The First Coeficient Calculation (Velocity... so on)
{
return f1(alpha,velocity,t)*dt;
}
double k21(void) //The Second Coeficient Calculation
{
return f1(alpha+k11()/2.,velocity,t+dt/2.)*dt;
}
double k31(void) //The Third Coeficient Calculation
{
return f1(alpha+k21()/2.,velocity,t+dt/2.)*dt;
}
double k41(void) //The Forth Coeficient Calculation
{
return f1(alpha+k31(),velocity,t+dt)*dt;
}
double k12(void) //The First Coeficient Calculation (Angle Acceleration... so on)
{
return f2(alpha,velocity,t)*dt;
}
double k22(void) //The Second Coeficient Calculation
{
return f2(alpha,velocity+k12()/2.,t+dt/2.)*dt;
}
double k32(void) //The Third Coeficient Calculation
{
return f2(alpha,velocity+k22()/2.,t+dt/2.)*dt;
}
double k42(void) //The Forth Coeficient Calculation
{
return f2(alpha,velocity+k32(),t+dt)*dt;
}
// Eugene Khijniak
// (c) 2003