Alright, so this is for an assignment, and I have it so close to figured out. Basically, we were given the code to create a turtle and the test client draws a polygon with n sides. The instructions are to modify the test client so that it draws a star with n points. To do this with odd-numbered values of n was easy because I simply made the change from turtle.turnLeft(angle) to turtle.turnLeft(angle*2).
I think most of my trouble is coming from complete ignorance of star geometry. I tried to draw it out and kind of make triangles between the points, and I've tested a few different things, but I keep getting lines that resemble this: VVVV and turn slightly left.
original code:
public class Turtle
{
private double x, y;
private double angle;
public Turtle(double x0, double y0, double a0)
{ x=x0; y=y0; angle=a0;}
public void turnLeft(double delta)
{angle += delta;}
public void goForward(double step)
{
double oldx=x, oldy=y;
x+=step*Math.cos(Math.toRadians(angle));
y+=step*Math.sin(Math.toRadians(angle));
StdDraw.line(oldx, oldy, x, y);
}
public static void main(String[] args)
{
int N=Integer.parseInt(args[0]);
double angle = 360.0/N;
double step = Math.sin(Math.toRadians(angle/2));
Turtle turtle= new Turtle(0.5, 0.0, angle/2);
for (int i=0;i<N;i++)
{
turtle.goForward(step);
turtle.turnLeft(angle);
}}}
EDITED AGAIN:
revised code (again):
public static void main(String[] args)
{
int N=Integer.parseInt(args[0]);
double angle = 360.0/N;
double q = ((N-2)*180)/N;
double p = ((180-q)/2);
double t = (180-q);
double v = (180 - 2*t);
double step = Math.sin(Math.toRadians(angle/2));
Turtle turtle= new Turtle(0.5, 0.0, (angle/2 + p));
for (int i=0;i<N;i++)
{
turtle.goForward(step/2);
turtle.turnLeft(-t);
turtle.goForward(step/2);
turtle.turnLeft(180-v);
}}}
This one gives a perfect star for all values of N except 7 for some reason.