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 ...
随机推荐
- drools入门示例
Drools是一个基于java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效.随着互联网金融的兴 ...
- Laravel 的核心概念
工欲善其事,必先利其器.在开发Xblog的过程中,稍微领悟了一点Laravel的思想.确实如此,这篇文章读完你可能并不能从无到有写出一个博客,但知道Laravel的核心概念之后,当你再次写起Larav ...
- 架构师养成记--34.Redis持久化
---恢复内容开始--- redis是一个支持持久化的内存数据库,也就是搜redis需要经常将内存中的数据同步到硬盘来保证持久化.redis持久化有两种方式. snapshotting(快照)默认方式 ...
- vue-cli3预设preset记录
这两天公司搭建新项目的时候发现vue-cli3有一个神奇的的东西:preset(预设).preset其实是你在create新vue项目的时候,生成的插件配置项预设,也就是你在项目中需要用到的插件安装成 ...
- poj3207 Ikki's Story IV - Panda's Trick 2-SAT
题目传送门 题意:在一个圆上顺时针安放着n个点,给出m条线段连接端点,要求线段不相交,线段可以在圆内也可以在圆外,问是否可以. 思路:假设一条线段,放在圆外是A,放在园内是A',那么两条线段如果必须一 ...
- SVN无法读取cruuent修复方法
解决方法:在网上百度和google了一大圈之后,终于得知是断电时current和txn-current文件没有写入当前最新版本号和最新版本的路径问题 当时非常抓狂,项目刷新一直为空. 1.先把curr ...
- jQuery多库共存问题解决方法
一.问题概述: 1.随着jQuery的流行,采用jQuery和$符为命名空间的js库越来越多,当然jQuery的$符也是参照的Prototype库的,所以当多个库同时以$符或者jQuery为命名空间时 ...
- 【Maven学习】maven中依赖的配置详解
根元素project下的dependencies可以包含一个或者多个dependency元素,以声明一个或多个项目依赖.每个依赖可以包含的元素有: groupId,artifactId和version ...
- 029-FastDFSClient工具栏模板
模板一: package cn.e3mall.common.utils; import org.csource.common.NameValuePair; import org.csource.fas ...
- hibernate与ibatis的区别
Hibernate 是当前最流行的O/R mapping框架,当前版本是3.05.它出身于sf.net,现在已经成为Jboss的一部分了 iBATIS 是另外一种优秀的O/R mapping框架,当前 ...