Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing a semicircle using the formula for the equation of a circle  RSS feed

 
Brian Stumbaugh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Proj01output.jpg
[Thumbnail for Proj01output.jpg]
 
Bruce Baker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im not sure what polar coordinates mean. This was my first project in my Intro to Java course.....so I can't imagine it would get too crazy.
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Funny enough that all the help Ive found online uses this......but this is the beginning of my class.....and we haven't touched on libraries or whatever yet
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formula to use for a circle is not x² + y² = r²
It is ∀θ • x = rcosθ ∧ y = rsinθ
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 (rcosθ, rsinθ) 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:
  • 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.
  •  
    Piet Souris
    Rancher
    Posts: 1943
    66
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!