求直线交点还是要推一个公式的。。

见博客https://blog.csdn.net/u013050857/article/details/40923789

还要学一下向量的定点比分法

另外poj精度好像卡的厉害,zoj1280就没啥问题

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<algorithm>
  5. #include<cmath>
  6. using namespace std;
  7.  
  8. const double esp = 1e-;
  9. const double inf = 1e20;
  10. const double pi = acos(-1.0);
  11. const int maxp = ;
  12.  
  13. int sgn(double x){
  14. if(fabs(x) < esp)return ;
  15. if(x < )return -;
  16. else return ;
  17. }
  18. inline double sqr(double x){return x*x;}
  19.  
  20. struct Point{
  21. double x,y;
  22. Point(){}
  23. Point(double _x,double _y):x(_x),y(_y){}
  24. void input(){scanf("%lf%lf",&x,&y);}
  25. void output(){printf("%.2lf %.2lf\n",x,y);}
  26. bool operator==(Point b)const {
  27. return sgn(x-b.x)== && sgn(y-b.y)==;
  28. }
  29. bool operator < (Point b)const {//判左下
  30. if( sgn(x-b.x)== ) //横坐标相等
  31. return sgn(y-b.y)<;
  32. return x<b.x;
  33. }
  34. Point operator - (const Point &b)const {
  35. return Point(x-b.x,y-b.y);
  36. }
  37. double operator ^(const Point &b)const {
  38. return x*b.y-y*b.x;
  39. }
  40. double operator *(const Point &b)const {
  41. return x*b.x+y*b.y;
  42. }
  43. double len(){
  44. return hypot(x,y);
  45. }
  46. double len2(){
  47. return x*x+y*y;
  48. }
  49. double distance(Point p){
  50. return hypot(x-p.x,y-p.y);
  51. }
  52. Point operator +(const Point &b)const {
  53. return Point(x+b.x,y+b.y);
  54. }
  55. Point operator *(const double &k)const {
  56. return Point(x*k,y*k);
  57. }
  58. Point operator /(const double &k)const {
  59. return Point(x/k,y/k);
  60. }
  61. double rad(Point a,Point b){
  62. Point p=*this;
  63. return fabs(atan2( fabs((a-p)^(b-p)),(a-p)*(b-p) ));
  64. }
  65. Point trunc(double r){
  66. double l=len();
  67. if(!sgn(l))return *this;
  68. r/=l;
  69. return Point(x*r,y*r);
  70. }
  71. Point rotleft(){
  72. return Point(-y,x);
  73. }
  74. Point rotright(){
  75. return Point(y,-x);
  76. }
  77. Point rotate(Point p,double angle){
  78. Point v=(*this)-p;
  79. double c=cos(angle),s=sin(angle);
  80. return Point(p.x+v.x*c-v.y*s, p.y+v.x*s+v.y*c);
  81. }
  82. };
  83. struct Line{
  84. Point s,e;
  85. Line(){}
  86. Line(Point s,Point e):s(s),e(e){}
  87. bool operator ==(Line v){
  88. return (s==v.s) && (e==v.e);
  89. }
  90. Line(Point p,double angle){
  91. s=p;
  92. if(sgn(angle-pi/)==)
  93. e=s+Point(,);
  94. else e=s+Point(,tan(angle));
  95. }
  96. Line(double a,double b,double c){
  97. if(sgn(a)==){
  98. s=Point(,-c/b);
  99. e=Point(,-c/b);
  100. }
  101. else if(sgn(b)==){
  102. s=Point(-c/a,);
  103. e=Point(-c/a,);
  104. }
  105. else {
  106. s=Point(,-c/b);
  107. e=Point(,(-c-a)/b);
  108. }
  109. }
  110. void input(){
  111. s.input();
  112. e.input();
  113. }
  114. void adjust(){
  115. if(e<s)swap(e,s);
  116. }
  117. double length(){
  118. return s.distance(e);
  119. }
  120. double angle(){
  121. double k=atan2(e.y-s.y,e.x-s.x);
  122. if(sgn(k)<)k+=pi;
  123. if(sgn(k-pi)==) k-=pi;
  124. return k;
  125. }
  126. int relation(Point p){
  127. int c=sgn((p-s)^(e-s));
  128. if(c<)return ;
  129. else if(c>)return ;
  130. else return ;
  131. }
  132. bool pointonseg(Point p){
  133. return sgn((p-s)^(e-s))== && sgn((p-s)*(p-e))<=;
  134. }
  135. bool parallel(Line v){
  136. return sgn((e-s)^(v.e-v.s))==;
  137. }
  138. int segcrossseg(Line v){
  139. int d1=sgn((e-s)^(v.s-s));
  140. int d2=sgn((e-s)^(v.e-s));
  141. int d3=sgn((v.e-v.s)^(s-v.s));
  142. int d4=sgn((v.e-v.s)^(e-v.s));
  143. if( (d1^d2)==- && (d3^d4)==-) return ;
  144. return (d1== && sgn((v.s-s)*(v.s-e))<=) ||
  145. (d2== && sgn((v.e-s)*(v.e-e))<=) ||
  146. (d3== && sgn((s-v.s)^(s-v.e))<=) ||
  147. (d4== && sgn((e-v.s)^(e-v.e))<=);
  148. }
  149. int linecrossseg(Line v){
  150. int d1=sgn((e-s)^(v.s-s));
  151. int d2=sgn((e-s)^(v.e-s));
  152. //cout<<(d1^d2)<<'\n';
  153. if((d1^d2)==-)return ;
  154. return d1== || d2==;
  155. }
  156. int linecrossline(Line v){
  157. if((*this).parallel(v))
  158. return v.relation(s)==;
  159. return ;
  160. }
  161. Point crosspoint(Line v){
  162. double a1=(v.e-v.s)^(s-v.s);//面积
  163. double a2=(v.e-v.s)^(e-v.s);
  164. return Point((s.x*a2-e.x*a1)/(a2-a1),
  165. (s.y*a2-e.y*a1)/(a2-a1));
  166. }
  167. double dispointtoline(Point p){
  168. return fabs((p-s)^(e-s))/length();
  169. }
  170. double dispointtoseg(Point p){
  171. if(sgn((p-s)*(e-s))< || sgn((p-e)*(s-e))<)
  172. return min(p.distance(s),p.distance(e));
  173. return dispointtoline(p);
  174. }
  175. double dissegtoseg(Line v){
  176. return min(min(dispointtoseg(v.s),dispointtoseg(v.e)),
  177. min(v.dispointtoline(s),v.dispointtoline(e)));
  178. }
  179. Point lineprog(Point p){//s+vt
  180. return s+( ((e-s)*((e-s)*(p-s)))/(e-s).len2() );
  181. }
  182. Point symmetrypoint(Point p){
  183. Point q=lineprog(p);
  184. return Point(*q.x-p.x,*q.y-p.y);
  185. }
  186. };
  187.  
  188. //判两直线是否相交
  189. int main(){
  190. int t;cin>>t;
  191. puts("INTERSECTING LINES OUTPUT");
  192. for(int tt=;tt<=t;tt++){
  193. double x1,y1,x2,y2;
  194. cin>>x1>>y1>>x2>>y2;
  195. Line line1=Line(Point(x1,y1),Point(x2,y2));
  196. cin>>x1>>y1>>x2>>y2;
  197. Line line2=Line(Point(x1,y1),Point(x2,y2));
  198. if(line1.linecrossline(line2)==)
  199. puts("NONE");
  200. else if(line1.linecrossline(line2)==)
  201. puts("LINE");
  202. else if(line1.linecrossline(line2)==){
  203. Point p=line1.crosspoint(line2);
  204. printf("POINT %.2lf %.2lf\n",p.x,p.y);
  205. }
  206. }
  207. puts("END OF OUTPUT");
  208. }

