nyoj 68-三点顺序(叉积)

68-三点顺序


内存限制:64MB 时间限制:1000ms 特判: No
通过数:3 提交数:5 难度:3

题目描述:

现在给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,现在让你判断A,B,C是顺时针给出的还是逆时针给出的?

如:

图1:顺时针给出

图2:逆时针给出 

 

        <图1>                   <图2>

输入描述:

每行是一组测试数据,有6个整数x1,y1,x2,y2,x3,y3分别表示A,B,C三个点的横纵坐标。(坐标值都在0到10000之间)
输入0 0 0 0 0 0表示输入结束
测试数据不超过10000组

输出描述:

如果这三个点是顺时针给出的,请输出1,逆时针给出则输出0

样例输入:

0 0 1 1 1 3
0 1 1 0 0 0
0 0 0 0 0 0

样例输出:

0
1

分析:
  1、叉积 --> (BA(x)*CA(y) - BA(y)*CA(x)),用于判断组成三点的顺序
  2、叉积 > 0 ==> 逆时针, 叉积 < 0 ==> 顺时针

核心代码:
1 double cross_product(node a, node b, node c)
2 {
3     return ((b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x));
4 }

C/C++代码实习(AC):

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <stack>
 7 #include <map>
 8 #include <queue>
 9 #include <set>
10 
11 using namespace std;
12 struct node
13 {
14     double x, y;
15 };
16 
17 double cross_product(node a, node b, node c)
18 {
19     return ((b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x));
20 }
21 
22 int main()
23 {
24     node a, b, c;
25     while(scanf("%lf%lf%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y), a.x || a.y || b.x || b.y || c.x || c.y)
26     {
27         if(cross_product(a, b, c) < 0) // 小于零为顺时针
28             printf("1
");
29         else
30             printf("0
");
31     }
32     return 0;
33 }
原文地址:https://www.cnblogs.com/GetcharZp/p/9111069.html