zoukankan      html  css  js  c++  java
  • Inside Triangle

     Inside Triangle

    https://hihocoder.com/contest/hiho225/problem/1

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    Determine if a point is inside a 2D triangle.

    输入

    The first line contains an integer T denoting the number of test case. (1 <= T <= 10)

    The following T lines each contain 8 integers, Px, Py, Ax, Ay, Bx, By, Cx, Cy. P denotes the point. ABC denotes the triangle. The coordinates are within [-1000000, 1000000].

    输出

    For each test case output YES or NO denoting if the point is inside the triangle. Note that the point is considered inside the triangle if the point is just on some side of the triangle.

    样例输入
    2  
    0 1 -1 0 1 0 0 2  
    0 0 0 1 1 0 1 1
    样例输出
    YES  
    NO

    用叉积判断是否同正负即可
     1 #include<iostream>
     2 #include<cstring>
     3 #include<string>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<queue>
     7 #include<vector>
     8 #include<cstdio>
     9 using namespace std;
    10 
    11 struct Point{
    12     long long x,y;
    13 };
    14 
    15 long long Cross(Point P,Point A,Point B){
    16     return (A.x-P.x)*(B.y-P.y)-(A.y-P.y)*(B.x-P.x);
    17 }
    18 
    19 
    20 int main(){
    21     int T;
    22     scanf("%d",&T);
    23     Point P,A,B,C;
    24     while(T--){
    25         scanf("%lld %lld %lld %lld %lld %lld %lld %lld",&P.x,&P.y,&A.x,&A.y,&B.x,&B.y,&C.x,&C.y);
    26         long long ans1=Cross(A,B,P);
    27         long long ans2=Cross(B,C,P);
    28         long long ans3=Cross(C,A,P);
    29         if((ans1<=0&&ans2<=0&&ans3<=0)||(ans1>=0&&ans2>=0&ans3>=0)){
    30             puts("YES");
    31         }
    32         else{
    33             puts("NO");
    34         }
    35     }
    36 
    37 }
    View Code
  • 相关阅读:
    寻找两个有序数组的中位数
    JAVA设计模式(组合模式)
    excel 操作
    研究生英语-春
    cvs
    Spring课程安排
    Spring的事务管理
    在WEB项目中集成Spring
    计算机网络参考模型
    揭开5G神秘面纱
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9856389.html
Copyright © 2011-2022 走看看