Best Answer:
Use some trig and draw some lines. Start by deciding the following: if you want to draw the star at (x,y) is that the top corner, or is that the center? We can go with top corner for now.

If you assign your points A B C D and E, starting at the top and going clockwise, then you know the following things:

drawing a star at (x,y) with (s,s) for width and height (assume it's the same width as height)

A.x the same as star.x+s/2

A.y the same as star.y

E.x the same as star.x

B.x the same as star.x+s

E.y the same as B.y

D.y the same as star.y+s

E.y the same as star.y+s (and the same as D.y)

Now, let the pentagon on the inside, starting with the point between A and B (the intersection of lines AC and BE) be labeled L M N O P

Each angle in this will be 108°, due to its being a regular pentagon. This makes angle APL 72°, angle ALP 72°, and PAL 36°. This can be said for any of the triangles - the 'bottom' or inside angles will be 72° and the 'top' or pointy angle will be 36°.

With this, we can draw a point between D and C (I'll label it as Q) that is the midpoint of the two. It will have the same x value as A, and the same Y value as D and C, and as star.y+s. The distance from A to Q will be s.

Because triangle AQD is similar to the triangle created by the midpoint of PL, we know the angle ADQ must be the same as APL, or 72°. We know AQD is a right angle, leaving 18° for DAQ. This is consistent with it being half of 36° because we should be bisecting that angle to form it.

Solving for the length of side DQ gives tan(18)*s, or .325*s - this is consistent with the drawing. It giving it half of s would cause it to extend to directly under e, which we know it shouldn't. This makes D's x value star.x + .175*s (because it's .325 away from the middle, toward the right edge.) Consequently, this makes C's x value .175 from the left edge, or star.x+.825*s

to recap, we know:

A = (star.x + s/2, star.y)

B = (star.x + s, ???)

C = (star.x + .825*s, star.y+s)

D = (star.x + .175*s, star.y+s)

E = (star.x, ???)

Now, the only thing we don't know is B.y and E.y, which will actually be the same thing. Just one more to solve.

Because we know the distance between each point should be the same, we can solve for the distance from D to C quite simply. The midpoint of them was .325*s, so the distance will be .650*s.This means the distance between E and D will also be .650*s. If we make a point called R in the bottom left corner of the 'box' around the star (not that we'd draw one, but it exists conceptually) we get a right triangle ERD. We know DE (hypotenuse) to be .650, we know DR to be .175 (the x value of D, because R's x value is 0). Solving for this unknown distance ER gives .626*s - thus, the y value of E will be (1-.626)*s, or .374*s. We also know B will have the same y distance.

So to recap:

A = (star.x + s/2, star.y)

B = (star.x + s, .374*s)

C = (star.x + .825*s, star.y+s)

D = (star.x + .175*s, star.y+s)

E = (star.x, .374*s)

Now, and interesting property of this is that if you make it wider or thinner, it will not effect the height at ALL. Same with making it taller or shorter, it won't effect the width at all. So we can replace the X value s's with w's for width, and the Y value s's with h for height.

so your function will be

public void drawStar(Graphics g, Color c, int x, int y, int w, int h) {

int ax = (w/2) + x;

int ay = y;

int bx = w+x;

int by = (int)(.374*(double)(h)+(double)(y));

int cx = (int)(.825*(double)(w)+(double)(x));

int cy = h+y;

int dx = (int)(.175*(double)(h)+(double)(x));

int dy = h+y;

int ex = x;

int ey = by;

Color colorSave = g.getColor(); // save previous color

g.setColor(c);

g.drawLine(ax,ay,cx,cy);

g.drawLine(bx,by,dx,dy);

g.drawLine(cx,cy,ex,ey);

g.drawLine(dx,dy,ax,ay);

g.drawLine(ex,ey,bx,by);

g.setColor(colorSave); // return it's previous color

}

EDIT EDIT EDIT::: wow... I'm kind of dumb. I could have made this a LOT easier than it was. You can even make it for an arbitrary number of sides! :-)

Just use the formula of (x,y) = (cos(t),sin(t)) - definition of a circle! Your points must be points on a circle - then just calculate a point, and the second point after it! Check out the example

make sure you define a

public static final double halfPI = Math.PI / 2;

at the top as a static member variable

public void paint(Graphics g) {

drawStar(g,Color.BLACK,5,300,300,100,1...

drawStar(g,Color.RED,6,100,100,20,20);

drawStar(g,Color.BLUE,9,200,400,40,40)...

drawStar(g,Color.YELLOW,27,400,200,10,...

drawStar(g,Color.GREEN,400,300,300,250...

}

public double circleX(int sides, int angle) {

double coeff = (double)angle/(double)sides;

return Math.cos(2*coeff*Math.PI-halfPI);

}

public double circleY(int sides, int angle) {

double coeff = (double)angle/(double)sides;

return Math.sin(2*coeff*Math.PI-halfPI);

}

public void drawStar(Graphics g, Color c, int sides, int x, int y, int w, int h) {

Color colorSave = g.getColor();

g.setColor(c);

for(int i = 0; i < sides; i++) {

int x1 = (int)(circleX(sides,i) * (double)(w)) + x;

int y1 = (int)(circleY(sides,i) * (double)(h)) + y;

int x2 = (int)(circleX(sides,(i+2)%sides) * (double)(w)) + x;

int y2 = (int)(circleY(sides,(i+2)%sides) * (double)(h)) + y;

g.drawLine(x1,y1,x2,y2);

}

}

}

Source(s):
I tested it, it works! :-)

Asker's rating