Created
June 13, 2020 10:26
-
-
Save Dominos/226b651a508cb48ce7272f7dd3e447e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
#include <cmath> | |
#include <sstream> | |
#include <string> | |
using namespace std; | |
class Shape { | |
public: | |
Shape() {} | |
virtual bool isInShape(int x, int y) { return false; }; | |
}; | |
class Circle : public Shape { | |
public: | |
Circle() {}; | |
Circle(int widthCoordinate, int heightCoordinate, int radius); | |
int getRadius(); | |
int* getMiddleCoordinates(); | |
void setRadius(int radius); | |
void setMiddleCoordinates(int x, int y); | |
bool isInShape(int x, int y); | |
private: | |
int radius, widthCoordinate, heightCoordinate; | |
int* middleCoordinates; | |
}; | |
Circle::Circle(int widthCoordinate, int heightCoordinate, int radius) { | |
this->radius = radius; | |
} | |
int Circle::getRadius() { | |
return this->radius; | |
} | |
void Circle::setRadius(int radius) { | |
this->radius = radius; | |
} | |
int* Circle::getMiddleCoordinates() { | |
if (middleCoordinates == NULL){ | |
int* coordinates = new int[2]; | |
coordinates[0] = widthCoordinate; | |
coordinates[1] = heightCoordinate; | |
middleCoordinates = coordinates; | |
} | |
return middleCoordinates; | |
} | |
void Circle::setMiddleCoordinates(int x, int y) { | |
this->widthCoordinate = x; | |
this->heightCoordinate = y; | |
} | |
bool Circle::isInShape(int x, int y) { | |
return pow((x - this->getMiddleCoordinates()[0]), 2) + pow((y - this->getMiddleCoordinates()[1]), 2) < | |
pow(getRadius(), 2); | |
} | |
class Rectangle : public Shape { | |
public: | |
Rectangle() {}; | |
Rectangle(int, int, int, int, int, int, int, int); | |
int* getLeftBottomCoordinates(); | |
int* getRightBottomCoordinates(); | |
int* getRightTopCoordinates(); | |
int* getLeftTopCoordinates(); | |
void setLeftBottomCoordinates(int x, int y); | |
void setRightBottomCoordinates(int x, int y); | |
void setRightTopCoordinates(int x, int y); | |
void setLeftTopCoordinates(int x, int y); | |
bool isInShape(int x, int y); | |
private: | |
bool isSet; | |
int* horizontalCoordinates; | |
int* verticalCoordinates; | |
int* leftBottomCoordinates = new int[2]; | |
int* rightBottomCoordinates = new int[2]; | |
int* rightTopCoordinates = new int[2]; | |
int* leftTopCoordinates = new int[2]; | |
}; | |
Rectangle::Rectangle(int leftBottomWidth, | |
int leftBottomHeight, | |
int rightBottomWidth, | |
int rightBottomHeight, | |
int rightTopWidth, | |
int rightTopHeight, | |
int leftTopWidth, | |
int leftTopHeight) { | |
this->leftBottomCoordinates[0] = leftBottomWidth; | |
this->leftBottomCoordinates[1] = leftBottomHeight; | |
this->rightBottomCoordinates[0] = rightBottomWidth; | |
this->rightBottomCoordinates[1] = rightBottomHeight; | |
this->rightTopCoordinates[0] = rightTopWidth; | |
this->rightTopCoordinates[1] = rightTopHeight; | |
this->leftTopCoordinates[0] = leftTopWidth; | |
this->leftTopCoordinates[1] = leftTopHeight; | |
} | |
int* Rectangle::getLeftBottomCoordinates() { | |
return leftBottomCoordinates; | |
} | |
int* Rectangle::getRightBottomCoordinates() { | |
return rightBottomCoordinates; | |
} | |
int* Rectangle::getRightTopCoordinates() { | |
return rightTopCoordinates; | |
} | |
int* Rectangle::getLeftTopCoordinates() { | |
return leftTopCoordinates; | |
} | |
void Rectangle::setLeftBottomCoordinates(int x, int y) { | |
this->leftBottomCoordinates[0] = x; | |
this->leftBottomCoordinates[1] = y; | |
} | |
void Rectangle::setRightBottomCoordinates(int x, int y) { | |
this->rightBottomCoordinates[0] = x; | |
this->rightBottomCoordinates[1] = y; | |
} | |
void Rectangle::setRightTopCoordinates(int x, int y) { | |
this->rightTopCoordinates[0] = x; | |
this->rightTopCoordinates[1] = y; | |
} | |
void Rectangle::setLeftTopCoordinates(int x, int y) { | |
this->leftTopCoordinates[0] = x; | |
this->leftTopCoordinates[1] = y; | |
} | |
bool Rectangle::isInShape(int x, int y) { | |
if (!isSet) { | |
int horizontalSize = getRightBottomCoordinates()[0] - getLeftBottomCoordinates()[0]; | |
int verticalSize = getLeftTopCoordinates()[1] - getLeftBottomCoordinates()[1]; | |
horizontalCoordinates = new int[horizontalSize]; | |
verticalCoordinates = new int[verticalSize]; | |
int j = 0; | |
for (int i = getLeftBottomCoordinates()[0]; i <= getRightBottomCoordinates()[0]; i++) { | |
horizontalCoordinates[j] = i; | |
j++; | |
} | |
j = 0; | |
for (int i = getLeftBottomCoordinates()[1]; i <= getLeftTopCoordinates()[1]; i++) { | |
verticalCoordinates[j] = i; | |
j++; | |
} | |
isSet = true; | |
} | |
bool isHorizontal = false, isVertical = false; | |
for (int i = 0; i < getRightBottomCoordinates()[0] - getLeftBottomCoordinates()[0]; i++) { | |
if (horizontalCoordinates[i] == x) { | |
isHorizontal = true; | |
} | |
if (isHorizontal) | |
continue; | |
} | |
for (int i = 0; i < getLeftTopCoordinates()[1] - getLeftBottomCoordinates()[1]; i++) { | |
if (verticalCoordinates[i] == y) { | |
isVertical = true; | |
} | |
if (isVertical) | |
continue; | |
} | |
bool isValid = isVertical && isHorizontal; | |
return isValid; | |
} | |
class Square : public Shape { | |
public: | |
Square() {}; | |
Square(int, int, int, int, int, int, int, int); | |
int* getLeftBottomCoordinates(); | |
int* getRightBottomCoordinates(); | |
int* getRightTopCoordinates(); | |
int* getLeftTopCoordinates(); | |
void setLeftBottomCoordinates(int x, int y); | |
void setRightBottomCoordinates(int x, int y); | |
void setRightTopCoordinates(int x, int y); | |
void setLeftTopCoordinates(int x, int y); | |
bool isInShape(int, int); | |
private: | |
bool isSet; | |
int* horizontalCoordinates; | |
int* verticalCoordinates; | |
int* leftBottomCoordinates = new int[2]; | |
int* rightBottomCoordinates = new int[2]; | |
int* rightTopCoordinates = new int[2]; | |
int* leftTopCoordinates = new int[2]; | |
}; | |
Square::Square(int leftBottomWidth, | |
int leftBottomHeight, | |
int rightBottomWidth, | |
int rightBottomHeight, | |
int rightTopWidth, | |
int rightTopHeight, | |
int leftTopWidth, | |
int leftTopHeight) { | |
this->leftBottomCoordinates[0] = leftBottomWidth; | |
this->leftBottomCoordinates[1] = leftBottomHeight; | |
this->rightBottomCoordinates[0] = rightBottomWidth; | |
this->rightBottomCoordinates[1] = rightBottomHeight; | |
this->rightTopCoordinates[0] = rightTopWidth; | |
this->rightTopCoordinates[1] = rightTopHeight; | |
this->leftTopCoordinates[0] = leftTopWidth; | |
this->leftTopCoordinates[1] = leftTopHeight; | |
} | |
int* Square::getLeftBottomCoordinates() { | |
return leftBottomCoordinates; | |
} | |
int* Square::getRightBottomCoordinates() { | |
return rightBottomCoordinates; | |
} | |
int* Square::getRightTopCoordinates() { | |
return rightTopCoordinates; | |
} | |
int* Square::getLeftTopCoordinates() { | |
return leftTopCoordinates; | |
} | |
void Square::setLeftBottomCoordinates(int x, int y) { | |
this->leftBottomCoordinates[0] = x; | |
this->leftBottomCoordinates[1] = y; | |
} | |
void Square::setRightBottomCoordinates(int x, int y) { | |
this->rightBottomCoordinates[0] = x; | |
this->rightBottomCoordinates[1] = y; | |
} | |
void Square::setRightTopCoordinates(int x, int y) { | |
this->rightTopCoordinates[0] = x; | |
this->rightTopCoordinates[1] = y; | |
} | |
void Square::setLeftTopCoordinates(int x, int y) { | |
this->leftTopCoordinates[0] = x; | |
this->leftTopCoordinates[1] = y; | |
} | |
bool Square::isInShape(int x, int y) { | |
if (!isSet) { | |
int size = getRightBottomCoordinates()[0] - getLeftBottomCoordinates()[0]; | |
horizontalCoordinates = new int[size]; | |
verticalCoordinates = new int[size]; | |
int j = 0; | |
for (int i = getLeftBottomCoordinates()[0]; i <= getRightBottomCoordinates()[0]; i++) { | |
horizontalCoordinates[j] = i; | |
j++; | |
} | |
j = 0; | |
for (int i = getLeftBottomCoordinates()[1]; i <= getLeftTopCoordinates()[1]; i++) { | |
verticalCoordinates[j] = i; | |
j++; | |
} | |
isSet = true; | |
} | |
bool isHorizontal = false, isVertical = false; | |
for (int i = 0; i < getRightBottomCoordinates()[0] - getLeftBottomCoordinates()[0]; i++) { | |
if (horizontalCoordinates[i] == x) { | |
isHorizontal = true; | |
} | |
if (isHorizontal) | |
continue; | |
} | |
for (int i = 0; i < getLeftTopCoordinates()[1] - getLeftBottomCoordinates()[1]; i++) { | |
if (verticalCoordinates[i] == y) { | |
isVertical = true; | |
} | |
if (isVertical) | |
continue; | |
} | |
bool isValid = isVertical && isHorizontal; | |
return isValid; | |
} | |
string intToString(int x) { | |
ostringstream ss; | |
ss << x; | |
string converted = ss.str(); | |
return converted; | |
} | |
int main(){ | |
int n ; | |
cin >> n; | |
Shape* shapes[n]; | |
char type; | |
int x,y; | |
for (int i = 0; i < n; i++) { | |
cin >> type; | |
switch (type) { | |
case 'o': { | |
cin >> x >> y; | |
Circle *circle = new Circle(); | |
circle->setMiddleCoordinates(x,y); | |
cin >> x; | |
circle->setRadius(x); | |
shapes[i] = circle; | |
break; | |
} | |
case 'p': { | |
int points[8]; | |
for (int j = 0; j < 8; j++) | |
cin >> points[j]; | |
shapes[i] = new Rectangle( | |
points[0],points[1],points[2], | |
points[3],points[4],points[5], | |
points[6],points[7]); | |
break; | |
} | |
case 'k': { | |
int points[8]; | |
for (int j = 0; j < 8; j++) | |
cin >> points[j]; | |
shapes[i] = new Square( | |
points[0],points[1],points[2], | |
points[3],points[4],points[5], | |
points[6],points[7]); | |
break; | |
} | |
default: | |
break; | |
} | |
} | |
int m; | |
cin >> m; | |
string output; | |
for (int i = 0; i < m; i++) { | |
cin >> x >> y; | |
bool isEmpty = true; | |
for (int j = 0; j < n; j++) { | |
if (shapes[j]->isInShape(x,y)) { | |
if (!isEmpty) { | |
output.append(" "); | |
} else { | |
if (i != 0) | |
output.append("\n"); | |
isEmpty = false; | |
} | |
output.append(intToString(j)); | |
} | |
} | |
} | |
cout << output; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment