Due: Thursday 2/6 10:00am

Submission name: w03_centroid

Skills: 23, 24

A Little Geometry:

The centroid of a polygon is a formulaic way of identifying what we would consider the “center”, which is non-trivial for irregular polygons. To find the centroid you must iterate over all the vertices of the polygon.

  • For each point from 0 to the number of points - 1, take the sum of the following, stored separately (like in sumX and sumY variables):
    • \[(x_{i} + x_{i+1})(x_{i}y_{i+1} - x_{i+1}y_{i})\]
    • \[(y_{i} + y_{i+1})(x_{i}y_{i+1} - x_{i+1}y_{i})\]
    • When i gets to the last point, perform the calculations above replacing \(i + 1\) with 0. That is to say the correct calculation requires the last point to loop back around to the first.
  • Take each of those sums and divide them by 6 times the signed area of the polygon.

Add Centroid to our Shapes

With all this in mind, add the following features to the PathShape class from last week:

  • 1 new instance variables:
    • PVector centroid: an array to store the x and y values of the centroid.
  • 1 new method
    • void setCentroid()
      • Sets the centroid instance variable.
  • 2 modified methods
    • display
      • Draws a circle of diameter 5 at the centroid. Use a different color than the inside instance variable.
    • makeRandomShape() or the Constructor
      • call setCentroid() after setArea()

Create a Septagon Class

A Septagon is a seven sided polygon. Create a Septagon subclass of Polygon that only contains code that maintains the seven-sidedness of the polygons.

a03-pathcentroid