-2

I'm trying to draw a hexagon shaped asteroid on a clone of Asteroids I'm making for a class.

    sprite = new Polygon();
    sprite.addPoint(0,0);
    sprite.addPoint(0,-40);
    sprite.addPoint(30,-40);
    sprite.addPoint(60,-10);
    sprite.addPoint(60,20);
    sprite.addPoint(40,50);
    sprite.addPoint(-20,50);
    sprite.addPoint(-50,20);
    sprite.addPoint(50,-10);
    sprite.addPoint(20,-40);

Yet when I do it, I end up with this :

So what is going wrong? I drew it out on a coordinate plane, and copied the points over. It was my understanding that Java would draw it out in the order I listed the points, and I had the (0,0) in there in the interest of rotating the asteroid for the game.

user1768884
  • 1,079
  • 1
  • 20
  • 34

4 Answers4

11

Zane is close, he forgot to include i in his formula:

for(i=0; i<6; i++) {
    sprite.addpoint(x + r*cos(i*2*pi/6), y + r*sin(i*2*pi/6))
}
Loran
  • 111
  • 1
  • 2
6

First, if it is supposed to be a hexagon, then it should have 6 points, not 10. Second, just drawing this on paper from your coordinates gives me quite a similar polygon as they one in your picture. So I guess your coordinates are wrong. Check them again.

If you want to draw a symmetric hexagon, then all you need is its center, say (x,y) and its radius r. Then the points of the hexagon are

for(i=0; i<6; i++) {
   sprite.addpoint(x + r*cos(i*2*pi/6), y + r*sin(i*2*pi/6))
}
Brent Writes Code
  • 19,075
  • 7
  • 52
  • 56
Zane
  • 926
  • 8
  • 21
5

It's not really a hexagon, the last two points look strange

sprite.addPoint(50,-10);
sprite.addPoint(20,-40);

Think those final two should be:

sprite.addPoint(-50,-10);
sprite.addPoint(-20,-40);

but even with that, its going to look a lop-sided pacman - back to the drawing board I think.

Andrew
  • 26,629
  • 5
  • 63
  • 86
1

I would check your coordinates. The last few transitions do not look right to me, especially (-50, 20) to (50, -10). It has a jump of 100 units in the x direction, bigger than any other change in coordinates. (-50, -10) seems more plausible.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75