Skip to content

Instantly share code, notes, and snippets.

@tarawa
Created May 25, 2013 09:06
Show Gist options
  • Save tarawa/5648460 to your computer and use it in GitHub Desktop.
Save tarawa/5648460 to your computer and use it in GitHub Desktop.
ZOJ1280 (C++)
#include <stdio.h>
#include <algorithm>
using namespace std;
struct vec {
int x,y;
};
struct line {
vec st,ed;
};
line a[1];
bool isSameline(line p,line q) {
p.ed.x-=p.st.x;
p.ed.y-=p.st.y;
q.ed.x-=p.st.x;
q.ed.y-=p.st.y;
int cross=p.ed.x*q.ed.y-p.ed.y*q.ed.x;
return cross==0;
}
bool isParallel(line p,line q) {
vec m,n;
m.x=p.ed.x-p.st.x;
m.y=p.ed.y-p.st.y;
n.x=q.ed.x-q.st.x;
n.y=q.ed.y-q.st.y;
int cross=m.x*n.y-m.y*n.x;
return cross==0;
}
void print_cross_point(line p,line q) {
int a[1],b[1],c[1];
a[0]=p.st.y-p.ed.y;
b[0]=p.ed.x-p.st.x;
c[0]=p.st.x*p.ed.y-p.ed.x*p.st.y;
a[1]=q.st.y-q.ed.y;
b[1]=q.ed.x-q.st.x;
c[1]=q.st.x*q.ed.y-q.ed.x*q.st.y;
double delta1=a[0]*b[1]-a[1]*b[0];
double m=(b[0]*c[1]-c[0]*b[1])/delta1,n=(a[1]*c[0]-a[0]*c[1])/delta1;
printf("POINT %.2f %.2f\n",m,n);
return;
}
int main() {
int n;
scanf("%d\n",&n);
printf("INTERSECTING LINES OUTPUT\n");
for (int i=0; i<n; i++) {
scanf("%d%d%d%d",&a[0].st.x,&a[0].st.y,&a[0].ed.x,&a[0].ed.y);
scanf("%d%d%d%d",&a[1].st.x,&a[1].st.y,&a[1].ed.x,&a[1].ed.y);
if (isSameline(a[0],a[1])) {
printf("LINE\n");
}
else if (isParallel(a[0],a[1])) {
printf("NONE\n");
}
else print_cross_point(a[0],a[1]);
}
printf("END OF OUTPUT\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment