lab 5 part 2 complete
This commit is contained in:
122
Lab5/image.cpp
122
Lab5/image.cpp
@@ -10,21 +10,19 @@
|
||||
*/
|
||||
|
||||
#include "image.h"
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
|
||||
// TODO
|
||||
Image::Image(){
|
||||
shapes.clear();
|
||||
}
|
||||
|
||||
// TODO
|
||||
Image::Image(const Image& from){
|
||||
for(int i = 0; i < from.shapes.size(); i++){
|
||||
shapes.push_back(from.shapes[i]->clone());
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
Image::~Image(){
|
||||
for(int i = 0; i < shapes.size(); i++){
|
||||
delete shapes[i];
|
||||
@@ -32,31 +30,29 @@ Image::~Image(){
|
||||
shapes.clear();
|
||||
}
|
||||
|
||||
// TODO
|
||||
Image &Image::operator=(const Image& rhs){
|
||||
for(int i = 0; i < shapes.size(); i++){
|
||||
delete shapes[i];
|
||||
}
|
||||
shapes.clear();
|
||||
for(int i = 0; i < rhs.shapes.size(); i++){
|
||||
shapes.push_back(rhs.shapes[i]->clone());
|
||||
if(this != &rhs){
|
||||
for(int i = 0; i < shapes.size(); i++){
|
||||
delete shapes[i];
|
||||
}
|
||||
shapes.clear();
|
||||
for(int i = 0; i < rhs.shapes.size(); i++){
|
||||
shapes.push_back(rhs.shapes[i]->clone());
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// TODO
|
||||
void Image::add(Shape* shape){
|
||||
shapes.push_back(shape);
|
||||
shapes.push_back(shape->clone());
|
||||
}
|
||||
|
||||
// TODO
|
||||
void Image::draw(GraphicsContext* gc){
|
||||
for(int i = 0; i < shapes.size(); i++){
|
||||
shapes[i]->draw(gc);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
void Image::out(std::ostream& os){
|
||||
for(int i = 0; i < shapes.size(); i++){
|
||||
shapes[i]->out(os);
|
||||
@@ -64,12 +60,102 @@ void Image::out(std::ostream& os){
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
void Image::in(std::istream& is){
|
||||
|
||||
string line;
|
||||
string token;
|
||||
int shapeCoords[3][3];
|
||||
getline(is, line);
|
||||
istringstream lineStream(line);
|
||||
do{
|
||||
lineStream >> token;
|
||||
switch(token[0]){
|
||||
case 'L':
|
||||
{
|
||||
// Next Line
|
||||
getline(is, line);
|
||||
lineStream.str(line);
|
||||
lineStream.clear();
|
||||
// Iterate past "Color:"
|
||||
lineStream >> token;
|
||||
// Get actual color code
|
||||
lineStream >> token;
|
||||
stringstream hexColor(token);
|
||||
// Convert hex code to unsigned int
|
||||
uint32_t color;
|
||||
hexColor >> hex >> color;
|
||||
// hexColor >> color;
|
||||
// Points
|
||||
for(int i = 0; i < 2; i++){
|
||||
getline(is, line);
|
||||
lineStream.str(line);
|
||||
lineStream.clear();
|
||||
// Iterate past "P#:"
|
||||
lineStream >> token;
|
||||
// Get each coord
|
||||
for(int j = 0; j < 3; j++){
|
||||
lineStream >> token;
|
||||
shapeCoords[i][j] = stoi(token);
|
||||
}
|
||||
}
|
||||
shapes.push_back(
|
||||
new Line(shapeCoords[0][0],shapeCoords[0][1],
|
||||
shapeCoords[1][0],shapeCoords[1][1],color)
|
||||
);
|
||||
break;
|
||||
}
|
||||
case 'T':
|
||||
{
|
||||
// Next Line
|
||||
getline(is, line);
|
||||
lineStream.str(line);
|
||||
lineStream.clear();
|
||||
// Iterate past "Color:"
|
||||
lineStream >> token;
|
||||
// Get actual color code
|
||||
lineStream >> token;
|
||||
stringstream hexColor(token);
|
||||
// Convert hex code to unsigned int
|
||||
uint32_t color;
|
||||
hexColor >> hex >> color;
|
||||
// hexColor >> color;
|
||||
// Points
|
||||
for(int i = 0; i < 3; i++){
|
||||
getline(is, line);
|
||||
lineStream.str(line);
|
||||
lineStream.clear();
|
||||
// Iterate past "P#:"
|
||||
lineStream >> token;
|
||||
// Get each coord
|
||||
for(int j = 0; j < 3; j++){
|
||||
lineStream >> token;
|
||||
shapeCoords[i][j] = stoi(token);
|
||||
}
|
||||
}
|
||||
shapes.push_back(
|
||||
new Triangle(shapeCoords[0][0],shapeCoords[0][1],
|
||||
shapeCoords[1][0],shapeCoords[1][1],
|
||||
shapeCoords[2][0],shapeCoords[2][1],color)
|
||||
);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
cout << "Invalid Shape";
|
||||
}
|
||||
|
||||
}
|
||||
// Go to blank line
|
||||
getline(is, line);
|
||||
lineStream.str(line);
|
||||
lineStream.clear();
|
||||
// Get next line
|
||||
getline(is, line);
|
||||
lineStream.str(line);
|
||||
lineStream.clear();
|
||||
lineStream >> token;
|
||||
} while(!is.eof()); // Loop until end of file
|
||||
}
|
||||
|
||||
// TODO
|
||||
void Image::erase(){
|
||||
for(int i = 0; i < shapes.size(); i++){
|
||||
delete shapes[i];
|
||||
|
||||
Reference in New Issue
Block a user