posted 1 year ago
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
posted 1 year ago
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 circleonly 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(1X*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.
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 circleonly 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(1X*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.
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
posted 1 year ago
Shannon,
Here is the part I think you said you didn't understand:
If you want to translate your originthe startto the middle of the screen, then you need an X offset and a Y offsetsuppose 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
Here is the part I think you said you didn't understand:
If you want to translate your originthe startto the middle of the screen, then you need an X offset and a Y offsetsuppose 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
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
Shannon Watts
Greenhorn
Posts: 2
posted 1 year ago
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.
 1
Surely that is rsin²θ + rcos²θ = r. And that looks like rectangular coordinates; in polar coordinates that would simply be r = nLes Morgan wrote:So the equation for a circle is: sin(a) + cos(a) = 1 (polar coordinates) . . .
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.
posted 1 year ago
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.
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:
Surely that is rsin²θ + rcos²θ = r. And that looks like rectangular coordinates; in polar coordinates that would simply be r = nLes Morgan wrote:So the equation for a circle is: sin(a) + cos(a) = 1 (polar coordinates) . . .
Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH
posted 1 year ago
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
(xx0)^2+(yy0)^2 = r^2
and solving for x,
x1 = x0 + sqrt(r^2(yy0)^2)
x2 = x0  sqrt(r^2(yy0)^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 nosolution 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 nonproportional font such as Courier to show it so that the spaces and asterisks are exactly the same width.
(xx0)^2+(yy0)^2 = r^2
and solving for x,
x1 = x0 + sqrt(r^2(yy0)^2)
x2 = x0  sqrt(r^2(yy0)^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 nosolution 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 nonproportional font such as Courier to show it so that the spaces and asterisks are exactly the same width.
Campbell Ritchie
Marshal
Posts: 57437
175
posted 1 year ago
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.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. . . .
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
