- #include <stdio.h>
-
#include <math.h>
-
-
struct _point {
-
double x;
-
double y;
-
};
-
typedef struct _point point;
-
-
struct _triangle {
-
point a;
-
point b;
-
point c;
-
};
-
typedef struct _triangle triangle;
-
-
int check(triangle t, point p);
-
-
int
-
main(void)
-
{
-
triangle t;
-
t.a.x = 1;
-
t.a.y = 1;
-
t.b.x = 3;
-
t.b.y = 1;
-
t.c.x = 3;
-
t.c.y = 2;
-
-
point p;
-
p.x = 2;
-
p.y = 2;
-
-
point q;
-
q.x = 3;
-
q.y = 3;
-
-
point r;
-
r.x = 2;
-
r.y = 1;
-
-
printf("Res: %d\n", check(t, p));
-
printf("Res: %d\n", check(t, q));
-
printf("Res: %d\n", check(t, r));
-
-
return 0;
-
}
-
-
double
-
distance(point p1, point p2)
-
{
-
double res;
-
double x = p1.x - p2.x;
-
double y = p1.y - p2.y;
-
res = sqrt(x*x + y*y);
-
-
return res;
-
}
-
-
double
-
area(point p1, point p2, point p3)
-
{
-
double res;
-
double a = distance(p1, p2);
-
double b = distance(p2, p3);
-
double c = distance(p3, p1);
-
double p = (a + b + c) / 2;
-
res = sqrt(p*(p-a)*(p-b)*(p-c));
-
-
return res;
-
}
-
-
int
-
check(triangle t, point p)
-
{
-
double res;
-
double base = area(t.a, t.b, t.c);
-
double x = area(t.a, t.b, p);
-
double y = area(t.b, t.c, p);
-
double z = area(t.c, t.a, p);
-
-
if (base <= (x+y+z))
-
if (0 != x && 0 != y && 0 != z)
-
return 1;
-
else
-
return 0;
-
else
-
return (-1);
- }
- 1: 内
- 0: 边
- -1:外