Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- struct rectangle {
- int bottom_left_x,
- bottom_left_y,
- top_right_x,
- top_right_y;
- };
- bool check_territory(const std::vector<rectangle>& graph, const rectangle& to_check);
- bool are_ints(const std::string& str);
- int main() {
- std::vector<rectangle> graph;
- std::string input;
- rectangle new_rect;
- while(true) {
- std::cout << "[!] Input graph coords. ";
- std::cin >> input;
- if (input.size() != 4 || !are_ints(input)) {
- std::cout << "[!] ERROR: Invalid Input. Try again..." << std::endl;
- continue;
- }
- new_rect = {
- int(input[0]) - 48,
- int(input[1]) - 48,
- int(input[2]) - 48,
- int(input[3]) - 48
- };
- if (check_territory(graph,new_rect)) {
- graph.push_back(new_rect);
- std::cout << "[!] This rectangle will fit. "
- << "Adding to graph... Ctrl+C to finish. "
- << std::endl;
- } else {
- std::cout << "[!] This rectangle does not fit. Try again."
- << std::endl;
- }
- }
- return 0;
- }
- bool check_territory(const std::vector<rectangle>& graph, const rectangle& to_check) {
- if (graph.size() == 0) return true;
- for (auto& rect : graph) {
- if ( !( to_check.bottom_left_x < rect.bottom_left_x
- || to_check.bottom_left_y < rect.bottom_left_y )
- || !( to_check.top_right_x > rect.top_right_x
- || to_check.top_right_y > rect.top_right_y )
- || !( to_check.bottom_left_x < rect.top_right_x
- || to_check.bottom_left_y < rect.top_right_y )
- || !( to_check.top_right_x > rect.bottom_left_x
- || to_check.top_right_y > rect.bottom_left_y )
- || !( to_check.bottom_left_x > rect.top_right_x
- || to_check.top_right_y < rect.bottom_left_y )
- || !( to_check.top_right_x < rect.bottom_left_x
- || to_check.bottom_left_y > rect.top_right_y )
- )
- {
- return false;
- }
- }
- return true;
- }
- bool are_ints(const std::string& str) {
- for (auto& c : str)
- if (c < '0' || c > '9')
- return false;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement