Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. /*
  2. circle.cpp
  3.  
  4. Draws a number of random points as very small circles
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <windows.h>
  9.  
  10. #include "resource.h"
  11. #include "math.h"
  12.  
  13. // your path for this include may vary
  14. #include "GraphicsFramework.h"
  15.  
  16. // Global variable to store the graphics framwork object
  17. GraphicsFramework* PGraphics;
  18.  
  19. HWND HOutput = 0; // handle to the output control
  20. HWND HDialog = 0;
  21.  
  22. // function to get the absolute value of an integer
  23. int Abs(int x) {
  24. if (x < 0) return -x;
  25. else return x;
  26. }
  27.  
  28. // function to get the sign (+1 or -1) of an integer
  29. int Sign(int x) {
  30. if (x < 0) return -1;
  31. else return 1;
  32. }
  33.  
  34.  
  35. void Circle(int xCenter, int yCenter, int r, unsigned int color) {
  36. double d;
  37. int rSquared;
  38. int x, y;
  39.  
  40. rSquared = r * r;
  41.  
  42. x = 0;
  43. y = r;
  44.  
  45. d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));
  46.  
  47. while (y >= x) {
  48. PGraphics->AddPoint(xCenter + x, yCenter + y, color); //draws circular arc for approx. 45-90 degrees
  49. PGraphics->AddPoint(xCenter - x, yCenter + y, color); //draws arc for approx. 90-135 degrees
  50. PGraphics->AddPoint(xCenter + x, yCenter - y, color); //draws arc for approx. 270-315 degrees
  51. PGraphics->AddPoint(xCenter - x, yCenter - y, color); //draws arc for approx. 225-270 degrees
  52. PGraphics->AddPoint(xCenter + y, yCenter + x, color); //draws arc for approx. 0-45 degrees
  53. PGraphics->AddPoint(xCenter - y, yCenter + x, color); //draws arc for approx. 135-180 degrees
  54. PGraphics->AddPoint(xCenter + y, yCenter - x, color); //draws arc for approx. 315-360 degrees
  55. PGraphics->AddPoint(xCenter - y, yCenter - x, color); //draws arc for approx. 180-225 degrees
  56. if (d > 0) { //if (x,y)lies inside the circle of radius r centered at (-1,0.5)
  57. }
  58. else {
  59. y--;
  60. }
  61. x++;
  62. d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));
  63. }
  64. }
  65.  
  66.  
  67. void DrawStuff() {
  68. COLORREF green = RGB(0, 255, 0); // green color to draw with
  69. COLORREF purple = RGB(255, 0, 255); // purple color to draw with
  70.  
  71. char str[32]; // string to store user input
  72. int numPoints; // user input
  73. int *xPts, *yPts; // ptrs for dynamic array of x,y coordinates
  74. int i; // loop and point variables
  75. RECT rect; // rectangle for the output window
  76. int xMin, wdRect, yMin, htRect; // min rectangle coords and rect width & height
  77. int xSum, ySum; // sum of x and y points
  78. int xAvg, yAvg; // center point of bounding circle
  79. int rMax;
  80.  
  81. // clear the scene and add an axis
  82. PGraphics->ClearScene(RGB(0, 0, 0));
  83. PGraphics->AddAxis(RGB(150, 150, 150), 10);
  84.  
  85. // get the rectangle info for this window
  86. GetClientRect(HOutput, &rect);
  87. wdRect = rect.right - rect.left;
  88. xMin = -wdRect / 2;
  89. htRect = rect.bottom - rect.top;
  90. yMin = -htRect / 2;
  91.  
  92. // get the user input from the edit boxes and
  93. // convert string input to integer
  94. GetDlgItemText(HDialog, IDC_EDIT_NUMPOINTS, str, 32);
  95. numPoints = atoi(str);
  96.  
  97. // allocate and initialize the arrays with random point values
  98. xPts = new int[numPoints];
  99. yPts = new int[numPoints];
  100. for (i = 0; i < numPoints; i++) {
  101. // keep points within range -wd/4..+wd/4, -ht/4..+ht/4 so
  102. // bounding circle won't get too large
  103. xPts[i] = wdRect/4 - rand() % (wdRect/2);
  104. yPts[i] = htRect/4 - rand() % (htRect/2);
  105. Circle(xPts[i], yPts[i], 1, green);
  106. }
  107.  
  108. // find the avg of all of the points
  109. // this will be the bounding circle center
  110. xSum = 0;
  111. ySum = 0;
  112. for (i = 0; i < numPoints; i++)
  113. {
  114. xSum += xPts[i];
  115. ySum += yPts[i];
  116. }
  117. xAvg = xSum / numPoints;
  118. yAvg = ySum / numPoints;
  119.  
  120. // finds the radius, rMax, of the bounding circle
  121. int TrMax;
  122. int distance;
  123. distance = 0;
  124. rMax = (int)distance;
  125. for (i = 0; i < numPoints; i++)
  126. {
  127. TrMax = sqrt(pow(xPts[i] - xAvg, 2.0) + pow(yPts[i] - yAvg, 2.0));
  128. while (TrMax > rMax)
  129. {
  130. TrMax = rMax;
  131. --TrMax;
  132. }
  133. }
  134.  
  135. // to draw the center
  136. Circle(xAvg, yAvg, 1, purple);
  137. // to draw the bounding circle
  138. Circle(xAvg, yAvg, rMax, purple);
  139.  
  140. // draw the points
  141. PGraphics->Draw();
  142.  
  143. // free up the allocated memory for the points
  144. delete[] xPts;
  145. delete[] yPts;
  146. }
  147.  
  148. /*
  149. DialogProc
  150. this is the window event handler for the main dialog
  151. */
  152. BOOL CALLBACK DialogProc (HWND hwnd,
  153. UINT message,
  154. WPARAM wParam,
  155. LPARAM lParam)
  156. {
  157. switch(message)
  158. {
  159. case WM_INITDIALOG:
  160. // dialog is initializing - store the picture box handle in a global variable for later
  161. HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);
  162.  
  163. // instantiate and initialize our graphics framework object
  164. PGraphics = new GraphicsFramework(HOutput);
  165.  
  166. break;
  167.  
  168. case WM_COMMAND:
  169. switch(LOWORD(wParam))
  170. {
  171. case IDC_BTN_DRAW:
  172. // draw button was pressed
  173. DrawStuff();
  174. break;
  175. case IDC_BTN_CLEAR:
  176. // clear button was pressed so clear the scene and draw the empty scene
  177. PGraphics->ClearScene(RGB(0, 0, 0));
  178. PGraphics->Draw();
  179. break;
  180. case IDCANCEL:
  181. // user is quitting so release the GraphicsFramework object and quit
  182. delete PGraphics;
  183. PostQuitMessage(0);
  184. break;
  185. }
  186.  
  187. }
  188. return FALSE;
  189. }
  190.  
  191. // this is the main function that starts the application
  192. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
  193. {
  194. // create the main window
  195. // store its handle in a global if needed
  196. HDialog = CreateDialog (GetModuleHandle(NULL),
  197. MAKEINTRESOURCE(IDD_DIALOG1),
  198. 0,
  199. DialogProc);
  200.  
  201. // make the dialog visible
  202. ShowWindow(HDialog, SW_SHOW);
  203.  
  204. // standard windows message loop
  205. MSG msg;
  206. int status;
  207. while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
  208. {
  209. if (status == -1)
  210. return -1;
  211. // avoid processing messages for the dialog
  212. if (!IsDialogMessage (HDialog, & msg))
  213. {
  214. TranslateMessage ( & msg );
  215. DispatchMessage ( & msg );
  216. }
  217. }
  218.  
  219. return (int)(msg.wParam);
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement