How to create nested polygon

BTW. I’m not sure if this would help or make it harder, but there’s this nice thing called PVector that can be used to represent a point. So finding a point between A and B is very easy in that case.

PVector a = new PVector(10, 10);
PVector b = new PVector(100, 30);
PVector c = PVector.lerp(a, b, 0.50);

That gives you a c that is exactly in the middle between a and b. Lerp means linear-interpolation. Then you can access c.x and c.y to get the coordinates.

You don’t need to use 0.50. If you use 0.0, you get a. With 1.0 you get b. With 0.20 you get a point between a and b, that is 1/5 of the the distance from a to b.

The program from above using PVector and not exactly at the middle (0.25):

PVector a, b;

void setup() {
  size(400, 400);
  a = new PVector(200, 200);
  b = new PVector(100, 100);
  fill(0);
  stroke(255);
}

void draw() {
  background(0);
  b.set(mouseX, mouseY);
  line(a.x, a.y, b.x, b.y);
  ellipse(a.x, a.y, 5, 5);
  ellipse(b.x, b.y, 5, 5);
  PVector c = PVector.lerp(a, b, 0.25);
  ellipse(c.x, c.y, 5, 5);
}

void mousePressed() {
  a.set(mouseX, mouseY);
}
1 Like