4 Replies - 10821 Views - Last Post: 27 May 2010 - 06:19 PM Rate Topic: -----

#1 nelsonbc25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-February 09

Drawing a random circle

Posted 16 May 2009 - 05:01 PM

I'm trying to modify a program so that it will take a random points points, calculate the center of those points, and draw a circle. The problem that I'm encountering is calculating the radius for the random points. When I enter a number of points it draws the points and the center, but the circle is not correct. Can someone help me see where my calculation for the radius?

/*
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.
	
	int xSum = 0;
	int ySum = 0;
	for (i = 0; i < numPoints; i++)
	{
		xSum += xPts[i];
		ySum += yPts[i];
	}
	int xAvg = xSum / numPoints;
	int yAvg = ySum / numPoints;

	int xMax=0;
	int yMax=0;
	int rMax=0;
	int xTemp=0;
	int yTemp=0;
	//Calculates rMax
	for (i = 0; i < numPoints; i++)
	{
		xTemp = xPts[i]-xAvg;
		yTemp = yPts[i]-yAvg;
		if (xTemp > xMax)
		{
			if (yTemp > yMax)
			{
			xMax=xTemp;
			yMax=yTemp;
			}//endif
		}//endif
		if (yTemp > yMax)
		{
			if (xTemp>xMax)
			{
				yMax=yTemp;
				xMax=xTemp;
			}//endif
		}//endif
		rMax = (((xMax^2)+(yMax^2)));
	}//endfor

	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);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Drawing a random circle

#2 pdkharkar   User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Drawing a random circle

Posted 16 May 2009 - 05:52 PM

I think you should try for graphics.
Give your coordinates to the program
let the program calculate the center just by taking the average of coordinates.
pass the values to the function circle(int,int,int);
use rand(); function to take random values.
Try to code the program.
Tell me if you are unable to do.
Was This Post Helpful? 0
  • +
  • -

#3 nelsonbc25   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-February 09

Re: Drawing a random circle

Posted 16 May 2009 - 06:37 PM

View Postpdkharkar, on 16 May, 2009 - 04:52 PM, said:

I think you should try for graphics.
Give your coordinates to the program
let the program calculate the center just by taking the average of coordinates.
pass the values to the function circle(int,int,int);
use rand(); function to take random values.
Try to code the program.
Tell me if you are unable to do.


What I'm having a problem with is calculating the 3rd integer, rMax. I've already coded the center calculation, I just need help with the radius calculation.
Was This Post Helpful? 0
  • +
  • -

#4 pdkharkar   User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Drawing a random circle

Posted 16 May 2009 - 07:15 PM

No no
You didn't get what I told (sorry it was my mistake, I didn't clear it out)

Use the graphics mode in C++
there is a function called "circle"
Its description is........
circle(int,int,int)
here
first int is the x coordinate of the center
second int is the y coordinate and
third int is the radius of the circle.

..............................try it..
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Guest*


Reputation:

Re: Drawing a random circle

Posted 27 May 2010 - 06:19 PM

Answering this question requires knowing what exactly you want for the results. If you want all the points to lie on or within the circle, then the radius should calculate the distance between the center and each point, and whichever distance is the longest should be your radius. If you want all points to lie on or outside of the circle, use the shortest distance. If you want all three points to lie on the circle, use any of the distances (they will all be the same). The trick for this last one, though, is that you need to correctly calculate the center point for the circle.

Your code looks like it is trying to find the largest distance, however you need to calculate dx * dx + dy * dy (dx and dy are delta values, so this is the distance between two points squared) and check if that is greater than your rMax. You don't need to bother with the square root while comparing (since if a squared > b squared, then a > b too), until you know what the maximum is. Then, after the loop is done, you can get the square root of the maximum to get your maximum radius.
Was This Post Helpful? 0

Page 1 of 1