Unix Programming - best way to draw a circle using curses

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2006 > best way to draw a circle using curses





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author best way to draw a circle using curses
DaVinci

2006-03-13, 7:51 am

hi,all...
here are my codes on how to a draw a circle using curses,but I found
it's not a good way and there are some bugs.
#include<vector>
#include<curses.h>
using std::vector;
struct Point
{
Point(int i_,int j_)
{
i = i_;
j = j_;
}
int i;
int j;
};



vector<Point> draw_circle(const Point& center,int radius)
{
vector<Point> vp;
for(int i = 0; i<LINES;i++)
for(int j = 0;j<COLS;j++)
{
if((i - center.i)*(i - center.i) + (j - center.j)*(j - center.j) ==
radius*radius )
{
vp.push_back(Point(i,j));
move(i,j);
addstr("a");
refresh();
usleep(100000);
}
}
return vp;
}


int main()
{
Point center (22,22);
int radius = 2; //int radius = 1,.....take care of here..@@
initscr();
clear();
draw_circle(center,radius);
sleep(33);
endwin();
return 0;
}

Q1:
at main() function,
I found it is so strange that if I declare int radius = 2,or int radius
= 1,or some
others ,the picture I draw is only have four point,but if the radius is
bigger,there may be more,but the quantity is not respected.

Q2:are there a better way to draw a circle??
Thanks in advance.

Fred Kleinschmidt

2006-03-13, 5:54 pm


"DaVinci" <apple.davinci@gmail.com> wrote in message
news:1142257365.474676.286410@j33g2000cwa.googlegroups.com...
> hi,all...
> here are my codes on how to a draw a circle using curses,but I found
> it's not a good way and there are some bugs.
> #include<vector>
> #include<curses.h>
> using std::vector;
> struct Point
> {
> Point(int i_,int j_)
> {
> i = i_;
> j = j_;
> }
> int i;
> int j;
> };
>
>
>
> vector<Point> draw_circle(const Point& center,int radius)
> {
> vector<Point> vp;
> for(int i = 0; i<LINES;i++)
> for(int j = 0;j<COLS;j++)
> {
> if((i - center.i)*(i - center.i) + (j - center.j)*(j - center.j) ==
> radius*radius )
> {
> vp.push_back(Point(i,j));
> move(i,j);
> addstr("a");
> refresh();
> usleep(100000);
> }
> }
> return vp;
> }
>
>
> int main()
> {
> Point center (22,22);
> int radius = 2; //int radius = 1,.....take care of here..@@
> initscr();
> clear();
> draw_circle(center,radius);
> sleep(33);
> endwin();
> return 0;
> }
>
> Q1:
> at main() function,
> I found it is so strange that if I declare int radius = 2,or int radius
> = 1,or some
> others ,the picture I draw is only have four point,but if the radius is
> bigger,there may be more,but the quantity is not respected.
>
> Q2:are there a better way to draw a circle??
> Thanks in advance.
>


Think about it for a minute.
How many combinations of INTEGERS i, j, and r are there,
for any given r, such that i**2 + j**2 = r**2?

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


Fletcher Glenn

2006-03-13, 5:54 pm

DaVinci wrote:
> hi,all...
> here are my codes on how to a draw a circle using curses,but I found
> it's not a good way and there are some bugs.
> #include<vector>
> #include<curses.h>
> using std::vector;
> struct Point
> {
> Point(int i_,int j_)
> {
> i = i_;
> j = j_;
> }
> int i;
> int j;
> };
>
>
>
> vector<Point> draw_circle(const Point& center,int radius)
> {
> vector<Point> vp;
> for(int i = 0; i<LINES;i++)
> for(int j = 0;j<COLS;j++)
> {
> if((i - center.i)*(i - center.i) + (j - center.j)*(j - center.j) ==
> radius*radius )
> {
> vp.push_back(Point(i,j));
> move(i,j);
> addstr("a");
> refresh();
> usleep(100000);
> }
> }
> return vp;
> }
>
>
> int main()
> {
> Point center (22,22);
> int radius = 2; //int radius = 1,.....take care of here..@@
> initscr();
> clear();
> draw_circle(center,radius);
> sleep(33);
> endwin();
> return 0;
> }
>
> Q1:
> at main() function,
> I found it is so strange that if I declare int radius = 2,or int radius
> = 1,or some
> others ,the picture I draw is only have four point,but if the radius is
> bigger,there may be more,but the quantity is not respected.
>
> Q2:are there a better way to draw a circle??
> Thanks in advance.
>


Try a net search on "Breshenham Circle Algorithm".

--

Fletcher Glenn

DaVinci

2006-03-14, 7:49 am


Fred Kleinschmidt wrote:
> "DaVinci" <apple.davinci@gmail.com> wrote in message
> news:1142257365.474676.286410@j33g2000cwa.googlegroups.com...
>
> Think about it for a minute.
> How many combinations of INTEGERS i, j, and r are there,
> for any given r, such that i**2 + j**2 = r**2?

If I change INTEGES to double,the result may be better.
But move(int,int) required arguments of int types.

There is no move(double,double).

so the circle must be ugly. ??
>
> --
> Fred L. Kleinschmidt
> Boeing Associate Technical Fellow
> Technical Architect, Software Reuse Project


RickE

2006-03-14, 7:49 am

A little set of Bresenham drawing routines can be found here;

http://www.pinenet.com/eng/Draw.html

It was an example of drawing to the linux framebuffer console using
FreePascal. I'm not sure you can draw circles in [n]curses text mode.

For the truly ambitious, the old Bellcore MGR window manager has
bitmapping routines as well as text. I can't compile it, but I think
there is a lot of salvagable material.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com