Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

printing asterisks in a circle  RSS feed

 
Shannon Watts
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need code to print a circle in asterisks using the equation of a circle. I've looked everywhere. My problem is I'm not really sure how to get it to use a center of (0,30) and a radius of 20. My professor said I need two displacement variables but that's when I got confused. I'm really new at this so only pleasant and helpful replies please
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the equation for a circle is: sin(a) + cos(a) = 1 (polar coordinates) and X*X + Y*Y = 1 (rectangular coordinates)
You need to have that 20 units radius, so you multiply though by 20. 20 * (sin(a) + cos(a)) = 20 and 20*X*X + 20*Y*Y = 20

You have to have a center of (0, 30) so you start at X=0, and Y=30. That does not make sense to me since you will not be able to show a circle--only half a circle.

Well, I like to use the polar coordinates method because I don't want to solve for X all the time (Y = sqrt(1-X*X)) where -1 <= X <= 1 and notice Y can never be -1, but has to be to have a circle.

In Java you need to have an X and a Y value to map anything onto a graphic screen, so you need to have offsets for X and Y, I usually call them dX (delta, change, X) and dY (delta, change, Y).
Using the polar coordinates method you get dX = 20 * cos(a) and dY = 20 * sin(a).

Your angle a will run from 0 to 360 degrees or 0 to 2 * PI, depending on the functions you use rectangular or polar sin and cos functions.

So you start out with
X = 0 and dX = cos(a) so you have the equation pX, position of pX, = X + dX
Y = 30 and dY = sin(a) so you have the equation pY, postion of pY, = Y + dY

So you put your asterisk at (pX, pY)

So what do you need help with now, because we are not going to code it for you, but we will answer specific questions to help.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shannon,

Here is the part I think you said you didn't understand:

If you want to translate your origin--the start--to the middle of the screen, then you need an X offset and a Y offset--suppose you have a display area of 200x200, you can then translate your (0, 30) Origin to (100, 100) using two displacement variables oX = width/2 - X, and oY = (height/2) - Y or
oX = width/2 and oY = (height/2)-30.

So your formula becomes:

pX = X + oX + dX

pY = Y + oY + dY

Les
 
Shannon Watts
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I don't want you to do the code for me but your explanation was what I needed. Thank you so much!
 
Carey Brown
Bartender
Posts: 2998
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try Googling "circle bresenham". It's a pretty nifty algorithm used by most graphic software for drawing circles.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:So the equation for a circle is: sin(a) + cos(a) = 1 (polar coordinates) . . .
Surely that is rsin²θ + rcos²θ = r. And that looks like rectangular coordinates; in polar coordinates that would simply be r = n

Yes, you might set the circle's centre at (50, 50) in which case the x position is 50 + rsinθ and y is 50 -rcosθ but you do not need to know that. What you need to know is that you are a distance of r from a set point. There are two methods in the Math class intended for use to convert rectangular coordinates to polar: 1 2. You can go through your locations and work out which location would be closest to the circle and put your asterisks there. But beware: a method returning a double is going to be imprecise, so the chances of the == operator working are negligible. You would have to find which is nearest. I agree that finding out about Bresenham's algorithm, as CB suggested, might be helpful.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeppers, you are quite right, but X component is cos(a) and Y is sin(a) for getting (X, Y). Therein, amongst other places, my mind went off track a bit.

For Polar you have a radius at an angle, so you have to have both, but sin and cos are used to express the X and Y components of the Vector, radius at angle, used in the Polar coordinate system.

Campbell Ritchie wrote:
Les Morgan wrote:So the equation for a circle is: sin(a) + cos(a) = 1 (polar coordinates) . . .
Surely that is rsin²θ + rcos²θ = r. And that looks like rectangular coordinates; in polar coordinates that would simply be r = n
 
Gary W. Lucas
Ranch Hand
Posts: 67
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because your Java application is printing out to the console or writing to a text file, you might find it easier to use the equation

(x-x0)^2+(y-y0)^2 = r^2

and solving for x,

x1 = x0 + sqrt(r^2-(y-y0)^2)
x2 = x0 - sqrt(r^2-(y-y0)^2)

to draw the circle with x corresponding to column number and y corresponding to row number, with (x0, y0) being the origin.

Since the application is printing, you start from the top and work your way down, counting row by row. Your row becomes the y coordinate, solve for x getting either (a) one solution, in which case you print one asterisk in the correct column, (b) two solutions, in which case you draw two, or (c) no solution in which case your row is outside the radius of the circle. You could loop on something like for row = -radius to +radius, which should avoid the no-solution case.

There are a couple of tricky things to watch out for. The shape of characters in most western fonts is not square, and if you assume that it is, your circle will be rather taller than wide... e.g. an ellipse. I'd focus on getting a "circle" to print first and then worry about the proportions later on. Generally, the ratio of height to width is close to 3/2. The other thing to pay attention to is that if you copy your output into a document, make sure you pick a non-proportional font such as Courier to show it so that the spaces and asterisks are exactly the same width.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:. . . X component is cos(a) and Y is sin(a) for getting (X, Y). Therein, amongst other places, my mind went off track a bit. . . .
You are right there; cos for x and sin for y. It was after all during the Swinging Sixties that I was taught about that definition of sin/cos.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!