# shapes3.py
from Processing import *
import math

window(500, 500)

# Utilities

# Given shape points, compute bounding box
def boundingBox( pts ):
    minX, maxX = pts[0][0], pts[0][0]
    minY, maxY = pts[0][1], pts[0][1]
    for p in pts:
        if p[0] < minX:
            minX = p[0]
        elif p[0] > maxX:
            maxX = p[0]
        if p[1] < minY:
            minY = p[1]
        elif p[1] > maxY:
            maxY = p[1]
    return [minX, minY, maxX, maxY]

# Rectangle Class
class Rectangle:
    def __init__(self, pts):
        self.pts = pts
        self.strokeColor = color(32)
        self.fillColor = color(255, 128, 128)
        self.bbox = boundingBox(self.pts)
        self.width = self.bbox[2] - self.bbox[0]
        self.height = self.bbox[3] - self.bbox[1]
        self.centerX = 0.5 * (self.bbox[2] + self.bbox[0])
        self.centerY = 0.5 * (self.bbox[3] + self.bbox[1])

    def draw(self):
        rectMode(CORNER)
        fill( self.fillColor )
        stroke( self.strokeColor )
        rect(self.bbox[0], self.bbox[1], self.width, self.height)

# Ellipse Class
class Ellipse:
    def __init__(self, pts):
        self.pts = pts
        self.strokeColor = color(32)
        self.fillColor = color(255, 128, 128)
        self.bbox = boundingBox(self.pts)
        self.width = self.bbox[2] - self.bbox[0]
        self.height = self.bbox[3] - self.bbox[1]
        self.centerX = 0.5 * (self.bbox[2] + self.bbox[0])
        self.centerY = 0.5 * (self.bbox[3] + self.bbox[1])
        
    def draw(self):
        ellipseMode(CORNER)
        fill( self.fillColor )
        stroke( self.strokeColor )
        ellipse(self.bbox[0], self.bbox[1], self.width, self.height)

shapes = []
def draw(o, e):
  background(200)
  for s in shapes:
      s.draw()

frameRate(20)
onLoop += draw
loop()

shapes.append( Rectangle ( [[100, 200], [200, 250]] ) )
shapes.append( Ellipse( [[200, 100], [270, 160]] ) )
