Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 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. rMax = 0
  124. for (i = 0; i < numPoints; i++)
  125. {
  126. TrMax = sqrt(pow(xPts[i] - xAvg, 2.0) + pow(yPts[i] - yAvg, 2.0));
  127. while (TrMax > rMax)
  128. {
  129. rMax = TrMax;
  130. }
  131. }
  132.  
  133. // to draw the center
  134. Circle(xAvg, yAvg, 1, purple);
  135. // to draw the bounding circle
  136. Circle(xAvg, yAvg, rMax, purple);
  137.  
  138. // draw the points
  139. PGraphics->Draw();
  140.  
  141. // free up the allocated memory for the points
  142. delete[] xPts;
  143. delete[] yPts;
  144. }
  145.  
  146. /*
  147. DialogProc
  148. this is the window event handler for the main dialog
  149. */
  150. BOOL CALLBACK DialogProc (HWND hwnd,
  151. UINT message,
  152. WPARAM wParam,
  153. LPARAM lParam)
  154. {
  155. switch(message)
  156. {
  157. case WM_INITDIALOG:
  158. // dialog is initializing - store the picture box handle in a global variable for later
  159. HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);
  160.  
  161. // instantiate and initialize our graphics framework object
  162. PGraphics = new GraphicsFramework(HOutput);
  163.  
  164. break;
  165.  
  166. case WM_COMMAND:
  167. switch(LOWORD(wParam))
  168. {
  169. case IDC_BTN_DRAW:
  170. // draw button was pressed
  171. DrawStuff();
  172. break;
  173. case IDC_BTN_CLEAR:
  174. // clear button was pressed so clear the scene and draw the empty scene
  175. PGraphics->ClearScene(RGB(0, 0, 0));
  176. PGraphics->Draw();
  177. break;
  178. case IDCANCEL:
  179. // user is quitting so release the GraphicsFramework object and quit
  180. delete PGraphics;
  181. PostQuitMessage(0);
  182. break;
  183. }
  184.  
  185. }
  186. return FALSE;
  187. }
  188.  
  189. // this is the main function that starts the application
  190. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
  191. {
  192. // create the main window
  193. // store its handle in a global if needed
  194. HDialog = CreateDialog (GetModuleHandle(NULL),
  195. MAKEINTRESOURCE(IDD_DIALOG1),
  196. 0,
  197. DialogProc);
  198.  
  199. // make the dialog visible
  200. ShowWindow(HDialog, SW_SHOW);
  201.  
  202. // standard windows message loop
  203. MSG msg;
  204. int status;
  205. while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
  206. {
  207. if (status == -1)
  208. return -1;
  209. // avoid processing messages for the dialog
  210. if (!IsDialogMessage (HDialog, & msg))
  211. {
  212. TranslateMessage ( & msg );
  213. DispatchMessage ( & msg );
  214. }
  215. }
  216.  
  217. return (int)(msg.wParam);
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement