POJ P2318 TOYS与POJ P1269 Intersecting Lines——计算几何入门题两道
rt,计算几何入门;
TOYS
Calculate the number of toys that land in each bin of a partitioned toy box.
Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys.
John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box.
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.
--by POJ
http://poj.org/problem?id=2318
由于数据范围小,M*N的暴力枚举即可,
对于一个点,使其与直线的一点构成向量,与直线方向向量作叉积判断位置即可;
代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const double eps=1e-;
int dcmp(double x){
if(fabs(x)<eps)return ;
return x<?-:;
}
struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y){ };
};
typedef Point Vector;
struct line{
Point s,t;
}lin[];
Vector operator - (Point A,Point B){
return Vector(A.x-B.x,A.y-B.y);
}
double Cross(Vector A,Vector B){
return A.x*B.y-A.y*B.x;
}
int ton[];
int main()
{
int i,j,k,p;
int n,m;
Point lu,rd,toy;
while(scanf("%d%d",&n,&m)==&&n!=){
memset(ton,,sizeof(ton));
scanf("%lf%lf%lf%lf",&lu.x,&lu.y,&rd.x,&rd.y);
for(i=;i<=n;i++){
scanf("%lf%lf",&lin[i].s.x,&lin[i].t.x);
lin[i].s.y=lu.y;lin[i].t.y=rd.y;
}
for(i=;i<=m;i++){
scanf("%lf%lf",&toy.x,&toy.y);
p=;
for(j=;j<=n;j++)
if(Cross(toy-lin[j].s,lin[j].t-lin[j].s)>){
ton[j-]++;p=;
break;
}
if(!p)
ton[n]++;
}
for(i=;i<=n;i++)
printf("%d: %d\n",i,ton[i]);
printf("\n");
}
}
Intersecting Lines
We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.
--by POJ
http://poj.org/problem?id=1269
判断两直线的位置关系并判断交点;
位置关系:
平行:向量叉积为零,且两直线上点间连向量与直线的向量叉积非零;
重合:向量叉积为零,且不平行;
相交:叉积非零;
求交点:设为(x,y),
交点与直线a的点构成向量与向量a叉积零,
与直线b的点构成向量与向量b叉积零,
列二元方程组,解之即得;
当然没这么麻烦!!!
因为其实有个结论:
设直线分别为P+tV和Q+tW且设向量u=P-Q,设交点在直线1的参数为t1,交点在直线2的参数为t2
t1=cross(w,u)/cross(v,w)
t2=cross(v,u)/cross(v,w)
然而并不知道,花了好久解了下方程
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const double eps=1e-;
int dcmp(double x){
if(fabs(x)<eps)return ;
return x>?:-;
}
struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y){ };
};
typedef Point Vector;
struct line{
Point s,t;
};
Vector operator - (Vector A,Vector B){
return Vector(A.x-B.x,A.y-B.y);
}
double Cross(Vector A,Vector B){
return A.x*B.y-A.y*B.x;
}
int main()
{
int i,j,k,n;
line lin1,lin2;
Point Poi;
Vector Ve1,Ve2;
double k1,k2,X,Y;
scanf("%d",&n);
printf("INTERSECTING LINES OUTPUT\n");
for(i=;i<=n;i++){
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&lin1.s.x,&lin1.s.y,&lin1.t.x,&lin1.t.y,&lin2.s.x,&lin2.s.y,&lin2.t.x,&lin2.t.y);
if(!dcmp(Cross(lin1.t-lin1.s,lin2.t-lin2.s))){
if(!dcmp(Cross(lin1.t-lin1.s,lin1.t-lin2.s)))
printf("LINE\n");
else
printf("NONE\n");
}
else{
Ve1=lin1.t-lin1.s;Ve2=lin2.t-lin2.s;
X=(Ve1.x*Ve2.x*(lin2.t.y-lin1.t.y)-lin2.t.x*Ve1.x*Ve2.y+lin1.t.x*Ve1.y*Ve2.x)/(Ve1.y*Ve2.x-Ve2.y*Ve1.x);
if(dcmp(Ve1.x)!=)
Y=lin1.t.y-(lin1.t.x-X)*Ve1.y/Ve1.x;
else
Y=lin2.t.y-(lin2.t.x-X)*Ve2.y/Ve2.x;
printf("POINT %.2lf %.2lf\n",X,Y);
}
}
printf("END OF OUTPUT\n");
}
祝AC
POJ P2318 TOYS与POJ P1269 Intersecting Lines——计算几何入门题两道的更多相关文章
- POJ 1269 - Intersecting Lines - [平面几何模板题]
题目链接:http://poj.org/problem?id=1269 Time Limit: 1000MS Memory Limit: 10000K Description We all know ...
- POJ 1269 Intersecting Lines --计算几何
题意: 二维平面,给两条线段,判断形成的直线是否重合,或是相交于一点,或是不相交. 解法: 简单几何. 重合: 叉积为0,且一条线段的一个端点到另一条直线的距离为0 不相交: 不满足重合的情况下叉积为 ...
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- poj 2318 TOYS & poj 2398 Toy Storage (叉积)
链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段.而且这些线段坐标是依照顺序给出的. 有n条线段,把盒子分层了n+1个区域,然后有m个玩具.这m个玩具的坐标是已知的,问最后每一个区域 ...
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 2823 UESTCoj 1221 Sliding Window 单调队列 经典入门题
题意:给出一个序列,求出每连续k个数字中最大的数和最小的数. 这是道单调队列裸题,直接写就行了. 本来用deque写出来后,发现在poj上硬是超时了,在discuss上看很多人也在抱怨超时的问题,据说 ...
- 任务调度分配题两道 POJ 1973 POJ 1180(斜率优化复习)
POJ 1973 这道题以前做过的.今儿重做一次.由于每个程序员要么做A,要么做B,可以联想到0/1背包(谢谢N巨).这样,可以设状态 dp[i][j]为i个程序员做j个A项目同时,最多可做多少个B项 ...
- poj 2926:Requirements(最远曼哈顿距离,入门题)
Requirements Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3908 Accepted: 1318 Desc ...
- POJ 3903 Stock Exchange 最长上升子序列入门题
题目链接:http://poj.org/problem?id=3903 最长上升子序列入门题. 算法时间复杂度 O(n*logn) . 代码: #include <iostream> #i ...
随机推荐
- (二)Python 装饰器
1. 函数 在 Python 中,使用关键字 def 和一个函数名以及一个可选的参数列表来定义函数.函数使用 return 关键字来返回值.定义和使用一个最简单的函数例子: >>> ...
- axios跨域问题
最近遇到一个很奇怪的问题,在帮助测试妹子做一个小项目的时候,遇到了一个很棘手的问题,axios请求的时候报404,请求type是options,我当时的第一反应就是跨域问题,果然在console里面还 ...
- 在Eclipse之中调试FastDFS-storage
FDFS版本为5.03 1.首先在eclipse之中创建一个C/C++工程,取名为FastDFS_v5.03 2.将FastDFS源码解压后拷贝到新创建的工程目录下,然后在ecipse之中刷新下工程就 ...
- docker版redmine安装部署
数据库准备 docker run -d --name some-postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=redmine postgr ...
- 安装配置python、beautifulsoup4、pip的心酸总结
1.python下载安装不纠结,但如果要加入到eclipse里面就要注意一下版本,版本不匹配会造成,要不python降级,要不eclipse升级的情况 2.在稍新版本的python立面就附带下载在了p ...
- (C/C++) FILE 讀寫檔案操作
在C/C++ 讀寫檔案操作比較常見應該是利用 FILE.ifstream.ofstream 在這篇筆記裡頭記錄 FILE.fstream 使用方法及操作 #include <iostream&g ...
- RestTemplate--解决中文乱码
[原文链接]:https://www.tecchen.xyz/rest-template-messycode.html 我的个人博客:https://www.tecchen.xyz 在开发扇贝-每日一 ...
- Vue局部注册 或者全局注册 组件时,组件定义要用 分隔命名,用驼峰命名是不生效的
Vue.component('all-canuse',{ props:['message'], template:'<div>{{message}}</div>' }) 像这样
- [转] Actor生命周期理解
[转] https://blog.csdn.net/wsscy2004/article/details/38875065 镇图:Actor内功心法图 Actor的生命周期可以用Hooks体现和控制,下 ...
- js实现瀑布流布局
window.onload = function () { var d1 = new Waterfall(); d1.init();};//构造函数function Waterfall() { thi ...