Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Math not quite right in the loop

  1. #1
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default 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.

    	/** 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:
      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.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default 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:

    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
    Last edited by Zaphod_b; July 15th, 2012 at 08:13 PM.

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    jps (July 16th, 2012)

  4. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Math not quite right in the loop

    Quote Originally Posted by Zaphod_b View Post
    (Isn't that what your second codelet gives?)
    yes
    Quote Originally Posted by Zaphod_b View Post
    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 View Post
    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 View Post
    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.

Similar Threads

  1. Math.E - what exactly is it?
    By RedCloudTsunami in forum Java Theory & Questions
    Replies: 2
    Last Post: July 10th, 2012, 12:24 AM
  2. [SOLVED] Help with Math.tan and Math.atan
    By Dr.Code in forum Algorithms & Recursion
    Replies: 6
    Last Post: July 2nd, 2012, 05:54 AM
  3. Math.pow
    By britton33 in forum What's Wrong With My Code?
    Replies: 30
    Last Post: July 1st, 2012, 04:42 PM
  4. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM
  5. Question on my math
    By SwEeTAcTioN in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 25th, 2009, 05:42 PM