0

I am trying to generate the x and y values of a circle with radius one, for x=rcos(a) and y=rsin(a), but when I use the three different methods below I only get the correct values only by adding them manually, but I would like to add them in a loop.

 ArrayList<Double> cCoords = new ArrayList<Double>();
 double pi = Math.PI;
 double pi_over_two = Math.PI/2.0;
 double pi_over_four = Math.PI/4.0;
 double pi_over_180 = Math.PI / 180.0;
 double piVals = 0;


 cCoords.add(Math.cos(0));
 cCoords.add(Math.sin(0));
 cCoords.add(Long.valueOf(Math.round(Math.cos(pi_over_two))).doubleValue());
 cCoords.add(Math.sin(pi_over_two));
 cCoords.add(Math.sin(pi_over_two+pi_over_two+pi_over_two));

////////Below values are printed correctly

    cCoords.add(Math.cos(pi_over_four) );
    cCoords.add(Math.sin(pi_over_four));

    cCoords.add(Math.cos(pi_over_four+pi_over_four));
    cCoords.add((Math.sin( pi_over_four+pi_over_four)) );

    cCoords.add( Math.cos(pi_over_four+pi_over_four+pi_over_four) );
    cCoords.add(Math.sin(pi_over_four+pi_over_four+pi_over_four));

    cCoords.add(Math.cos(pi_over_four +pi_over_four+pi_over_four+pi_over_four) );
    cCoords.add(Math.sin(pi_over_four +pi_over_four+pi_over_four+pi_over_four));

    cCoords.add(Math.cos(pi_over_four+pi_over_four + pi_over_four+pi_over_four+pi_over_four));
    cCoords.add(Math.sin( pi_over_four+pi_over_four +pi_over_four+pi_over_four+pi_over_four) );

    cCoords.add( Math.cos(pi_over_four+pi_over_four+pi_over_four +pi_over_four+pi_over_four+pi_over_four) );
    cCoords.add(Math.sin(pi_over_four+pi_over_four+pi_over_four + pi_over_four+pi_over_four+pi_over_four));


//////////////Above values are printed correctly


    for(int i = 0; i < 6; i++) { // Wrong values

        cCoords.add(i, Math.cos(pi_over_four) );
        cCoords.add(i+1, Math.sin(pi_over_four));

        pi_over_four+=pi_over_four;

    }
/////


    for(int i = 0; i < 6; i++) { // x=1 coordinate gets repeated with different y values

        cCoords.add( Math.cos(pi_over_four) );
        cCoords.add( Math.sin(pi_over_four));

        pi_over_four+=pi_over_four;

    }

////

    for(int i = 0; i < cCoords.size()-1; i+=2) {
        System.out.print("(" + cCoords.get(i) + ",");
        System.out.println(cCoords.get(i+1) + ")\n");
    }
0

2 Answers 2

2

You are getting the wrong values because you are inserting the incorrect values in the loop after changing the value of variable pi_over_four itself.

For instance, in the below code snippet which you say is printing the value correctly, you have doubled the value for the next iteration i.e. 2 * pi_over_four from just pi_over_four:

cCoords.add(Math.cos(pi_over_four));
cCoords.add(Math.sin(pi_over_four));

cCoords.add(Math.cos(pi_over_four + pi_over_four));
cCoords.add(Math.sin(pi_over_four + pi_over_four)); 

In the next iteration it becomes 3 * pi_over_four:

cCoords.add(Math.cos(pi_over_four + pi_over_four + pi_over_four));
cCoords.add(Math.sin(pi_over_four + pi_over_four + pi_over_four)); 

Note that here you are not changing the value of variable pi_over_four. You are just doing n * pi_over_four and then storing it back into the list.

But if you'll see your loop carefully, you are changing the value of pi_over_four itself which is wrong. What you should be doing instead is something like this:

for(int i = 0; i < 6; i++) {
    cCoords.add(i, Math.cos((i + 1) * pi_over_four));
    cCoords.add(i+1, Math.sin((i + 1) * pi_over_four));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the help, found out the loop should be similar to your suggestion : int j=0; for(int i = 0; i < 6; i+=2) { cCoords.add(i, Math.cos( (j ) * pi_over_four) ); cCoords.add(i+1, Math.sin( (j ) * pi_over_four) ); j+=0.5 )
@user1420482 Your Welcome! :)
1

pi_over_four is changing each time you loop, so in reality, instead of multiplying it by 2 each time you are multiplying it by 2^x where x is i+1. To imagine this, on the first loop, pi_over_four is equal to pi/4, on the second it's equal to pi/2, but then you add pi/2 to that instead of pi/4, making the 3rd loop have it be equal to pi. Store your new pi_over_four in a different variable to fix this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.