Chinaunix首页 | 论坛 | 博客
  • 博客访问: 368956
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-05-18 08:55:39

  1. #include <stdio.h>
  2. #include <math.h>

  3. struct _point {
  4.     double x;
  5.     double y;
  6. };
  7. typedef struct _point point;

  8. struct _triangle {
  9.     point a;
  10.     point b;
  11.     point c;
  12. };
  13. typedef struct _triangle triangle;

  14. int check(triangle t, point p);

  15. int
  16. main(void)
  17. {
  18.     triangle t;
  19.     t.a.x = 1;
  20.     t.a.y = 1;
  21.     t.b.x = 3;
  22.     t.b.y = 1;
  23.     t.c.x = 3;
  24.     t.c.y = 2;

  25.     point p;
  26.     p.x = 2;
  27.     p.y = 2;

  28.     point q;
  29.     q.x = 3;
  30.     q.y = 3;

  31.     point r;
  32.     r.x = 2;
  33.     r.y = 1;

  34.     printf("Res: %d\n", check(t, p));
  35.     printf("Res: %d\n", check(t, q));
  36.     printf("Res: %d\n", check(t, r));

  37.     return 0;
  38. }

  39. double
  40. distance(point p1, point p2)
  41. {
  42.     double res;
  43.     double x = p1.x - p2.x;
  44.     double y = p1.y - p2.y;
  45.     res = sqrt(x*x + y*y);

  46.     return res;
  47. }

  48. double
  49. area(point p1, point p2, point p3)
  50. {
  51.     double res;
  52.     double a = distance(p1, p2);
  53.     double b = distance(p2, p3);
  54.     double c = distance(p3, p1);
  55.     double p = (a + b + c) / 2;
  56.     res = sqrt(p*(p-a)*(p-b)*(p-c));
  57.     
  58.     return res;
  59. }

  60. int
  61. check(triangle t, point p)
  62. {
  63.     double res;
  64.     double base = area(t.a, t.b, t.c);
  65.     double x = area(t.a, t.b, p);
  66.     double y = area(t.b, t.c, p);
  67.     double z = area(t.c, t.a, p);

  68.     if (base <= (x+y+z))
  69.         if (0 != x && 0 != y && 0 != z)
  70.             return 1;
  71.         else
  72.             return 0;
  73.     else
  74.         return (-1);
  75. }

  • 1: 内
  • 0: 边
  • -1:外
阅读(1427) | 评论(0) | 转发(0) |
0

上一篇:单实例模式1

下一篇:字符串连接返回串尾

给主人留下些什么吧!~~