Created
February 4, 2017 19:05
-
-
Save frp/0fc32c8a7a40c54ab27cb0f67a15a88e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <fstream> | |
| #include <iostream> | |
| #include <cmath> | |
| #include <cstdlib> | |
| #include <array> | |
| using namespace std; | |
| struct point { | |
| int64_t x, y; | |
| }; | |
| struct rect { | |
| point p1, p2, p3, p4; | |
| }; | |
| istream& operator>>(istream& stream, point& p) { | |
| return stream >> p.x >> p.y; | |
| } | |
| istream& operator>>(istream& stream, rect& r) { | |
| return stream >> r.p1 >> r.p2 >> r.p3 >> r.p4; | |
| } | |
| // returns oriented double area of triangle | |
| int64_t area(point p1, point p2, point p3) { | |
| return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x); | |
| } | |
| // retruns true if point belongs to rectangle | |
| bool belongs_to_rect(point p, rect r) { | |
| int64_t s_area = 0 ; | |
| // the idea is that if the point is inside, the sum of the areas of | |
| // triangles it makes with the sides of rectangle will add up to the area of | |
| // the rectangle | |
| s_area += abs(area(p, r.p1, r.p2)); | |
| s_area += abs(area(p, r.p2, r.p3)); | |
| s_area += abs(area(p, r.p3, r.p4)); | |
| s_area += abs(area(p, r.p4, r.p1)); | |
| int64_t r_area = abs(area(r.p1, r.p2, r.p3)) + abs(area(r.p1, r.p3, r.p4)); | |
| return r_area == s_area; | |
| } | |
| int main() { | |
| ifstream in("input.txt"); | |
| ofstream out("output.txt"); | |
| int n; | |
| in >> n; | |
| int result = 0; | |
| for (int i = 0; i < n; i++) { | |
| point p; rect r; | |
| in >> p >> r; | |
| if (belongs_to_rect(p, r)) { | |
| result++; | |
| } | |
| } | |
| out << result << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment