UVA - 143 Orchard Trees (点在三角形内)


题意:
给出三角形的三个点的坐标(浮点数),
问落在三角形内及三角形边上的整点有多少?
思路:所有点暴力判断(点的范围1-99,三角形可能是0-100,因为这个WA了一下orz)
AC代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const double eps = 1e-;
const double pi = acos(-1.0);
const int maxp = ;
int sgn(double x)
{
if(fabs(x) < eps) return ;
else return x < ? - : ;
}
struct Point{
double x, y;
Point(){}
Point(double _x, double _y){
x = _x, y = _y;
}
void input(){
scanf("%lf%lf", &x, &y);
}
bool operator == (Point b) const{
return sgn(x - b.x) == && sgn(y - b.y) == ;
}
bool operator < (Point b)const{
return sgn(x - b.x) == ? sgn(y - b.y < ) : x < b.x;
}
Point operator - (const Point &b)const{
return Point(x - b.x, y - b.y);
}
//²æ»ý
double operator ^(const Point &b){
return x * b.y - y * b.x;
}
//µã»ý
double operator *(const Point &b){
return x * b.x + y * b.y;
}
double len(){
return hypot(x, y);
}
double len2(){
return x * x + y * y;
}
double distant(Point p){
return hypot(x - p.x, y - p.y);
}
Point operator + (const Point &b)const{
return Point (x + b.x, y + b.y);
}
Point operator * (const double &k)const{
return Point(x * k, y * k);
}
Point operator / (const double &k)const{
return Point(x / k, y / k);
}
};
struct Line{
Point s, e;
Line(){}
Line(Point _s, Point _e){s = _s, e = _e;}
bool operator == (Line v){
return (s == v.s) && (e == v.e);
}
bool pointonseg(Point p){
return sgn((p - s)^(e - s)) == && sgn((p - e)*(p - s)) <= ;
} };
struct polygon{
int n;
Point p[maxp];
Line l[maxp];
void add(Point q){
p[n ++] = q;
}
void input(int _n){
n = _n;
for(int i = ;i < n;i++) p[i].input();
}
void getline(){
for(int i = ;i < n;i++){
l[i] = Line(p[i], p[(i+) % n]);
}
}
int relationpoint(Point q){
for(int i = ;i < n;i++){
if(p[i] == q) return ;
}
getline();
for(int i = ;i < n;i++){
if(l[i].pointonseg(q)) return ;
}
int cnt = ;
for(int i = ;i < n;i++){
int j = (i + ) % n;
int k = sgn((q - p[j])^(p[i] - p[j]));
int u = sgn(p[i].y - q.y);
int v = sgn(p[j].y - q.y);
if(k > && u < && v >= ) cnt++;
if(k < && v < && u >= ) cnt--;
}
return cnt != ;
}
};
int main()
{
double x1, x2, x3, y1, y2, y3;
polygon a;
while(~scanf("%lf%lf%lf%lf%lf%lf",&x1, &y1, &x2, &y2, &x3, &y3) && (x1|| x2|| x3|| y1|| y2|| y3))
{
a.n = ;
a.add(Point(x1,y1));
a.add(Point(x2,y2));
a.add(Point(x3,y3));
int cnt = ;
for(double i = ;i <= ;i++)
for(double j = ;j <= ;j++)
if(a.relationpoint(Point(i,j))) cnt ++;
printf("%4d\n",cnt);
}
return ;
}
UVA - 143 Orchard Trees (点在三角形内)的更多相关文章
- UVa 143 - Orchard Trees
题目大意:果园里的树排列成矩阵,它们的x和y坐标均是1~99的整数.输入若干三角形,依次统计每一个三角形内部和边界上共有多少棵树. 三角形P0P1P2有向面积为A:2A = x0y1 + x2y0 + ...
- hud 1633 Orchard Trees 点是否在三角形内模板 *
Orchard Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 2D空间中判断一点是否在三角形内
要注意如果是XY坐标轴的2D空间,要取差乘分量z而不是y. 实现原理是,将三角形ABC三个边(AB,BC,CA)分别与比较点判断差乘,如果这3个差乘结果表示的方向一致,说明就在三角形内. 效果: 代码 ...
- hrbustoj 1142:围困(计算几何基础题,判断点是否在三角形内)
围困 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 360(138 users) Total Accepted: 157(12 ...
- 【Leetcode】判断平面中1个点是否落在三角形内
参考资料: 题目: https://blog.csdn.net/dongtinghong/article/details/78657403 符号重载: https://blog.csdn.net/cd ...
- 【TOJ 3005】Triangle(判断点是否在三角形内+卡精度)
描述 Given the coordinates of the vertices of a triangle,And a point. You just need to judge whether t ...
- UVA 11186 Circum Triangle (枚举三角形优化)(转)
题意:圆上有n个点,求出这n个点组成的所有三角形的面积之和 题解: 当我们要求出S(i,j,k)时,我们需要假设k在j的左侧,k在i与j之间,k在i的右侧. 如果k在 j的左侧 那么 S(i,j,k ...
- hdu 4353 统计点在三角形内的个数
Finding Mine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- poj 2954 Triangle 三角形内的整点数
poj 2954 Triangle 题意 给出一个三角形的三个点,问三角形内部有多少个整点. 解法 pick's law 一个多边形如果每个顶点都由整点构成,该多边形的面积为\(S\),该多边形边上的 ...
随机推荐
- error LNK2005: __get_invalid_parameter_handler 已经在 LIBCMTD.lib(invarg.obj) 中定义
转自VC错误http://www.vcerror.com/?p=166 问题描述: 在用vs2012编译程序的时候,release版本正常编译通过,但Debug版本提示下面的很多错误; 1>LI ...
- js匿名函数测试
js匿名函数测试 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- 深入解读 Redis 的持久化
Redis持久化 Java大猿帅成长手册,GitHub JavaEgg ,N线互联网开发必备技能兵器谱 Redis 的数据全部在内存里,如果突然宕机,数据就会全部丢失,因此必须有一种机制来保证 Red ...
- C. Ancient Berland Circus(三点确定最小多边形)
题目链接:https://codeforces.com/problemset/problem/1/C 题意:对于一个正多边形,只给出了其中三点的坐标,求这个多边形可能的最小面积,给出的三个点一定能够组 ...
- Linux(二)—— Linux配置及指令
目录 Linux配置及指令 一.linux中常用软件的安装 二.主机名和网络 1.修改主机名 2.设置网络 三.关闭防火墙 1.检查防火墙是否开启 2.清除策略 3.永久关闭第一个防火墙 4.关闭第二 ...
- ELK日志分析平台.1-搭建
ELK日志分析平台.1-搭建 2017-12-28 | admin 一.简介1.核心组成 ELK由Elasticsearch.Logstash和Kibana三部分组件组成: Elastic ...
- 多线程实现奇偶统计v2 - 信号量实现
#include <stdio.h> #include <stdlib.h> #include <time.h> #include "pthread.h& ...
- fixture实战---通过fixure,解决方法依赖逻辑
import pytest@pytest.fixture()def login(): print('输入用户名密码登陆') def test_cart(login): print('用例1,登陆后执行 ...
- 使用vue.js开发小程序
写在前面 刚刚开源的mpvue引起了不少前端er们的注意,下图是一个简单的对比. 话不多说,我们现在感受一下如何使用mpvue开发小程序.(以下内容参照mpvue文档完成). 开发环境 node np ...
- Codeforces Round #568 (Div. 2) G2. Playlist for Polycarp (hard version)
因为不会打公式,随意就先将就一下? #include<cstdio> #include<algorithm> #include<iostream> #include ...