1

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.

enter image description here

enter image description here

enter image description here

  • 1
    Are you wanting just the outline of a star? Or do you want it to look kind of like the odd-numbered ones, with each vertex connected to its next-but-one neighbour? – Dawood ibn Kareem Mar 10 '14 at 02:23
  • Honestly, I don;t think it matters, but I was aiming for the outline. If making it look like the odd-numbered ones would be easier, I'm open to doing it that way. –  Mar 10 '14 at 02:34
  • 1
    No, I think going for the outline will be easier. So, you're going to have to work out what the angle is for the points of the star, and what the angle is for the inner vertices of the star, then draw `n*2` segments, alternating what angles you turn your turtle by. Does that make sense? Do you need my help calculating the angles? – Dawood ibn Kareem Mar 10 '14 at 02:35
  • That helps, but I don't know how to calculate the angles of the points or the inner vertices of the star. –  Mar 10 '14 at 02:42
  • 1
    OK. So did you deal with the outer vertices OK? There's a bit of freedom here, depending on how pointy you want the star to be. What matters is that the difference between the two angles is `360.0/n`. So whatever angle you used for the outer vertices, just subtract `360.0/n` and turn in the opposite direction. – Dawood ibn Kareem Mar 10 '14 at 02:47
  • this is also helping: http://www.algebra.com/algebra/homework/Polygons/Polygons.faq.question.225075.html –  Mar 10 '14 at 02:48
  • 1
    You are very welcome. Don't forget that the angle that you want to turn your turtle is always 180 MINUS the actual angle you want drawn. So in the five-pointed example, to make your pointy angle of 36 degrees, you actually want to turn your turtle 144 degrees. I hope that makes sense. – Dawood ibn Kareem Mar 10 '14 at 02:50
  • I keep getting really close, but not quite there. editing original post and hoping you see this. –  Mar 10 '14 at 03:16
  • 1
    OK, I've seen your revision. What happens if you change the line where you set `turn2` to `double turn2 = angle - turn;` ? (Sorry, I don't have any software handy to try this myself) – Dawood ibn Kareem Mar 10 '14 at 03:40
  • 1
    Oh, I didn't see your newest revision till now. That looks to me like a rounding error - maybe your turtle software only allows whole numbers of degrees for the angle. Do you get the same problem when `n = 11` and when `n = 13`, or do those work OK? – Dawood ibn Kareem Mar 10 '14 at 04:39
  • n=11 and n=13 have the same little cross thing going on. Weird. –  Mar 10 '14 at 04:48
  • Do you know if there's a way for me to fix it? –  Mar 10 '14 at 04:49
  • and again, thank you so much for all of your help! –  Mar 10 '14 at 04:49
  • I've been thinking about how to fix it. Any method that I think of seems far too difficult for what you've been asked to do. So I'm sure your teacher will overlook these little cross things. If you can tell me what the turtle software you're using is, I could have a little bit of a play and see if I can come up with any clever ideas. – Dawood ibn Kareem Mar 10 '14 at 04:54
  • Also, would you like me to have a look at that other question that you asked - the one with the intersecting rectangles? It wasn't clear from the comments whether you found an answer to it or not. – Dawood ibn Kareem Mar 10 '14 at 04:55
  • I'm going to add to my lab report that a rounding error is probably responsible for the little cross. But by "turtle software" I'm not entirely sure what you mean. I'll answer what I think you mean: the drawings are generated by Standard draw (http://introcs.cs.princeton.edu/java/stdlib/StdDraw.java.html) code that is provided with the textbook. I keep a copy of StdDraw in the same folder as my program. Here's the api: http://introcs.cs.princeton.edu/java/stdlib/javadoc/StdDraw.html –  Mar 10 '14 at 04:59
  • I did manage to figure out my rectangles! I posted the edited code, and someone told me it was wrong; however, she/he did not realize that the values of x and y were the center of the rectangle. –  Mar 10 '14 at 05:01
  • Cool! Well done with the rectangles. I won't bother looking at the rectangle question then, if you're happy with how it worked out. But I will have a look at Standard Draw later, and see if I can do anything about your rounding error. Probably a few hours from now. – Dawood ibn Kareem Mar 10 '14 at 05:06
  • 2
    I found your problem. You need to change the line that sets `q` to `double q = ((N-2)*180.0)/N;` - what's happening now is an integer division, so `q` is slightly off what you want it to be. – Dawood ibn Kareem Mar 10 '14 at 07:06

1 Answers1

1
double q = ((N-2)*180.)/N;

This is to ensure that the whole expression is computed using doubles and avoid truncation of the decimal part... (not a problem for N=3, 4, 5, 6, 8, 9, 10, 12, 15, 18, 20...).