/******************************************************************************
  * Name: Donna Gabai
  * NetID: dgabai
  * Precept: P99
  * 
  * Description: An object class to represent a 2D regular polygon.
  * 
  * Dependencies: StdDraw
  ****************************************************************************/

import java.awt.Color;

public class Ngon {

    // instance variables
    private int n;         // number of sides
    private double r;      // center to vertex distance
    private double x;      // center x-coordinate
    private double y;      // center y-coordinate
    private Color c;       // color of the polygon
    private double angle;  // interior angle in radians
    
    // constructor for N-gon with radius r, centered at (x, y), color c
    public Ngon(int n, double r, double x, double y, Color c) {
        
        // put the values of the arguments into the instance variables
        this.n = n;
        this.r = r;
        this.x = x;
        this.y = y;
        this.c = c;
        
        // set the interior angle
        angle = 2 * Math.PI / n;
    }
    
    // constructor for N-gon with radius r, centered at (0, 0), black
    public Ngon(int n, double r) {
        
        // put the values of the arguments into the instance variables
        this.n = n;
        this.r = r;
        
        // "this" is optional below
        this.x = 0;
        this.y = 0;
        this.c = StdDraw.BLACK;
        
        // set the interior angle
        angle = 2 * Math.PI / n;
    }
    
    // sets the pen color and draws the diamond
    public void drawFilled() {
        
        // compute the vertices and pack into x and y arrays
        double[] xPts = new double[n];
        double[] yPts = new double[n];
        
        for (int i = 0; i < n; i++) {
            xPts[i] = x + r * Math.cos(i * angle);
            yPts[i] = y + r * Math.sin(i * angle);
        }
        
        // set pen color
        StdDraw.setPenColor(c);
        
        // draw
        StdDraw.filledPolygon(xPts, yPts);
    }
    
    // returns the area of the polygon: area = 1/2 * (r squared)*n*sin(angle)
    public double area() {
        double a = 0.5 * r * r * n * Math.sin(angle);
        return a;
    }
    
    // merges with another Ngon with the same number of sides
    public Ngon merge(Ngon that) {
        if (this.n != that.n) return null;
        
        // local variables for the new ngon
        double r =  this.r + that.r;
        double x = (this.x + that.x) / 2.0;
        double y = (this.y + that.y) / 2.0;
        
        // average r, g, b values for the new color
        int newR = (this.c.getRed()   + that.c.getRed())   / 2;
        int newG = (this.c.getGreen() + that.c.getGreen()) / 2;
        int newB = (this.c.getBlue()  + that.c.getBlue())  / 2;
        
        // create the Color object
        Color c = new Color(newR, newG, newB);
        
        // create the new Ngon
        Ngon newGon = new Ngon(n, r, x, y, c);
        
        return newGon;
    }
    
    // NOTE: The provided main() does not test the second constructor. 
    public static void main(String[] args) {
        
        // provided tests
        StdDraw.setXscale(-1, 1); // set scale
        StdDraw.setYscale(-1, 1); // in main()
        Color red = StdDraw.RED;
        Color blue = StdDraw.BLUE;
        Ngon h1 = new Ngon(6, .3, -.6, .6, red);
        Ngon h2 = new Ngon(6, .3, .6, -.6, blue);
        Ngon h3 = h1.merge(h2);
        h1.drawFilled();
        h2.drawFilled();
        h3.drawFilled();
        System.out.println(h3.area());
        
        // added tests for second constructor
        Ngon diamond = new Ngon(4, .2);
        diamond.drawFilled();
        System.out.println(diamond.area()); //2*.2^2=.08
    }
}