Development Java - Making Random 5 Pointed Stars

Discussion in 'Software' started by hacker 8991, 25 Feb 2006.

  1. hacker 8991

    hacker 8991 What's a Dremel?

    Joined:
    8 Jan 2004
    Posts:
    643
    Likes Received:
    1
    This isn't a request for help, but instead an explanation of what I had to deal with in trying to draw (and fill) five pointed stars in Java.

    At the beginning of the week, we were assigned a project in computer science which asked us to make 10 randomly placed and sized stars (five pointed). By the second day, I had figured out how to graph a pentagram using only three variables: the center (x, y), and the radius. This loop gives you the five points that, if connected, would make a regular pentagon, which is the basis for a pentagram.
    Code:
    for (int c = 0; c < 5; c++)
    {
       xValues[c] = radius * Math.cos (c * 0.4 * PI) + xCenter;
       yValues[c] = radius * Math.sin (c * 0.4 * PI) + yCenter;
    }
    Some things to note: (1) 5 is derived from the number of sides. This loop could give the points for any regular polygon, but that's not what I was shooting for. (2) 0.4 is derived from the angle between the points (the angle of the arc). It's 360 / 5, or 72 degrees, which is 2 / 5 * PI radians.

    But I wanted to draw a pentagram, not a pentagon. To do this, you'd think that you coudl skip every other point, just like you would when drawing one with your pencil, but that's not the case. If you do that, the interior of the pentagram is unfilled.

    To find the points where the lines intersect, some geometry is involved. I won't delve into it, but the interior polygon's radius has a direct ratio to that of the outside polygon's. The smaller radius (sRadius) is as follows.
    Code:
    sRadius = radius * Math.cos (0.4 * PI) / Math.cos (0.2 * PI);
    Now, to draw the pentagram, you take the first point from the large pentagon, then one from the small one, and repeat until you end up where you started. There's only one caveat: the smaller pentagon has to be rotate 36 degrees (PI / 5 radians).

    [​IMG]
    Sample output of the finished script.

    All of this took me a couple days to figure out, and you can see the finished product (alernate link). You can check out the source code in that directory.

    - tf
     
    Last edited: 25 Feb 2006
  2. hyperactive98

    hyperactive98 What's a Dremel?

    Joined:
    30 Mar 2002
    Posts:
    99
    Likes Received:
    0
    That link is borken but i'm interested in seeing your code/final result.
     
  3. hacker 8991

    hacker 8991 What's a Dremel?

    Joined:
    8 Jan 2004
    Posts:
    643
    Likes Received:
    1

Share This Page