/* Filename: Math3.cpp Programmers: Brian LaSitis, Br. David Carlson, Br. Isidore Minerd Date: March 5, 2003; August 15, 2009 This Win32 program illustrates simple graphics by graphing a mathematical function, in this case a rational function. 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 Studio 2005 Microsoft Visual Studio 2008 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); 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) { LPCWSTR szAppName = L"WinApp"; LPCWSTR szWndName = L"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, L"Could not create window.", L"Error", 0); return 0; } hWnd = CreateWindow(szAppName, szWndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, 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, L"Graph", 5); // Draw the axes: MoveToEx(hDC, 100, 300, NULL); LineTo(hDC, 500, 300); MoveToEx(hDC, 300, 50, NULL); LineTo(hDC, 300, 550); // Draw the graph of the function g: for (x = -200; x <= 200; x++) SetPixel(hDC, 300 + x, 300 + g(x), RGB(0, 0, 255)); // blue 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 rational function. Return: In the function name we return this integer approximation to the value of g at x. */ int g(int x) { return (50.0 * x - 1000.0) / (x + 20); }