1. Find the center, (xAvg, yAvg), of the bounding circle, copy and paste the above code that does precisely this.
2. Find the radius, rMax, of the bounding circle. This will be the maximum of all the distances from
(xAvg, yAvg) to (xPts[i], yPts[i]). You will have to make your own code to do this. You can do a for loop similar
in format to up above.
3. Call the Circle() function to draw the center and circle, in purple. Just copy and paste the two Circle commands up above
that do this.
The only error I am getting is fr undeclared identifiers, but I do not see why. My brain just may be too fried to see it at the moment. Any help would be deeply appreciated. Thanks, here is my code.
/*
circle.cpp
Draws a number of random points as very small circles
*/
#include <stdio.h>
#include <windows.h>
#include "resource.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
// 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 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;
// loop here
for (i = 0; i < numPoints; i++) {
rMax = 0;
int x = (xAvg - xPts[i]);
int y = (yAvg - yPts[i]);
temp = (x * x) + (y * y);
tempDist = sqrt(temp);
if (rMax < tempDist){
rMax = tempDist;
}
}
Circle(xAvg, yAvg, 1, purple); // to draw the center
Circle(xAvg, yAvg, rMax, purple); // to draw the bounding circle
// draw the points
PGraphics->Draw();
// 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);
}

New Topic/Question
Reply




MultiQuote






|