Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- circle.cpp
- Draws a number of random points as very small circles
- */
- #include <stdio.h>
- #include <windows.h>
- #include "resource.h"
- #include "math.h"
- // your path for this include may vary
- #include "GraphicsFramework.h"
- // Global variable to store the graphics framwork object
- GraphicsFramework* PGraphics;
- HWND HOutput = 0; // handle to the output control
- HWND HDialog = 0;
- // function to get the absolute value of an integer
- int Abs(int x) {
- if (x < 0) return -x;
- else return x;
- }
- // function to get the sign (+1 or -1) of an integer
- int Sign(int x) {
- if (x < 0) return -1;
- else return 1;
- }
- void Circle(int xCenter, int yCenter, int r, unsigned int color) {
- double d;
- int rSquared;
- int x, y;
- rSquared = r * r;
- x = 0;
- y = r;
- d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));
- while (y >= x) {
- PGraphics->AddPoint(xCenter + x, yCenter + y, color); //draws circular arc for approx. 45-90 degrees
- PGraphics->AddPoint(xCenter - x, yCenter + y, color); //draws arc for approx. 90-135 degrees
- PGraphics->AddPoint(xCenter + x, yCenter - y, color); //draws arc for approx. 270-315 degrees
- PGraphics->AddPoint(xCenter - x, yCenter - y, color); //draws arc for approx. 225-270 degrees
- PGraphics->AddPoint(xCenter + y, yCenter + x, color); //draws arc for approx. 0-45 degrees
- PGraphics->AddPoint(xCenter - y, yCenter + x, color); //draws arc for approx. 135-180 degrees
- PGraphics->AddPoint(xCenter + y, yCenter - x, color); //draws arc for approx. 315-360 degrees
- PGraphics->AddPoint(xCenter - y, yCenter - x, color); //draws arc for approx. 180-225 degrees
- if (d > 0) { //if (x,y)lies inside the circle of radius r centered at (-1,0.5)
- }
- else {
- y--;
- }
- x++;
- d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));
- }
- }
- 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
- int numPoints; // user input
- int *xPts, *yPts; // ptrs for dynamic array of x,y coordinates
- int i; // loop and point variables
- RECT rect; // rectangle for the output window
- int xMin, wdRect, yMin, htRect; // min rectangle coords and rect width & height
- int xSum, ySum; // sum of x and y points
- int xAvg, yAvg; // center point of bounding circle
- int rMax;
- // 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;
- htRect = rect.bottom - rect.top;
- yMin = -htRect / 2;
- // get the user input from the edit boxes and
- // convert string input to integer
- GetDlgItemText(HDialog, IDC_EDIT_NUMPOINTS, str, 32);
- numPoints = atoi(str);
- // allocate and initialize the arrays with random point values
- xPts = new int[numPoints];
- yPts = new int[numPoints];
- for (i = 0; i < numPoints; i++) {
- // keep points within range -wd/4..+wd/4, -ht/4..+ht/4 so
- // bounding circle won't get too large
- xPts[i] = wdRect/4 - rand() % (wdRect/2);
- yPts[i] = htRect/4 - rand() % (htRect/2);
- Circle(xPts[i], yPts[i], 1, green);
- }
- // find the avg of all of the points
- // this will be the bounding circle center
- xSum = 0;
- ySum = 0;
- for (i = 0; i < numPoints; i++)
- {
- xSum += xPts[i];
- ySum += yPts[i];
- }
- xAvg = xSum / numPoints;
- yAvg = ySum / numPoints;
- // finds the radius, rMax, of the bounding circle
- int TrMax;
- int distance;
- distance = 0;
- rMax = (int)distance;
- for (i = 0; i < numPoints; i++)
- {
- TrMax = sqrt(pow(xPts[i] - xAvg, 2.0) + pow(yPts[i] - yAvg, 2.0));
- while (TrMax > rMax)
- {
- TrMax = rMax;
- --TrMax;
- }
- }
- // to draw the center
- Circle(xAvg, yAvg, 1, purple);
- // to draw the bounding circle
- Circle(xAvg, yAvg, rMax, purple);
- // draw the points
- PGraphics->Draw();
- // free up the allocated memory for the points
- delete[] xPts;
- delete[] yPts;
- }
- /*
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement