programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering 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:

# The art and science of java Chapter 9 programming exercise to draw a heart

Dennis Ouyang
Greenhorn
Posts: 11
I have the correct code for the problem 7 in chapter 9 of "The art and science of java", which I found from a solutions manual online. Images of the page describing the problem are provided in attachment. however, I cannot tell how this programmer
found the correct values to pass to the GArc and GLine methods in order to draw the heart picture. I know it has something to do with trigonometry and fact that Math.sqrt(2) is the factor you multiply the side of a right triangle by to find the hypotenuse,
but I cannot see why the solution has those values for dx, height, and so forth. I realize this may be more of a math question than concerning java, but any help would be useful.

Javadoc on the ACM libraries I used if you need it: http://cs.stanford.edu/people/eroberts/jtf/javadoc/student/index.html.
http://cs.stanford.edu/people/eroberts/jtf/

p361-Question-7-part-1.png
p-361-question-7-part-2.png

Campbell Ritchie
Marshal
Posts: 56599
172
Obviously the ACM drawing classes are an extension of Swing classes; I do not have them.
Does anything go wrong when you draw the heart? Did you work out the coordinates on paper before trying to program them? Here are some possible coordinates for the square:-In which case the centres of the circles will be 30, 30 and 70, 30.

Dennis Ouyang
Greenhorn
Posts: 11
here is my latest attempt. I tried setting the midpoint of the circle each arc is drawn from to exactly halfway between the points you display above. So the midpoint of the left arc would be (30, 35) for the left arc, and to define the upper left corner of the rectangle bounding the arc, I just subtract the radius, from the x and y coordinates, as seen below:

The arcs are still not connecting with the rays below. I have tried this with multiple sizes. I think the secret is to define just one number for either length To Ray (distance from center of the diamond to diamond vertex, or the radius of each circle that defines the left and right arcs, then just make all other coordinates functions of that, as seen in the official answer I originally posted. But I cannot tell how the official answer made the calculations. Nothing I remember on geometry tells me what, if any, relation there is between the radius of the circles making the left and right arcs and the rays that connect with them.

 It is sorta covered in the JavaRanch Style Guide.