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. Myeclipse修改项目名称发布后web Context root名称无法修改

    选中项目,右键--->Properties--->在搜索框搜索:deployment as  然后点击显示出的搜索项.修改右侧视图的Web Context Root名称即可. 如图:

  2. Asp.net的生命周期之应用程序生命周期

    参考:http://msdn.microsoft.com/zh-cn/library/ms178473(v=vs.100).aspx 参考:http://www.cnblogs.com/JimmyZh ...

  3. 架构师养成记--23.sigar使用实例

    作用是检测机器的硬件环境 注意在jdk的bin目录下加上sigar的lib目录中的文件 import java.net.InetAddress; import java.net.UnknownHost ...

  4. Docker for Windows 启动失败,提示Kubernetes证书无效

    起因 部署服务器到一台很久未更新的系统(windows 10),安装docker后,恰好系统自动更新,重启后docker不能启动,提示Kubernetes证书无效(未截到图,抱歉) 排查 因为没有开启 ...

  5. 谈谈数据库的ACID

    一.事务 定义:所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位. 准备工作:为了说明事务的ACID原理,我们使用银行账户及资金管理的案例进行分析. // 创建 ...

  6. Linux系统编程:线程控制

    一.提出问题 问1.线程存在的意义是什么?什么时候适合使用多线程? 答1.在单进程环境中实现多任务,线程可访问其所在进程的资源,例如内存.描述符等.对于单进程,如果要完成多项任务,这些任务只能依次执行 ...

  7. javascript绑定事件addEventListener与attachEvent

    1.eleObj.addEventListener(eventName,handle,useCapture); eleObj:DOM元素: eventName:事件名称.注意,这里的事件名称没有“ o ...

  8. JavaScript设计模式-9.工厂模式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. Java PrepareStatement

    1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程2.使用 Statement 对象.在对数据库只执行一次性存取的时侯,用 Statement 对象 ...

  10. PHP之mb_strripos使用

    mb_strripos (PHP 4 >= 4.0.6, PHP 5, PHP 7) mb_strrpos - Find position of last occurrence of a str ...