计算几何——直线交点poj1269的更多相关文章

  1. UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)

    Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...

  2. hdu 2528:Area(计算几何,求线段与直线交点 + 求多边形面积)

    Area Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. UVA 11178 Morley's Theorem(旋转+直线交点)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18543 [思路] 旋转+直线交点 第一个计算几何题,照着书上代码打 ...

  4. POJ_1269_Intersecting Lines_求直线交点

    POJ_1269_Intersecting Lines_求直线交点 Description We all know that a pair of distinct points on a plane ...

  5. ZOJ 1280 Interesting Lines | 求两直线交点

    原题: 求两直线交点 思路借鉴于:http://blog.csdn.net/zxy_snow/article/details/6341282 感谢大佬 #include<cstdio> # ...

  6. 2017 ACM-ICPC乌鲁木齐网络赛 B. Out-out-control cars(计算几何 直线相交)

    题目描述 Two out-of-control cars crashed within about a half-hour Wednesday afternoon on Deer Park Avenu ...

  7. hdu 1086 You can Solve a Geometry Problem too 求n条直线交点的个数

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  8. poj1269(直线交点)

    传送门:Intersecting Lines 题意:给出N组直线,每组2条直线,求出直线是否相交.如果共线则输出LINE,相交则输入点坐标,否则输出NONE. 分析:模板裸题,直接上模板... #in ...

  9. poj1269计算几何直线和直线的关系

    We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a p ...

随机推荐

  1. LNMP之PHP

    PHP LNMP环境下的PHP安装 CGI指的是通用网关接口,为HTTP服务器与其他机器上的程序服务通信交流的一种工具,性能差,所以被淘汰了. FastCGI,是一个可以伸缩.高速的在HTTP服务器和 ...

  2. projects

    layout title project 开源项目 本文记录我收藏的开源项目

  3. C#反射从入门到放弃(这部分遇到的新东西太多了让人接受不能)

    首先,我们需要知道type,type是类型的类型(笑 官方点的说法是,BCL声明了一个Type抽象类,它被设计用来包含类型的特性, 使用这个类的对象(抽象类的对象?这显然是错误的,但是这里用的其实是T ...

  4. python编程语言学习day02

    格式化输出 (1)info 格式 (2)%字符串占位 %s 表示字符串占位 %d 表示整数占位 %f 表示浮点数占位 中间的%     之后是所需要输入的值 多个占位, %  之后用()括号括起    ...

  5. jQuery - 动画相关

    // 显示隐藏 $("div").show(); // 显示 $("div").hide(); // 隐藏 // 显示过程3秒, 3秒之内, 元素的宽,高和透明 ...

  6. mui请求数据接口问题

    今天我在本地模拟做一个数据请求,第一次用的mui自带的方法来请求数据,当时我的本地接口地址是http://localhost:8087/jeecg/sightseerController.do?che ...

  7. 通过队列实现进程间的通信(使用阻塞方式调用func函数)

    #_author:来童星#date:2019/12/17#通过队列实现进程间的通信from multiprocessing import Poolimport osimport timedef fun ...

  8. windows启动redis失败

    # Warning: no config file specified, using the default config. In order to specify a config file use ...

  9. NX二次开发-UFUN设置工程图PNG图片高度UF_DRF_set_image_height

    #include <uf.h> #include <uf_drf.h> UF_initialize(); //插入PNG char* file_name = "D:\ ...

  10. faster-rcnn代码阅读-rpn-data层

    这一节讲述rpn-data层,和这一层有关的结构图如下: rpn-data层的prototxt定义如下: layer { name: 'rpn-data' type: 'Python' bottom: ...