POJ 1269 Intersecting Lines(计算几何)
题意:给定4个点的坐标,前2个点是一条线,后2个点是另一条线,求这两条线的关系,如果相交,就输出交点。
题解:先判断是否共线,我用的是叉积的性质,用了2遍就可以判断4个点是否共线了,在用斜率判断是否平行,最后就是相交了,求交点就好了。
求交点的过程和高中知识差不多,用y=kx+c来求,只不过要注意斜率不存在的时候特殊处理,还有就是求斜率的时候一定要强制转换,(坑爹的我,调试了一小时才找到这个bug)
AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int INF=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
const int MAX_N= 1 ;
const int EPS= 1e-9 ;
int a[10][8],n; int Multic(int x0,int y0,int x1,int y1,int x2,int y2)
{
int a1=x1-x0,b1=y1-y0;
int a2=x2-x0,b2=y2-y0;
return a1*b2-a2*b1;
} void sol(int row)
{
int te1=Multic(a[row][0],a[row][1],a[row][4],a[row][5],a[row][2],a[row][3]);
int te2=Multic(a[row][0],a[row][1],a[row][6],a[row][7],a[row][2],a[row][3]);
if (te1==0&&te2==0)
{
puts("LINE");
return;
}
if ((a[row][1]-a[row][3])*(a[row][4]-a[row][6])==
(a[row][0]-a[row][2])*(a[row][5]-a[row][7]))
{
puts("NONE");
return;
}
if (a[row][0]-a[row][2]==0||(a[row][4]-a[row][6])==0)
{
if (a[row][0]-a[row][2]==0)
{
double ansx=a[row][0];
double k2=(a[row][5]-a[row][7])/(double)(a[row][4]-a[row][6]);
double c2=a[row][7]-k2*a[row][6];
double ansy=k2*ansx+c2;
printf("POINT %.2f %.2f\n",ansx,ansy);
}
if ((a[row][4]-a[row][6])==0)
{
double ansx=a[row][4];
double k1=(a[row][1]-a[row][3])/(double)(a[row][0]-a[row][2]);
double c1=a[row][1]-k1*a[row][0];
double ansy=k1*ansx+c1;
}
return;
} double k1=(a[row][1]-a[row][3])/(double)(a[row][0]-a[row][2]);///一定要注意这 ,要强制类型转换!!!!!!!!!!!!
double k2=(a[row][5]-a[row][7])/(double)(a[row][4]-a[row][6]);///否则就是int除int了 double c1=a[row][1]-k1*a[row][0];
double c2=a[row][7]-k2*a[row][6]; double ansx=(c2-c1)/(k1-k2);
double ansy=k1*ansx+c1; printf("POINT %.2f %.2f\n",ansx,ansy);
} int main()
{
SI(n);
rep(i,n)
rep(j,8)
SI(a[i][j]);
puts("INTERSECTING LINES OUTPUT");
rep(i,n)
sol(i);
puts("END OF OUTPUT");
return 0;
}
POJ 1269 Intersecting Lines(计算几何)的更多相关文章
- POJ 1269 Intersecting Lines --计算几何
题意: 二维平面,给两条线段,判断形成的直线是否重合,或是相交于一点,或是不相交. 解法: 简单几何. 重合: 叉积为0,且一条线段的一个端点到另一条直线的距离为0 不相交: 不满足重合的情况下叉积为 ...
- POJ 1269 Intersecting Lines(判断两直线位置关系)
题目传送门:POJ 1269 Intersecting Lines Description We all know that a pair of distinct points on a plane ...
- ●POJ 1269 Intersecting Lines
题链: http://poj.org/problem?id=1269 题解: 计算几何,直线交点 模板题,试了一下直线的向量参数方程求交点的方法. (方法详见<算法竞赛入门经典——训练指南> ...
- POJ 1269 - Intersecting Lines - [平面几何模板题]
题目链接:http://poj.org/problem?id=1269 Time Limit: 1000MS Memory Limit: 10000K Description We all know ...
- 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 ...
- poj 1269 Intersecting Lines
题目链接:http://poj.org/problem?id=1269 题目大意:给出四个点的坐标x1,y1,x2,y2,x3,y3,x4,y4,前两个形成一条直线,后两个坐标形成一条直线.然后问你是 ...
- 判断两条直线的位置关系 POJ 1269 Intersecting Lines
两条直线可能有三种关系:1.共线 2.平行(不包括共线) 3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...
- poj 1269 Intersecting Lines——叉积求直线交点坐标
题目:http://poj.org/problem?id=1269 相关知识: 叉积求面积:https://www.cnblogs.com/xiexinxinlove/p/3708147.html什么 ...
- POJ 1269 Intersecting Lines (判断直线位置关系)
题目链接:POJ 1269 Problem Description We all know that a pair of distinct points on a plane defines a li ...
随机推荐
- Android—对话框
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- VPS搭建VPN(pptpd)
环境:Ubuntu Server 12.04 下载FQ程序 wget http://cdxf.yun.ftn.qq.com/ftn_handler/40ad8a2875adf1f7b5193f54a5 ...
- Lua快速入门
-- 两个横线开始单行的注释 --[[ 加上两个[和]表示 多行的注释. --]] ---------------------------------------------------- -- 1. ...
- MUA
a big deal analysis analytics cooperate 合作 efficient explicitly fine grained Granularity graph geogr ...
- C#加载dll 创建类对象
//加载dll 创建类对象string sqlightAssembly = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "syst ...
- javascript 毫秒转日期 日期时间转毫秒
js毫秒时间转换成日期时间 var oldTime = (new Date("2011/11/11 20:10:10")).getTime(); //得到毫秒数 大多数是用毫秒数除 ...
- centos 6安装redis 2.8.19
下载安装: wget https://github.com/antirez/redis/archive/2.8.19.tar.gz tar xvzf redis-stable.tar.gz cd re ...
- maven设置---Dmaven.multiModuleProjectDirectory system propery is not set.
设置maven 环境变量: MAVEN_HOME:D:\Java\apache-maven-3.3.3 M2_HOME:D:\Java\apache-maven-3.3.3 path:%MAVEN_H ...
- Oracle监控代理安装ITM(IBM Tivoli Monitoring)
1 监控代理安装 2 1.1 安装 2 1.1.1 解压安装包 2 1.1.2 安装 2 1.2 配置 5 1.2.1 给Agent授权 5 1.2.2 配置Oracle Agent 10 目录 1 ...
- ArrayList和LinkedList遍历方式及性能对比分析
ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayLis ...