Math not quite right in the loop
I am trying to draw circles in the form of a target in one of these online tutorials from a school. The instructions were to draw an archery target, or a chain store logo. Basicaly a 3 ring target. The outer ring was to be one inch, and each following ring to be 0.35 inch smaller. The math appears to work out fine for the given values. I find that increasing the NUM_RINGS will make the start ring larger than intended. Here is the code as was being tested.
Code :
/** The number of rings to draw */
private static final int NUM_RINGS = 3;
/** The number of pixels per inch */
private static final int PIXELS_PER_INCH = 72;
/** The amount to scale down each ring */
private static final double SCALE_FACTOR = 0.35;
/** The size of the outer ring in inches */
private static final int OUTER_RING_SIZE = 1;
for (int i = NUM_RINGS; i > 0; i--) {
double size = OUTER_RING_SIZE * PIXELS_PER_INCH * SCALE_FACTOR * i;
After thinking about it I come up with the following:
Code :
for (int i = 0; i < NUM_RINGS; i++) {
double size = (OUTER_RING_SIZE * PIXELS_PER_INCH) - (PIXELS_PER_INCH * (SCALE_FACTOR * i));
This seems to work with the only problem I have found being that if you request more rings than you have room for, the smallest rings are just too small to be drawn. Looking for any suggestions to improve what I mean to do vs what I have done, as I am still not quite sure the math is what it should be.
Re: Math not quite right in the loop
Well, you stated that each inner ring is supposed to be 0.35 inch smaller than the previous one.
So, if this is the requirement then here's the plan:
Outer ring size = 1 inch.
The next ring size= 1 - 0.35 = 0.65 inch.
The next ring is the inner circle (the bulls-eye). Its size = 0.65-0.35 = 0.30 inch.
(Isn't that what your second codelet gives?)
Now, obviously you can't make any ring that is 0.35 inch smaller than the third ring, so (as we used to say in the Neighborhood) that's all she wrote.
So: What is it that you "mean" to do?
If you want to have more rings and keep the outer ring size equal to one inch, then, obviously, the spacing between the rings must be less.
Bottom line: If you are looking for suggestions to improve what you "mean to do" maybe you should articulate your intent.
Anyhow...
Once a decision has been made as to ring spacing, a simple loop is easy to implement.
Something along the lines of your code might go like this:
Code :
Define variables outermostRingSize, deltaRingSiize and numberOfRings,
and initialize them with appropriate values.
Define a variable named ringSize and initialize it to outermostRingSize
Make a simple loop for the number of rings
for (int i = 0; i < numRings; i++)
{
// Do whatever you want to do with ringSize
// Subtract deltaRingSize from ringSize for the next time through the loop
}
Note: I would, probably define the size of the inner ring (the bulls-eye) first and work out from there rather than going from outer to inner, but that's just me. I'm funny that way. (My reasoning is that the bulls-eye is the most important thing about a target, and I like to take care of the important stuff first. But that's kind of irrelevant.)
By the way:
I note that the Target Stores Logo has the inner circle size equal to 1/3 the outer ring size, so the values for a logo with 1 inch outer ring size would be slightly different from your 0.35 inch ring spacing design:
Outer ring size = 1 inch.
Middle ring size = 0.67 inch.
Circle in the middle size = 0.33 inch.
When figuring out how to draw target bulls-eyes with arbitrary numbers of rings, I would start with the inner circle (the bulls-eye). Then I would add as many equally-spaced rings as I want. The first ring from the center may or may not be spaced equal to the size of the center, but all of the other rings would be equally spaced. Then I would scale everything to whatever physical dimension that is required for the outer-most ring.
See how it goes?
Design the output first:
Start in the middle and work your way out. Adjust the overall size as the final step of your design.
Then write the program using parameters from your design.
Cheers!
Z
Re: Math not quite right in the loop
Quote:
Originally Posted by
Zaphod_b
(Isn't that what your second codelet gives?)
yes
Quote:
Originally Posted by
Zaphod_b
So: What is it that you "mean" to do?
I guess I just wanted a second opinion that the code was correct the second time around. I don't have a live person to talk to about it. Thanks for your input.
Quote:
Originally Posted by
Zaphod_b
Outer ring size = 1 inch.
Middle ring size = 0.67 inch.
Circle in the middle size = 0.33 inch.
Yes I am aware 0.67 and 0.33 are closer to the same size, I was using the constants defined in the instructions.
Quote:
Originally Posted by
Zaphod_b
Start in the middle and work your way out.
Actually the assignment is drawing full circles, and placing the outermost first gives the impression of rings from the z ordering.
Thanks very much for the lengthy reply you took the time to offer, you fully answered my questions.