1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
#include <cmath> #include <iostream> #include <stdio.h> #define INF 1000000000 using namespace std; int n; int c=1; double m1,m2,d,d1,d2,x1,x2,x3,x4,y_1,y2,y3,y4,c1,c2; double px,py; void intersect() { if(x1!=x2) { m1=((y_1-y2)/(x1-x2)); } else { m1=INF; } if(x3!=x4) m2=(y3-y4)/(x3-x4); else m2=INF; if(m1==INF) c1=0; else c1=y_1-m1*x1; if(m2==INF) c2=0; else c2=y3-m2*x3; if(m1==INF) { px=x1; if(m2==0) { py=y3; printf("POINT %.2lf %.2lf\n",px,py); return; } else { py=m2*px+c2; printf("POINT %.2lf %.2lf\n",px,py); return; } } if(m2==INF) { px=x3; if(m1==0) { py=y_1; printf("POINT %.2lf %.2lf\n",px,py); return; } else { py=m1*px+c1; printf("POINT %.2lf %.2lf\n",px,py); return; } } if(m1==0) { py=y_1; if(m2==INF) { px=x3; printf("POINT %.2lf %.2lf\n",px,py); return; } else { px=(py-c2)/m2; printf("POINT %.2lf %.2lf\n",px,py); return; } } if(m2==0) { py=y3; if(m1==INF) { px=x1; printf("POINT %.2lf %.2lf\n",px,py); return; } else { px=(py-c1)/m1; printf("POINT %.2lf %.2lf\n",px,py); return; } } if(m1!=m2) { px=-(c1-c2)/(m1-m2); py=m1*px+c1; printf("POINT %.2lf %.2lf\n",px,py); } } int main() { cin>>n; cout<<"INTERSECTING LINES OUTPUT\n"; while(n--) { cin>>x1>>y_1>>x2>>y2>>x3>>y3>>x4>>y4; /* comparing the slopes of the st. line x1y1-x2y2, x3y3-x4y4, st. line x1y1-x3y3, x2y2-x4y4, st. line x1y1-x4y4, x2y2-x3y3, */ d=(y_1-y2)*(x3-x4)-(y3-y4)*(x1-x2); d1=(y_1-y3)*(x2-x4)-(y2-y4)*(x1-x3); d2=(y_1-y4)*(x2-x3)-(y2-y3)*(x1-x4); if(d==0 && d1==0 && d2==0) { cout<<"LINE\n"; continue; } else if(d==0) { cout<<"NONE\n"; continue; } else { intersect(); } } cout<<"END OF OUTPUT\n"; return 0; } |
Another approach:
(but gives wa)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include<stdio.h> int main() { printf("INTERSECTING LINES OUTPUT\n"); int n,x_1,x_2,x_3,x_4,y_1,y_2,y_3,y_4; double d,d1,d2,t1,t2,x,y; scanf("%d",&n); while(n>0) { scanf("%d%d%d%d%d%d%d%d",&x_1,&y_1,&x_2,&y_2,&x_3,&y_3,&x_4,&y_4); d=(double)((x_1-x_2)*(y_3-y_4))-((y_1-y_2)*(x_3-x_4)); d1=(double)((x_3-x_4)*(y_3-y_1))-((y_3-y_4)*(x_3-x_1)); d2=(double)(-((x_3-x_1)*(y_1-y_2))+((y_3-y_1)*(x_1-x_2))); if((int)d==0 && (int)d1==0 && (int)d2==0) { printf("LINE\n"); } else if((int)d==0) { printf("NONE\n"); } else { t1=d1/d; t2=d2/d; x=x_1+t1*(x_2-x_1); y=y_1+t1*(y_2-y_1); printf("POINT %.2lf %.2lf\n",x,y); } n--; } printf("END OF OUTPUT\n"); return 0; } |