Brian Stumbaugh

Greenhorn

Posts: 5

posted 2 years ago

So Ive got to make a turtle on an image draw a semicircle that starts at the top of a circle and goes down and to the left, changing colors halfway through. I've got everything down, I can draw the turtles and make them do straight lines. My problem is more math related. I need to use the equation of a circle, give it points, and figure out how to write that code in java. We use the equation for a circle: (x - a)^2 + (y - b)^2 = r^2

amanda.setName("amanda");

amanda.setShellColor(Color.BLUE);

amanda.setBodyColor(Color.RED);

amanda.setPenColor(Color.YELLOW);

amanda.setPenWidth(3);

amanda.forward();

amanda.turn(-90);

amanda.setPenColor(Color.BLUE);

amanda.forward(???);

the question marks are where I need to draw a line on an arc, like in the picture. This is for a homework assignment, but I've already missed the deadline and now Im just trying to figure it out so I can continue on in the class and not drop out. Any help would be appreciated.

amanda.setName("amanda");

amanda.setShellColor(Color.BLUE);

amanda.setBodyColor(Color.RED);

amanda.setPenColor(Color.YELLOW);

amanda.setPenWidth(3);

amanda.forward();

amanda.turn(-90);

amanda.setPenColor(Color.BLUE);

amanda.forward(???);

the question marks are where I need to draw a line on an arc, like in the picture. This is for a homework assignment, but I've already missed the deadline and now Im just trying to figure it out so I can continue on in the class and not drop out. Any help would be appreciated.

Proj01output.jpg

Bruce Baker

Greenhorn

Posts: 2

posted 2 years ago

May I assume that you are permitted to use polar coordinate's? I can't see any way that you can direct Amanda in only the x direction and then only the y direction and end up with an arc (of any length). Let me know and I'll dig out my calculus book that spells out very clearly the translation between Cartesian and Polar coordinates. Bruce Baker

Brian Stumbaugh

Greenhorn

Posts: 5

Campbell Ritchie

Sheriff

Posts: 53752

127

posted 2 years ago

Why are you not using things like Graphics#drawArc?

More complicated suggestion:

Cast the Graphics object to Graphics2D.

Duplicate it with its create() method.

Translate its graphics so the origin is in the centre of where you want the circle.

Scale its graphics by 1.0 in the

More complicated suggestion:

Cast the Graphics object to Graphics2D.

Duplicate it with its create() method.

Translate its graphics so the origin is in the centre of where you want the circle.

Scale its graphics by 1.0 in the

*x*direction and -1.0 in the*y*direction. Then you can have all the coordinates pointing the same direction you did when you were at school and had to draw Cartesian coordinates on paper.
Brian Stumbaugh

Greenhorn

Posts: 5

Campbell Ritchie

Sheriff

Posts: 53752

127

posted 2 years ago

The formula to use for a circle is not

It is ∀θ •

You can use any value of θ not equal to −∞ or +∞, but in practice you only need values between 0 and 2π for a complete circle. So you would have to choose lots of values of θ between 0 and 0.5π and colour the points at (r

Beware:1: You have to supply axes. These should be the same if you want a circular arc. 2: The angles are in degrees, whereas those I showed earlier are in radians. 3: Check the directions. I think 0° = 3o'clock and 90° = 12o'clock without scaling, but check the documentation carefully.

*x*² +*y*² =*r*²It is ∀θ •

*x*= r*cos*θ ∧*y*= r*sin*θYou can use any value of θ not equal to −∞ or +∞, but in practice you only need values between 0 and 2π for a complete circle. So you would have to choose lots of values of θ between 0 and 0.5π and colour the points at (r

*cos*θ, r*sin*θ) to get a quadrantic arc, which is what you appear to want from the picture you posted. I can't see why you should go to all that effort when you can say g.drawArc(....);Beware:

Piet Souris

Rancher

Posts: 1783

55

posted 2 years ago

the direction of a rotation is always from the positive x-axis to the positive y-axis, for positive angles.

And for a standard JPanel the x-axis goes from left to right, the y axis from top to bottom.

So that would leave 90 degrees at the bottom.

But I think that Brian should give some more information. The example he gives in the opening post

reminds me of LOGO, with a " turtle" called Amanda, so to speak.

So, Brian, can you tell how you implemented the " amanda" commands? The idea being to get

some information how you actually draw things.

Greetz,

Piet

And for a standard JPanel the x-axis goes from left to right, the y axis from top to bottom.

So that would leave 90 degrees at the bottom.

But I think that Brian should give some more information. The example he gives in the opening post

reminds me of LOGO, with a " turtle" called Amanda, so to speak.

So, Brian, can you tell how you implemented the " amanda" commands? The idea being to get

some information how you actually draw things.

Greetz,

Piet

It is sorta covered in the JavaRanch Style Guide. |