How to Make an Eight Corner Star Using PVector and PShape

I've gotten all eight corners, the only problem is, I have an extra line. But whenever I remove P10, it becomes a seven sided star. Here is what I have so far:

float radius = 200;
PVector p1 = new PVector(cos(radians(0))*radius, sin(radians(0))*radius);
PVector p2 = new PVector(cos(radians(45))*radius, sin(radians(45))*radius);
PVector p3 = new PVector(cos(radians(90))*radius, sin(radians(90))*radius);
PVector p4 = new PVector(cos(radians(135))*radius, sin(radians(135))*radius);
PVector p5 = new PVector(cos(radians(180))*radius, sin(radians(180))*radius);
PVector p6 = new PVector(cos(radians(225))*radius, sin(radians(225))*radius);
PVector p7 = new PVector(cos(radians(270))*radius, sin(radians(270))*radius);
PVector p8 = new PVector(cos(radians(315))*radius, sin(radians(315))*radius);
PVector p9 = new PVector(cos(radians(360))*radius, sin(radians(360))*radius);
PVector p10 = new PVector(cos(radians(45))*radius, sin(radians(45))*radius);
PShape star;

void setup(){
  size(500,500);
  background(255);
}

void draw(){
  pushMatrix();
  translate(250,250);
  rotate(radians(20));
  scale(0.5);
  star = createShape();
  star.beginShape();
  //star.stroke(255,255,0);
  //star.fill(255,255,0);
  star.vertex(p1.x,p1.y);
  star.vertex(p3.x,p3.y);
  star.vertex(p5.x,p5.y);
  star.vertex(p7.x,p7.y);
  star.vertex(p9.x,p9.y);
  star.vertex(p2.x,p2.y);
  star.vertex(p4.x,p4.y);
  star.vertex(p6.x,p6.y); 
  star.vertex(p8.x,p8.y);
  star.vertex(p10.x,p10.y);
  star.endShape(CLOSE);
  shape(star);
  popMatrix();

}
Tagged:

Answers

  • If you wish to close the stars by having two rectangles (like you have in your example), you should also treat it as two rectangles, thus using two shapes (1 for the first rect, the other for the other rect). If you want to create the pattern without the inner lines as 2 rectangles such as this one:

    http://www.robinsfyi.com/holidays/christmas/images/pateightpointstar.gif

    Then as you can see you can begin the shape at 1 point, and create a shape that ends at the same point.

  • If you use beginShape (QUADS) then it'll take each set of 4 vertices as the corners of a rectangle...

  • I'm actually trying to make it look like this:

    Screen Shot 2017-10-07 at 10.20.56 PM

  • edited October 2017

    Ok, use a piece of paper and number the points from 0 to 7... See if you can spot a pattern - you can draw all the lines in one for loop.

  • edited October 2017

    @cookiesandcreamio -- currently you are attempting to draw:

    1,3,5,7,9,2,4,6,8,10,CLOSE (10->1).

    That is not what you want.

    If you label the point numbers on a piece of paper as koogs suggests and then trace between the points with a pencil, you will see the sequence of numbers that you want.

    1,4,7... ?

  • We are computer programmers though, we start counting from 0...

  • edited October 2017

    ....so to do that, you would name your variables p0, p1, p2... p9.

    Later, you could change your code to replace your manual list of 10 separate PVector variables with that a single array of 10 PVectors, PVector[10] pvs -- and you would access the elements with pv[0], pv[1]... pv[9].

Sign In or Register to comment.