Skip to content

Instantly share code, notes, and snippets.

@waff
Created April 17, 2013 03:54
Show Gist options
  • Save waff/5401679 to your computer and use it in GitHub Desktop.
Save waff/5401679 to your computer and use it in GitHub Desktop.
POJ1269 考虑两直线 平行 or 共线 ?是否垂直于x轴?即sameline过程,非常关键 再叉积求交点即可 吐槽"%.2lf" 过不了,得用"%.2f"
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <utility>
#include <cstdio>
#include <string>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
const double eps = 1e-6;
typedef long long int64;
struct point
{
double x, y;
};
typedef point line;
int n;
point a[5];
int cmp_d(double x)
{
if (std::abs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
line operator - (point a, point b) { return (line) { a.x - b.x, a.y - b.y }; }
line operator + (point a, point b) { return (line) { a.x + b.x, a.y + b.y }; }
line operator * (point a, double lmd) { return (line) { a.x * lmd, a.y * lmd }; }
double cross(line a, line b) { return a.x * b.y - a.y * b.x; }
int sameline()
{
if (cmp_d(cross(a[1] - a[2], a[3] - a[4])) == 0)
{
if (cmp_d(a[1].x - a[2].x) == 0 && cmp_d(a[3].x - a[4].x) == 0)
{
if (cmp_d(a[1].x - a[3].x) == 0) return 1;
else return -1;
}
double k1 = (a[1].y - a[2].y) / (a[1].x - a[2].x);
double b1 = a[1].y - k1 * a[1].x;
if (cmp_d(a[3].x * k1 + b1 - a[3].y) == 0) return 1;
else return -1;
}
return 0;
}
void intersection(point a, line v, point b, line w)
{
line u = a - b;
double t = cross(w, u) / cross(v, w);
line aim = a + v * t;
printf("POINT %.2f %.2f\n", aim.x, aim.y);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1269.in", "r", stdin);
freopen("1269.out", "w", stdout);
#endif
puts("INTERSECTING LINES OUTPUT");
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
int b;
for (int j = 1; j <= 4; ++j) scanf("%lf%lf", &a[j].x, &a[j].y);
if ((b = sameline()) == 1) puts("LINE");
else if (b == -1) puts("NONE");
else intersection(a[1], a[1] - a[2], a[3], a[3] - a[4]);
}
puts("END OF OUTPUT");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment