Teen Programmers Unite  
 

 

Return to forum top

Drawing a circle in C++

Posted by split [send private reply] at February 12, 2003, 05:17:34 PM

This question concerns drawing a circle in the dos window, not a GUI/form.

I have the function below which plots a point in the 80x25 dos window.

#include < windows.h >
#include < stdio.h >

void plot(int xC, int yC) {
COORD coordinate = {xC,yC};
HANDLE hOutHandle=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutHandle,coordinate);

printf("*");
}

The function above works fine as I have used it in many previous applications. I was wondering how I could use this function to draw a circle-like figure out to the console somehow. I understand that a circle is the collection of all points equidistant from the center but I cannot figure out how to get all the coordinates needed to form this. If anyone has any hints/suggestions or if anyone will explain to me why this would not be possible (if it isn't) please tell me.

Posted by taubz [send private reply] at February 12, 2003, 09:09:25 PM

Trigonometry or the pythagorean theorum will help.

Posted by split [send private reply] at February 12, 2003, 09:45:10 PM

Ah..I don't know any trigonometry yet and I have no idea how I would apply the pythagorean theorem.

Posted by split [send private reply] at February 12, 2003, 09:50:47 PM

I've been thinking about this for a while..all I've gotten is an inefficient way to produce a mediocre ellipsis-type figure.

Posted by taubz [send private reply] at February 12, 2003, 10:19:00 PM

"ellipse-type" (ellipsis is a word too, but unrelated)

Applying the pythagorean theorum here isn't very difficult. Draw the problem on paper until you stumble on a right triangle.

- taubz

Posted by split [send private reply] at February 12, 2003, 10:36:10 PM

Oh yeah heh ellipsis=(...). Alright I'll get out the graphing paper and try to figure it out.

Posted by CViper [send private reply] at February 13, 2003, 07:38:30 AM

You might stumble across another problem, namely that moving n coords along the x-axis dosen't move you the same distance (in pixels) as when you move n coords along the y-axis, as the character cells in the console aren't square. That could be the reason you get an elipsoid (assuming that's the right word :D).

Posted by DragonWolf [send private reply] at February 13, 2003, 08:27:22 AM

I can't think of any famous Circle drawing algorithms off the top of my head. But a quick search in google finds a fair few essays and references on "drawing a circle on a raster display" or "circle drawing algorithm on a raster display"

Posted by taubz [send private reply] at February 13, 2003, 08:49:56 AM

ellipsoid is a 3D surface. This time the correct word is "elliptical."

Posted by split [send private reply] at February 13, 2003, 05:16:07 PM

I think these Bresenham line-drawing algorithms will work. Even if they end up not being what I am looking for they are still very interesting..I win either way.

Posted by DragonWolf [send private reply] at February 14, 2003, 04:25:07 AM

there are plenty of interesting line drawing algorithms, but I can't think of any that can actually be used to easily draw a circle. Perhaps 2 Bezier curves or something.

Then again processing time might be less on a Bezier curve over using Cos/Tan/Sin for drawing a circle.

Posted by CodeRed [send private reply] at February 14, 2003, 05:10:43 PM

Yeah thats what I used in my 13h library, the bressenham algos, slightly modified for the .625 aspect ratio (to fix the circles looking like ovals problem)

Posted by pramod [send private reply] at February 16, 2003, 04:45:50 AM

Basically you now, there are 2 problem's the character cells aren't sqaure, the window itself is not square [at least not when its not full screen].

GetConsoleWindow, a function that is supposed to return the HWND of the console window, doesn't seem to be present on my system. Does anybody else have this problem? I mean, the header files don't have it, and when I added a declaration, the linker gave an error.

How can I get in pixels the size of the part of the window used to display text? Once I have this, I can go about normalizing(is that the word?), the ellipse into a circle.

Posted by CViper [send private reply] at February 16, 2003, 07:16:16 AM

you might want to look at GetConsoleScreenBufferInfo( HANDLE, PCONSOLE_SCREEN_BUFFER_INFO );

Didn't find GetConsoleWindow() in the SDK-doc's, so I'd guess it dosen't exist. You could try GetActiveWindow(), which gets the window attached to the current process.

Posted by split [send private reply] at February 16, 2003, 01:22:28 PM

MicrosoftReallyShouldHaveThoughtTwiceAboutHowTheyNamedTheirAPIFunctions(); --Oh well, I guess they make sense. I can't think of a better way..

Posted by pramod [send private reply] at February 17, 2003, 12:40:14 PM

Actually, my docs, the MSDN July 200 CD have the function, but it doens't appear in the code completion of VC++. But GetConsoleScreenBufferInfo seems to return all the sizes in character cells.


Actually, the user can set the size of the font [I think that is the size of the character cell] at the top left corner of the console window. How can the program get at this?

Posted by CViper [send private reply] at February 17, 2003, 04:28:50 PM

You have to install the mssdk aswell (with updated headers/libs/whatever). I don't really suggest that you do install the mssdk if you're stuff works currently, the only thing it did for me was creating a huge mess from everything.

SetConsoleWindowInfo() maybe. I don't know; I tend to avoid win32 API console stuff, but make sure to check the Console Functions section in the MSDN Platform SDK doc's ;)

and about the long names: that's when a few #def's get really handy.. :D

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.