/*
Selah Lynch problem.cc July 2004

Class Problem Definitions
see declarations for class description
*/

#include <stdio.h>
#include "problem.h"

Problem::Problem(char* filename){
  //printf("BEGIN Problem(char*)\n");
  ReadPoints(filename);
  //printf("END Problem(char*)\n");

}

Problem::Problem(Problem& p){
  //printf("BEGIN Problem(Problem&)\n");
  numpoints=p.numpoints;
  coords=new double* [numpoints];
  for(int i=0; i<numpoints; i++){
    coords[i] = new double [2];
    coords[i][X]=p.coords[i][X];
    coords[i][Y]=p.coords[i][Y];
  }
  //printf("END Problem(Problem&)\n");
}

Problem::~Problem(){
  //printf("BEGIN ~Problem()\n");

  for(int i=0; i<numpoints; i++)
    delete [] coords[i];
  delete [] coords;

  //printf("END ~Problem()\n");
}

double Problem::GetX(int index){
  return coords[index][X];
}

double Problem::GetY(int index){
  return coords[index][Y];
}

int Problem::GetNumPoints(){
  return numpoints;
}

void Problem::Display(){
  for(int i=0; i<numpoints; i++){
    printf("Point%d - (%f, %f)\n", i, GetX(i), GetY(i) );
  }
}

void Problem::ReadPoints(char* filename){
  //printf("BEGIN ReadPoints(char*)\n");
  ifstream fin(filename);
  check(fin.good(), "Input file failed to open");

  fin>>numpoints;
  //printf("CHECK1 ReadPoints(char*)\n");

  coords = new double* [numpoints];
  check(coords!=NULL, "Allocation of coords failed!");

  //printf("CHECK2 ReadPoints(char*)\n");

  for(int i=0; i<numpoints; i++){
    coords[i]= new double [2];
    fin>>coords[i][X]>>coords[i][Y];
  }
  //printf("END ReadPoints(char*)\n");

}



void Problem::check(bool b, char* mess){
  if(!b){
    cerr<<"ERROR! [Problem] - "<<mess<<endl;
    exit(0);
  }
}
