/* Filename: Math2.cpp Programmers: Brian LaSitis, Br. David Carlson Date: March 5, 2003 This Win32 program illustrates simple graphics by graphing two mathematical function in the same window. Both are modified sine functions, and the one function's amplitude is modulated by the other. Although much of the Windows code in the program is not understandable to beginning students, the WM_PAINT section in the WndProc function is fairly easy to follow. Students who have a basic understanding of calling class functions can read this section of code and modify it to draw something of their choosing. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET To compile this program in Visual C++ 6.0, create a new project of type "Win32 Application" (not Win32 Console application) and then make sure that "empty project" is selected. Do not select Console application. You can then create a new C++ source file for this project and paste in this code. To compile this program in Visual C++ .NET, create a Visual C++ project of type "Win32 project" (not Console application) and under Application settings be sure that Windows application and Empty project are checked. You can then create a new C++ file for this project and paste in this code. */ #include #include // Global constant for pi: const double pi = 3.1415926535; // Function prototypes: int g(int x); int f(int x); LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); // For a Win32 app, there is a WinMain function, not a main function. Ignore the gory Windows details. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, INT iCmdShow) { TCHAR szAppName[] = "WinApp"; TCHAR szWndName[] = "Simple Drawing"; HWND hWnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; if (! RegisterClass(&wndclass)) { MessageBox(NULL, "Could not create window.", "Error", 0); return 0; } hWnd = CreateWindow(szAppName, szWndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 580, 400, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam; } // This function processes the various messages for the window. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int x; switch (message) { case WM_CREATE: return 0; case WM_PAINT: HDC hDC; PAINTSTRUCT ps; HPEN hOldPen, hNewPen; hDC = BeginPaint(hWnd, &ps); hNewPen = CreatePen(PS_SOLID, 1, RGB(0, 180, 180)); // aqua color (mixes green and blue) hOldPen = (HPEN)SelectObject(hDC, hNewPen); //*** Students: try changing the section between this comment line and the one marked below. *** // Print a message: TextOut(hDC, 50, 50, "Graph", 5); // Draw the axes: MoveToEx(hDC, 100, 200, NULL); LineTo(hDC, 500, 200); MoveToEx(hDC, 300, 100, NULL); LineTo(hDC, 300, 300); // Draw the graph of the function g: for (x = -200; x <= 200; x++) SetPixel(hDC, 300 + x, 200 + g(x), RGB(0, 0, 255)); // blue color // Draw the graph of the function f, the one that modulates the amplitude of g: for (x = -200; x <= 200; x++) SetPixel(hDC, 300 + x, 200 + f(x), RGB(255, 0, 0)); // red color //*** Students: this is the end of the section that you might try to change. *** EndPaint(hWnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, message, wParam, lParam); } /* Given: x An integer value. Task: To compute the nearest integer to the value of g at x where g is a modified sine function. Return: In the function name we return this integer approximation to the value of g at x. */ int g(int x) { return 100 * sin(pi * x / 200.0) * sin(pi * x / 25.0); } /* Given: x An integer value. Task: To compute the nearest integer to the value of f at x where f is a modified sine function. Return: In the function name we return this integer approximation to the value of f at x. */ int f(int x) { return 100 * sin(pi * x / 200.0); }