Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- using namespace std;
- double vectorProd(double x1,double y1,double x2,double y2){
- return x1*y2 - x2*y1;
- }
- bool signsIsDifferent(double a,double b){
- return a <= 0 && b >= 0 || a >= 0 && b <= 0;
- }
- struct Point{
- double x = .0,y = .0;
- double distanceTo(Point point){
- return sqrt(sqrt((point.x - x)*(point.x - x) + (point.y - y)*(point.y - y)));
- }
- bool operator == (const Point &point) const {
- return this->x == point.x && this->y == point.y;
- }
- };
- struct Line{
- Point l,r;
- bool isParallelTo(Line line){
- double x1 = r.x - l.x;
- double y1 = r.y - l.y;
- double x2 = line.r.x - line.l.x;
- double y2 = line.r.y - line.l.y;
- return (abs(vectorProd(x1,y1,x2,y2)) < 1e-8);
- }
- bool isCrossesTo(Line line){
- Point l1 = line.l;
- Point r1 = line.r;
- double v1 = vectorProd(l1.x - l.x,l1.y - l.y,r.x - l.x,r.y - l.y);
- double v2 = vectorProd(r1.x - l.x,r1.y - l.y,r.x - l.x,r.y - l.y);
- double v3 = vectorProd(r.x - r1.x,r.y - r1.y,l1.x - r1.x, l1.y - r1.y);
- double v4 = vectorProd(l.x - r1.x,l.y - r1.y,l1.x - r1.x, l1.y - r1.y);
- int c = (v1 == 0) + (v2 == 0) + (v3 == 0) + (v4 == 0);
- if(l1 == l || r1 == l || l1 == r || r1 == r){
- return 1;
- }
- if(c == 4){
- if(l.distanceTo(r1) + r1.distanceTo(r) == l.distanceTo(r) || l1.distanceTo(r) + r.distanceTo(r1) == l1.distanceTo(r1))return 1;
- }
- return signsIsDifferent(v1,v2) && signsIsDifferent(v3,v4);
- }
- };
- int howAreTheLines(Line l1,Line l2){
- if(l1.isParallelTo(l2) && l1.isCrossesTo(l2)) return 2;
- if(l1.isParallelTo(l2)) return 1;
- if(l1.isCrossesTo(l2)) return -1;
- return 0;
- }
- int main(){
- int n;
- cin >> n;
- Line * arr = new Line[n];
- for(int i = 0;i < n;++i){
- cout << "Введите координаты начала и конца отрезка\n";
- Point p1,p2;
- cin >> p1.x >> p1.y >> p2.x >> p2.y;
- Line line;
- line.l = p1;
- line.r = p2;
- arr[i] = line;
- }
- for(int i = 0;i < n;++i){
- for(int j = i+1;j < n;++j){
- int ans = howAreTheLines(arr[i],arr[j]);
- if(ans == 2) cout << "Отрезки " << i+1 << " и " << j+1 << " пересекаются и паралельны\n";
- else if(ans == -1)cout << "Отрезки " << i+1 << " и " << j+1 << " пересекаются\n";
- else if(ans == 1) cout << "Отрезки " << i+1 << " и " << j+1 << " параллельны\n";
- else cout << "Отрезки " << i+1 << " и " << j+1 << " ни пересекаются, ни параллельны\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement