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\),该多边形边上的 ...
随机推荐
- java 实现一套流程管理、流转的思路(伪工作流) 【仅供参考】
转: java 实现一套流程管理.流转的思路(伪工作流) 在做某个管理项目时,被要求实现一套流程管理,比如请假的申请审批流程等,在参考了很多资料,并和同事讨论后,得到了一个自主实现的流程管理. 以下提 ...
- Java 空字符串和 字符串为null的区别
之前一直没有搞清楚 字符串为空和字符串为null的区别,今天写代码一直出现NullPointerException异常,我一直没有搞清楚,后来发现我是这样写的 String s = null; s = ...
- SecureCRT key登录linux ssh设置
一.首先用secureCrt创建密钥 1.使用SecureCRT创建私钥和公钥. SecureCRT quick Connect-> Authentiation -> Public Key ...
- Codeforces 388C Fox and Card Game (贪心博弈)
Codeforces Round #228 (Div. 1) 题目链接:C. Fox and Card Game Fox Ciel is playing a card game with her fr ...
- 【扩展lucas定理】
洛谷模板题面:https://www.luogu.org/problemnew/show/P4720 扩展卢卡斯被用于解决模数为合数情形下的组合数问题. 首先我们把模数mod质因数分解,解决模每个素数 ...
- Rust <6>:闭包
单线程环境: 从宿主环境中捕获的变量,是引用,会改变原有的值,与 golang 的闭包行为一样: 以参数形式传入的变量,默认会发生 move:而 golang 的闭包参数,是宿主环境的副本,相当于在 ...
- 以您熟悉的编程语言为例完成一个hello/hi的简单的网络聊天程序
Socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟机或不同计算机之间的通信,应用程序通常通过"套接字"向网络发出 ...
- CookieUtil.java
package util; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.n ...
- 推荐MarkDown编辑工具Typora--文本画流程图示例
程序员界名言:talk is cheap, show me the code CODE: ### 8. 修改预留手机号-per.MCReservedMobilePhoneUpd #### 8.1业务规 ...
- JS提示信息来检测相应id的标签
2015-07~2015-08 (其中$为document.getElementById()) 使用span提示信息来检测相应id的标签,没有返回值 infoTips("LRYH" ...