Advertisement
Guest User

Untitled

a guest
May 6th, 2010
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3.  
  4. #include "resource.h"
  5.  
  6. // your path for this include may vary
  7. #include "GraphicsFramework.h"
  8.  
  9. // Global variable to store the graphics framwork object
  10. GraphicsFramework* PGraphics;
  11.  
  12. HWND HOutput = 0;  // handle to the output control
  13. HWND HDialog = 0;
  14. //Function prototype
  15. void DrawLine(int x1, int x2, int y1, int y2, unsigned int color)
  16. // function to get the absolute value of an integer
  17. int Abs(int x) {
  18.     if (x < 0)  return -x;
  19.     else        return x;
  20. }
  21.  
  22. // function to get the sign (+1 or -1) of an integer
  23. int Sign(int x) {
  24.     if (x < 0)  return -1;
  25.     else        return 1;
  26. }
  27.  
  28. // function to draw a line between the two points (x, y1) and (x2, y2)
  29. void DrawLine(int x1, int y1, int x2, int y2, unsigned int color);
  30. {
  31.  
  32.     PGraphics->AddPoint(x1+1, y1, color);
  33.     PGraphics->AddPoint(x1, y1+1, color);
  34.     PGraphics->AddPoint(x1+1, y1+1, color);
  35.  
  36.     PGraphics->AddPoint(x2+1, y2, color);
  37.     PGraphics->AddPoint(x2, y2+1, color);
  38.     PGraphics->AddPoint(x2+1, y2+1, color);
  39. }
  40.  
  41.  
  42. void DrawStuff() {
  43.     COLORREF green = RGB(0, 255, 0);     // green color to draw with
  44.     COLORREF purple = RGB(255, 0, 255);  // purple color to draw with
  45.  
  46.     char str[32];                       // string to store user input
  47.     int h, k;                           // parabola vertex
  48.     double a;                           // parabola constant - might be a decimal
  49.     int x, y;                           // loop and point variables
  50.     int xPrev, yPrev;                   // previous point for drawng line segments
  51.     int ymin, ymax;                     // limits for y loop
  52.     RECT rect;                          // rectangle for the output window
  53.  
  54.     // get the user input from the edit boxes and
  55.     // convert string input to integer
  56.     GetDlgItemText(HDialog, IDC_EDIT_VERTEXX, str, 32);
  57.     h = atoi(str);
  58.     GetDlgItemText(HDialog, IDC_EDIT_VERTEXY, str, 32);
  59.     k = atoi(str);
  60.     GetDlgItemText(HDialog, IDC_EDIT_CONSTA, str, 32);
  61.     a = atof(str);                              // use atof to allow user to enter a decimal
  62.  
  63.     // get the rect for this window
  64.     GetClientRect(HOutput, &rect);
  65.  
  66.     // use the rectangle info to set up y loop limits
  67.     ymin = -(rect.bottom - rect.top) / 2;
  68.     ymax =  (rect.bottom - rect.top) / 2;
  69.  
  70.     // clear the scene and add an axis
  71.     PGraphics->ClearScene(RGB(0, 0, 0));
  72.     PGraphics->AddAxis(RGB(150, 150, 150), 10);
  73.  
  74.     // loop in y, calculate x and draw
  75.     for (y = ymin; y <= ymax; y++) {
  76.         x = (int)( a * (y-k) * (y-k) ) + h;
  77.         PGraphics->AddPoint(x, y, green);
  78.     }
  79.  
  80.     // draw the points
  81.     PGraphics->Draw();
  82.     DrawLines(
  83. }
  84.  
  85. /*
  86. DialogProc
  87. this is the window event handler for the main dialog
  88. */
  89. BOOL CALLBACK DialogProc (HWND hwnd,
  90.     UINT message,
  91.     WPARAM wParam,
  92.     LPARAM lParam)
  93. {
  94.     switch(message)
  95.     {
  96.     case WM_INITDIALOG:
  97.         // dialog is initializing - store the picture box handle in a global variable for later
  98.         HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);        
  99.  
  100.         // instantiate and initialize our graphics framework object
  101.         PGraphics = new GraphicsFramework(HOutput);
  102.  
  103.         break;
  104.  
  105.     case WM_COMMAND:
  106.         switch(LOWORD(wParam))
  107.         {
  108.             case IDC_BTN_DRAW:
  109.                 // draw button was pressed
  110.                 DrawStuff();
  111.                 break;
  112.             case IDC_BTN_CLEAR:
  113.                 // clear button was pressed so clear the scene and draw the empty scene
  114.                 PGraphics->ClearScene(RGB(0, 0, 0));
  115.                 PGraphics->Draw();
  116.                 break;
  117.             case IDCANCEL:
  118.                 // user is quitting so release the GraphicsFramework object and quit
  119.                 delete PGraphics;
  120.                 PostQuitMessage(0);
  121.                 break;
  122.         }
  123.                  
  124.     }
  125.     return FALSE;
  126. }
  127.  
  128. // this is the main function that starts the application
  129. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
  130. {
  131.     // create the main window
  132.     // store its handle in a global if needed
  133.     HDialog = CreateDialog (GetModuleHandle(NULL),
  134.         MAKEINTRESOURCE(IDD_DIALOG1),
  135.         0,
  136.         DialogProc);
  137.  
  138.     // make the dialog visible
  139.     ShowWindow(HDialog, SW_SHOW);
  140.  
  141.     // standard windows message loop
  142.     MSG  msg;
  143.     int status;
  144.     while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
  145.     {
  146.         if (status == -1)
  147.             return -1;
  148.         // avoid processing messages for the dialog
  149.         if (!IsDialogMessage (HDialog, & msg))
  150.         {
  151.             TranslateMessage ( & msg );
  152.             DispatchMessage ( & msg );
  153.         }
  154.     }
  155.  
  156.     return (int)(msg.wParam);
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement