I am working on a project to draw a circle around points and must connect to the furthest point from the center. I have everything to work but I can't seem to get the circle to go to the furthest point each time. Here is the code for the project.
/*
circle.cpp
Draws a number of random points as very small circles
*/
#include <math.h>
#include <stdio.h>
#include <windows.h>
#include "resource.h"
// your path for this include may vary
#include "GraphicsFramework.h"
int xSum, ySum, xAvg, yAvg = 0;
double rMax = 0;
// 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;
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);
}
xSum = 0;
ySum = 0;
for (i = 0; i < numPoints; i++)
{
xSum += xPts[i];
ySum += yPts[i];
}
xAvg = xSum / numPoints;
yAvg = ySum / numPoints;
for (i = 0; i < numPoints; i++)
{
double distance = 0;
Circle(xAvg, yAvg, 1, purple); // to draw the center
Circle(xAvg, yAvg, rMax, purple); // to draw the bounding circle
// 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);
}
Here is the part that I am having trouble with.
for (i = 0; i < numPoints; i++)
{
double distance = 0;
When posting code, use [code][/code] tags.
Post only the relevant part.
As I understand, you just get the average of all coordinates and then find the distance to the furthest one?
Well, that's not a good algorithm for this..
Anyway, your problem here is that if (distance > rMax) rMax = sqrt (distance); if you wrote units, rMax would be meters and distance would be m2. Of course the square will be almost always larger than a root, so you are very likely to just be assigning the distance to the last point here.
I didn't see where the code tags was. Sorry about that. Yea I am trying to get to the furthest point but can't figure out how to code it in the program.