zoukankan      html  css  js  c++  java
  • Uva 10112 【简单的计算几何】.cpp

    题意:

      给出n个点的x坐标和y坐标,让你求出由其中三个点组成的三角形,要求这个三角形里面不包含别的点且面积最小。

    思路:

      三角形里面不包含别的点的判断方法是最暴力的一层for循环用面积法看看除去组成这个三角形以外的别的点是否在这个三角形内。

    Tips:

      这题的关键其实只是掌握面积法。即利用差积的方法求面积。

    Code:

    View Code
     1 #include <stdio.h>
     2 #include <cstring>
     3 #include <cmath>
     4 #include <algorithm>
     5 #include <ctime>
     6 using namespace std;
     7 #define eps 1e-8
     8 
     9 struct Point
    10 {
    11     char num;
    12     double x;
    13     double y;
    14 }po[20];
    15 
    16 double xmul(Point a, Point b, Point c) {
    17     return (c.y-a.y)*(b.x-a.x)-(b.y-a.y)*(c.x-a.x);
    18 }
    19 
    20 double area(Point a, Point b, Point c)
    21 {
    22     return fabs(0.5*xmul(a, b, c));
    23 }
    24 
    25 bool isin(Point a, Point b, Point c, Point p) {
    26     return fabs(area(a, b, p)+area(a, c, p)+area(b, c, p)-area(a, b, c)) < eps;
    27 }
    28 
    29 int main()
    30 {
    31     Point a, b, c;
    32     bool flag;
    33     int n;
    34     double tar;
    35     while(EOF != scanf("%d", &n)) {
    36         if(n == 0) break;
    37         tar = -1;
    38         for(int i = 0; i < n; ++i) {
    39             scanf("%*c%c %lf %lf", &po[i].num, &po[i].x, &po[i].y);
    40          //   printf("%c %lf %lf\n", po[i].num, po[i].x, po[i].y);
    41         }
    42         for(int i = 0; i < n; ++i)
    43             for(int j = i+1; j < n; ++j)
    44                 for(int k = j+1; k < n; ++k) {
    45                     flag = true;
    46                     for(int t = 0; t < n; ++t)
    47                         if(t != i && t != j && t != k && isin(po[i], po[j], po[k], po[t])) {
    48                             flag = false;
    49                             break;
    50                         }
    51                     if(flag && fabs(area(po[i], po[j], po[k]) > tar+eps)) {
    52                         a = po[i], b = po[j], c = po[k];
    53                         tar = area(po[i], po[j], po[k]);
    54                     }
    55                 }
    56         printf("%c%c%c\n", a.num, b.num, c.num);
    57     }
    58     return 0;
    59 }

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=101&page=show_problem&problem=1053

  • 相关阅读:
    网格形变
    网格简化
    无法打开包括文件: “QWidget”: No such file or directory
    遇到一个 bug svg 抖动的解决方案
    echarts-gl 遇到一个错误 groupGL 未定义
    鼠标操控三维视角
    鼠标控制3维操作 不知道能不能获得一些灵感
    tensorflow 安装
    Codeforces Round #541 (Div. 2) B.Draw!
    Codeforces Round #541 (Div. 2) A.Sea Battle
  • 原文地址:https://www.cnblogs.com/Griselda/p/2963855.html
Copyright © 2011-2022 走看看