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——计算几何入门题两道的更多相关文章

  1. POJ 1269 - Intersecting Lines - [平面几何模板题]

    题目链接:http://poj.org/problem?id=1269 Time Limit: 1000MS Memory Limit: 10000K Description We all know ...

  2. POJ 1269 Intersecting Lines --计算几何

    题意: 二维平面,给两条线段,判断形成的直线是否重合,或是相交于一点,或是不相交. 解法: 简单几何. 重合: 叉积为0,且一条线段的一个端点到另一条直线的距离为0 不相交: 不满足重合的情况下叉积为 ...

  3. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  4. poj 2318 TOYS &amp; poj 2398 Toy Storage (叉积)

    链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段.而且这些线段坐标是依照顺序给出的. 有n条线段,把盒子分层了n+1个区域,然后有m个玩具.这m个玩具的坐标是已知的,问最后每一个区域 ...

  5. POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】

    链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  6. POJ 2823 UESTCoj 1221 Sliding Window 单调队列 经典入门题

    题意:给出一个序列,求出每连续k个数字中最大的数和最小的数. 这是道单调队列裸题,直接写就行了. 本来用deque写出来后,发现在poj上硬是超时了,在discuss上看很多人也在抱怨超时的问题,据说 ...

  7. 任务调度分配题两道 POJ 1973 POJ 1180(斜率优化复习)

    POJ 1973 这道题以前做过的.今儿重做一次.由于每个程序员要么做A,要么做B,可以联想到0/1背包(谢谢N巨).这样,可以设状态 dp[i][j]为i个程序员做j个A项目同时,最多可做多少个B项 ...

  8. poj 2926:Requirements(最远曼哈顿距离,入门题)

    Requirements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3908   Accepted: 1318 Desc ...

  9. POJ 3903 Stock Exchange 最长上升子序列入门题

    题目链接:http://poj.org/problem?id=3903 最长上升子序列入门题. 算法时间复杂度 O(n*logn) . 代码: #include <iostream> #i ...

随机推荐

  1. Oracle ltrim() rtrim() 函数详细用法

    今天在论坛里看了一篇帖子,讨论ltrim() 函数的详细用法,下面我借几个高手的回答总结一下: 先看几个实例: SQL> select ltrim('109224323','109') from ...

  2. 嵌入式C语言自我修养 05:零长度数组

    5.1 什么是零长度数组 顾名思义,零长度数组就是长度为0的数组. ANSI C 标准规定:定义一个数组时,数组的长度必须是一个常数,即数组的长度在编译的时候是确定的.在ANSI C 中定义一个数组的 ...

  3. ES6之新增let命令使用方法

    let命令的用法 let是es6中的声明一个变量的命令,只在它声明的代码块中有效,出了这个代码块就会报错.也非常适合for循环,在循环中i的值只在循环语句中生效,在外边取不到的. var命令声明的是一 ...

  4. appium获取toast方法

    配置toast请注意: 1.指定desired_caps["automationName"] = "UiAutomator2" 2.要求安装jdk1.8 64位 ...

  5. 微信内置的浏览器window.location.href 跳转不兼容问题

    1.不兼容苹果手机---->>>>使用模拟触发a标签 <a id="alink" href="http://www.baidu.com&qu ...

  6. [转] 2018年最新桌面CPU性能排行天梯图(含至强处理器)

    [FROM] http://www.idn100.com/zuzhuangdiannaopeizhi-pc2849/ 排名 处理器 图例 分数 1 Intel Xeon Platinum 8173M ...

  7. 造一个轮子然后安装到pypi上

    之前写了一个爬虫的包,主要是根据自己写爬虫的情况总结一下. 因为每次都要重复写一些代码,所以提炼出来,类似一个框架的样子吧. 开始是放在自己的项目里引用,但如果换了一个项目,就得重新拷一遍,很麻烦. ...

  8. 代码版本控制:git使用

    1.https://github.com/ 注册账号 2. 点击 Start a project 3. 4. 5.      Clone or download 6.      安装git 7.    ...

  9. 查看Postgresql的连接状况

    今天遇到一个问题,就是pg一直报错,说有太多的客户端连接到数据库上面.但现在不知道是什么程序连接.pg默认的max_connection是100,我并没有修改过,以为平时公司内部用,应该够了,但现在貌 ...

  10. List的定制排序 包括使用lambda表达式来实现的方法

    1.先实现Comparator的接口 重写compare方法 根据比较大小来返回数值: 比如:(Integer o1  -   Integer o2); return 1 表示o1>o2; re ...