zoukankan      html  css  js  c++  java
  • 判断点是否在三角形内

    求面积根据叉乘:不懂可以先看一下我的博客里计算机几何,叉乘2016-11-1016:11:01

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cmath>
     4 using namespace std;
     5 
     6 struct point {
     7     double x, y;
     8 };
     9 
    10 struct v {
    11     point start, end;
    12 };
    13 
    14 
    15 double dotProduct(v v1, v  v2) {
    16     return (v1.end.x - v1.start.x)*(v2.end.x - v2.start.x) + (v1.end.y - v1.start.y)*(v2.end.y - v2.start.y);
    17 }
    18 double crossProduct(v v1, v v2) {
    19     return (v1.end.x - v1.start.x)*(v2.end.y - v2.start.y) - (v2.end.x - v2.start.y)*(v1.end.y - v1.start.y);
    20 }
    21 
    22 bool inTriangle(point a,point b,point c, point p) {
    23     v ab, ac, bc;
    24     ab.start = a, ab.end = b;
    25     ac.start = a, ac.end = c;
    26     bc.start = b, bc.end = c;
    27     double Sabc = fabs(crossProduct(ab, ac));
    28 
    29     v pb;
    30     pb.start = p, pb.end= b;
    31     double Sabp = fabs(crossProduct(ab, pb));
    32 
    33     v pc;
    34     pc.start = p, pc.end = c;
    35     double Sacp = fabs(crossProduct(ac, pc));
    36 
    37     double Sbcp = fabs(crossProduct(bc, pc));
    38 
    39     if (fabs(Sabc - Sabp - Sacp - Sbcp) < 1e-5)
    40         return true;
    41     return false;
    42 }
    43 
    44 
    45 int main() {
    46     point p1, p2, p3,p0;
    47     while (cin >> p1.x >> p1.y >> p2.x >> p2.y >> p3.x >> p3.y >> p0.x >> p0.y) {
    48         cout << inTriangle(p1, p2, p3, p0);
    49     }
    50 }
    自己选的路,跪着也要把它走完------ACM坑
  • 相关阅读:
    LeetCode 566 重塑矩阵
    LeetCode 283 移动零
    C++Template(类模板二)
    Qt之简单绘图实现
    QT控件之QSlider
    Redis
    布局总结三: icon图标+标题上下两排排列
    vue中在data中引入图片的路径方法
    布局总结二:宽高比固定比例---移动端
    在vue中使用vue-awesome-swiper插件
  • 原文地址:https://www.cnblogs.com/IKnowYou0/p/6051339.html
Copyright © 2011-2022 走看看