Using ELLIPSE in PShape not the same as using ellipse()

edited May 2014 in Questions about Code

When trying to convert a regularly drawn collection of shapes into one PShape, I was successful except for the fact the the ELLIPSE parameter draws circles based on a corner point, unlike ellipse(), which by default uses the center coordinates as the center of the circle. As you can see in the picture below, the circles have shifted from being drawn in the center to be drawn from the upper left hand corner (Picture on the right was drawn by PShape). Does anyone know how to fix this?

Thanks

Regular render vs. PShape

Here is the original code:

float radius = 120;

void setup()  {
  background(0);
  size(400,400);
  strokeWeight(5);
  stroke(255);
  fill(255,0,255,150);
  noLoop();
}
void draw()  {
  pushMatrix();
  translate(200,200);
  radius = 150;
  for(float i = (TWO_PI/7); i < TWO_PI; i += (TWO_PI/7))  {
    calcLine(i);
  }
  ellipse(0,0,radius,radius);
  popMatrix();
}  

void calcLine(float angle)  {
  float x = (cos(angle) * radius);
  float y = (sin(angle) * radius);
  line(0,0,x,y);
  pushStyle();
  fill(0,255,0,150);
  ellipse(x,y, 20,20);
  popStyle();
}

And here is the PShape code:

float radius = 150;
PShape[] lines = new PShape[7]; //PShape for the arms
PShape[] dots = new PShape[7];  //PShape for the dots on the arms
PShape arm;

void setup()  {
  //sets up the drawing board
  background(0);
  size(400,400,P2D);
  strokeWeight(5);
  stroke(255);
  fill(255,0,255,150);
  smooth();
  //calculates the arms of Ball
  int j = 0;
  for(float i = (TWO_PI/7); i < TWO_PI; i += (TWO_PI/7))  {
      calcLine(i, j);
      j += 1;
  }
  //groups PShapes for arms into one PShape
  arm = createShape(GROUP);
  for(int i = 0; i < 7; i++)  {
    arm.addChild(lines[i]);
    arm.addChild(dots[i]);
  }

  //draws ball
  pushMatrix();
  translate(200,200);
  shape(arm);
  ellipse(0,0,radius,radius);
  popMatrix();
}  

void calcLine(float angle, int j)  {
  float x = (cos(angle) * radius);
  float y = (sin(angle) * radius);
  lines[j] = createShape(LINE,0,0,x,y);
  pushStyle();
  fill(0,255,0,150);
  dots[j] = createShape(ELLIPSE,x,y, 20,20);
  popStyle();
}
Tagged:
Sign In or Register to comment.