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. jersey annotations

    参照: http://blog.csdn.net/a19881029/article/details/43056429 官网文档翻译版 @Path 用来为资源类或方法定义URI,当然除了静态URI也支 ...

  2. BZOJ3168. [HEOI2013]钙铁锌硒维生素(线性代数+二分图匹配)

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=3168 题解 首先,我们需要求出对于任意的 \(i, j(1 \leq i, j \leq ...

  3. C# .net 使用正则表达式去掉字符串中的数字

    /// <summary>/// 去掉字符串中的数字/// </summary>/// <param name="key"></param ...

  4. .NET(C#):使用反射来获取枚举的名称、值和特性

    首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enum MyEnum { AAA, BBB, CCC } 背后的IL是这样的: .clas ...

  5. Linux网络编程服务器模型选择之IO复用循环并发服务器

    在前面我们介绍了循环服务器,并发服务器模型.简单的循环服务器每次只能处理一个请求,即处理的请求是串行的,效率过低:并发服务器可以通过创建多个进程或者是线程来并发的处理多个请求.但是当客户端增加时,就需 ...

  6. Java反射机制的浅显理解(这篇文章还没写好,留个草稿给自己看的)

    目前只是有一个大概的理解,先把自己感觉容易立即的概念放这里,等以后结合实际工作理解深刻了再来补充. 一.什么是Java反射机制?(多种定义) 1. JAVA反射机制是在运行状态中,对于任意一个类,都能 ...

  7. javascript中prototype与__proto__

    1.prototype:构造函数独有的属性: __proto__:每个对象都有一个名为__proto__的属性: 注意:每个构造函数(自带与自创)都有一个prototype的属性,构造函数的proto ...

  8. 【文档】六、Mysql Binlog版本

    binlog文件格式有以下几种: v1:用于3.23版本 v3:用于4.0.2到4.1版本 v4:用于5.0及以上版本 v2版本只在4.0.x版本中使用,目前已经不再支持了. 处理binlog的程序必 ...

  9. js判断数组中是否有重复值得三种方法

    方法一: var s = ary.join(",")+","; for(var i=0;i<ary.length;i++) { if(s.replace( ...

  10. 王亮:游戏AI探索之旅——从alphago到moba游戏

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由云加社区技术沙龙 发表于云+社区专栏 演讲嘉宾:王亮,腾讯AI高级研究员.2013年加入腾讯,从事大数据预测以及游戏AI研发工作.目前 ...