Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <vector>
- #include <string>
- using namespace std;
- struct Point
- {
- float x;
- float y;
- };
- struct Line
- {
- Point p1;
- Point p2;
- };
- bool on_segment(Point p, Point q, Point r)
- {
- if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
- q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y))
- return true;
- return false;
- }
- int orientation(Point p, Point q, Point r)
- {
- int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
- if (val == 0) return 0;
- return (val > 0) ? 1 : 2;
- }
- bool do_intersect(Point p1, Point q1, Point p2, Point q2)
- {
- if ((p1.x == q2.x && p1.y == q2.y) || (q1.x == p2.x && q1.y == p2.y)) return false;
- int o1 = orientation(p1, q1, p2);
- int o2 = orientation(p1, q1, q2);
- int o3 = orientation(p2, q2, p1);
- int o4 = orientation(p2, q2, q1);
- if (o1 != o2 && o3 != o4) return true;
- if (o1 == 0 && on_segment(p1, p2, q1)) return true;
- if (o2 == 0 && on_segment(p1, q2, q1)) return true;
- if (o3 == 0 && on_segment(p2, p1, q2)) return true;
- if (o4 == 0 && on_segment(p2, q1, q2)) return true;
- return false;
- }
- bool do_lines_intersection(vector<Line> lines)
- {
- if (lines.size() < 3) return false;
- for (int i = 0; i < lines.size() - 1; i++) {
- if (do_intersect(lines[i].p1, lines[i].p2, lines[lines.size() - 1].p1, lines[lines.size() - 1].p2) == true) {
- return true;
- }
- }
- return false;
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- vector<Point> points;
- vector<Line> lines;
- Point point;
- Line line;
- int count = 0;
- bool stop = false;
- while (stop != true) {
- cout << "Координата X " << count + 1 << "-й точки\n>>> "; cin >> point.x;
- cout << "Координата Y " << count + 1 << "-й точки\n>>> "; cin >> point.y;
- if (count == 0) {
- line.p1 = point;
- }
- else if (count == 1 && line.p1.x == point.x && line.p1.y == point.y) {
- cout << "Неверные координаты. Вторая точка не может находится на одном месте с первой\n";
- continue;
- }
- else if (count > 0) {
- line.p2 = point;
- lines.push_back(line);
- if (do_lines_intersection(lines) == true) {
- lines.pop_back();
- cout << "Неверные координаты. Так линии пересекаются\n";
- continue;
- }
- line.p1 = point;
- }
- if (count > 2 && lines[0].p1.x == point.x && lines[0].p1.y == point.y) {
- line.p2 = point;
- lines.push_back(line);
- stop = true;
- }
- points.push_back(point);
- count++;
- }
- cout << "Полученные координаты: ";
- for (int i = 0; i < points.size() - 1; i++) {
- cout << "[" << points[i].x << "; " << points[i].y << "] ";
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement