1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/*
cosine_soln.cpp
Draws a cosine function using line segments
*/
#include <math.h>
#include <stdio.h>
#include <windows.h>
#include "resource.h"
#pragma intrinsic (sin, cos, tan)
// your path for this include may vary
#include "GraphicsFramework.h"
int x;
double y;
double rad;
// Global variable to store the graphics framwork object
GraphicsFramework* PGraphics;
HWND HOutput = 0; // handle to the output control
HWND HDialog = 0;
void DrawStuff() {
COLORREF green = RGB(0, 255, 0); // green color to draw with
COLORREF purple = RGB(255, 0, 255); // purple color to draw with
char str[32]; // string to store user input
double amplitude;
double period;
double phaseAngle;
const double PI = 3.1415927; // constant Pi
RECT rect; // rectangle for the output window
int xMin, xMax, wdRect; // min & max x rectangle coords
double rad = PI/180;
// clear the scene and add an axis
PGraphics->ClearScene(RGB(0, 0, 0));
PGraphics->AddAxis(RGB(150, 150, 150), 10);
// get the rectangle info for this window
GetClientRect(HOutput, &rect);
wdRect = rect.right - rect.left;
xMin = -wdRect / 2;
xMax = wdRect / 2;
// get the user input from the edit boxes and
// convert string input to double
GetDlgItemText(HDialog, IDC_EDIT_AMPLITUDE, str, 32);
amplitude = atof(str);
GetDlgItemText(HDialog, IDC_EDIT_PERIOD, str, 32);
period = atof(str);
GetDlgItemText(HDialog, IDC_EDIT_PHASEANGLE, str, 32);
phaseAngle = atof(str);
// Add the for loop here, that creates the points (x,y)
//one pixel at a time and then adds the points via the
// command PGraphics->AddPoint(x,y,green)
for (int x = xMin; x <= xMax; x++) {
y = cos((double)2*PI/180);
PGraphics->AddPoint(x,y,green);
}
PGraphics->Draw();
}
/*
DialogProc
this is the window event handler for the main dialog
*/
BOOL CALLBACK DialogProc (HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
// dialog is initializing - store the picture box handle in a global variable for later
HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);
// instantiate and initialize our graphics framework object
PGraphics = new GraphicsFramework(HOutput);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BTN_DRAW:
// draw button was pressed
DrawStuff();
break;
case IDC_BTN_CLEAR:
// clear button was pressed so clear the scene and draw the empty scene
PGraphics->ClearScene(RGB(0, 0, 0));
PGraphics->Draw();
break;
case IDCANCEL:
// user is quitting so release the GraphicsFramework object and quit
delete PGraphics;
PostQuitMessage(0);
break;
}
}
return FALSE;
}
// this is the main function that starts the application
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
// create the main window
// store its handle in a global if needed
HDialog = CreateDialog (GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_DIALOG1),
0,
DialogProc);
// make the dialog visible
ShowWindow(HDialog, SW_SHOW);
// standard windows message loop
MSG msg;
int status;
while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
{
if (status == -1)
return -1;
// avoid processing messages for the dialog
if (!IsDialogMessage (HDialog, & msg))
{
TranslateMessage ( & msg );
DispatchMessage ( & msg );
}
}
return (int)(msg.wParam);
}
|