With n leverage, you end up with 2n vertices, even ones on the outer circle, and odd ones on the inner circle. When viewed from the center, the vertices are located at evenly spaced angles (the angle is 2 * PI / 2 * n = Pi / n). On the unit circle (r = 1), the coordinates x, y of the points i = 0..n are cos (x), sin (x). Multiply these coordinates with the appropriate radius (rOuter or rInner, depending on whether I am odd or even) and add this vector to the center of the star to get the coordinates for each vertex in the star path.
Here is the function of creating a star shape with a given number of hands, a central coordinate and an outer, inner radius:
public static Shape createStar(int arms, Point center, double rOuter, double rInner) { double angle = Math.PI / arms; GeneralPath path = new GeneralPath(); for (int i = 0; i < 2 * arms; i++) { double r = (i & 1) == 0 ? rOuter : rInner; Point2D.Double p = new Point2D.Double(center.x + Math.cos(i * angle) * r, center.y + Math.sin(i * angle) * r); if (i == 0) path.moveTo(p.getX(), p.getY()); else path.lineTo(p.getX(), p.getY()); } path.closePath(); return path; }
Peter Walser
source share