We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Trig for a star..
Page Index Toggle Pages: 1
Trig for a star.. (Read 1344 times)
Trig for a star..
Mar 29th, 2009, 6:31pm
 
Trying to create a star with processing.. any ideas why it sometimes doesn't complete the circumference

http://www.openprocessing.org/visuals/?visualID=1324

I don't see the problem
Re: Trig for a star..
Reply #1 - Mar 29th, 2009, 7:38pm
 
Alright, took me a while but  I needed to know. Smiley

That problem was that "a" was rounded after multiplying it so everytime it was a decimal number it was rounded so when you then mutiplied it by "n" you didnt fill up a total 360 degrees and got that gap. You didnt get it everytime cause, when it wasnt a decimal it worked fine.

I changed it by making your i a float. Maybe there are better ways to do it. but worked fine...

just change that part


private void calcPoly(){
    Point p;
    float i=0, n = points.length;
    float a = 360/n;
    float cosa, sina;
   
    for( i=0;i<n;i++){
 
      points[int(i)] = new Point(0,0);
       p = points[int(i)];
       cosa = cos((a*i) * PI/180);
       sina = sin((a*i) * PI/180);
       if(i%2==0){
          p.x = r1 * cosa;
          p.y = r1 * sina;
       }else{
          p.x = r2 * cosa;
          p.y = r2 * sina;
       }

       
    }
 }
Re: Trig for a star..
Reply #2 - Mar 30th, 2009, 9:18am
 
Thanks for the digging!! You were right about changing the int to a float, but it wasn't the i that was affecting the outcome, it was the n;

Because a comes from 360/n. I didn't realize that an integer would take precedence in a calculation in Java.

Code:

    int i=0;
    float n = points.length;
    float a = 360/n;
    float cosa, sina;
     
    for(i=0;i<int(n);i++){
      points[i] = new Point(0,0);
       p = points[i];
       cosa = cos((a*i) * PI/180);
       sina = sin((a*i) * PI/180);
       if(i%2==0){
          p.x = r1 * cosa;
          p.y = r1 * sina;
       }else{
          p.x = r2 * cosa;
          p.y = r2 * sina;
       }         
    }



Cheers!
Re: Trig for a star..
Reply #3 - Mar 30th, 2009, 9:52am
 
true.... my fault... was late last night Smiley anyway, case solved! Smiley
Page Index Toggle Pages: 1