programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# printing asterisks in a circle

Greenhorn
Posts: 2
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

Rancher
Posts: 783
19
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: 783
19
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.

pX = X + oX + dX

pY = Y + oY + dY

Les

Shannon Watts
Greenhorn
Posts: 2
Oh I don't want you to do the code for me but your explanation was what I needed. Thank you so much!

Saloon Keeper
Posts: 3707
47
Try Googling "circle bresenham". It's a pretty nifty algorithm used by most graphic software for drawing circles.

Marshal
Posts: 57437
175
• 1

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: 783
19
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

Ranch Hand
Posts: 69
6
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: 57437
175

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.

 The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?" The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS