pku 1410(判断线段是否跟矩形相交)

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 22:16:43
pku 1410(判断线段是否跟矩形相交)
x[kAǿ.lf7ta(M'en d>XE_(j"EL/M dt>͙ssZ8Bp0_|.OYdQo<-H/'`YEè7< ?~r_3p)Q3]0zC I!%}788א _}0? O,ڝ0գ|HK2d f ]k!Nb9 n[.W p:`k{UVtA[JӀ)y8*}xj[s׶jk#Zi5p=ꉎ( ńO"_Ue1Ͳ:(PT, Q4]vA|-;}~QUd "AۀVäQWV@I,R\PIXXX|&.<-T|uQ' $jɹ4ί#.Չ@"0f B.7U T צy0-n_ǁ|KQ#ӭD[M-^,q?HH

pku 1410(判断线段是否跟矩形相交)
pku 1410(判断线段是否跟矩形相交)

pku 1410(判断线段是否跟矩形相交)
1/* 2* 题目要求:判断线段是否跟矩形相交 3* 注意:线段完全在矩形内也是相交 4* 解法:分别判断线段是否跟矩形的四条边相交,再判断线段的两个端点是否都在矩形内 5*/ 6 7 #include 8 #include 9 #include 1011usingnamespace std; 1213struct point { 14double x; 15double y; 16}A,B,C,D,E,F; 1718double crossProd(point A,point B,point C) { 19return (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x); 20} 2122bool segIntersect(point A,point B,point C,point D) {//判断线段是否相交 23if (max(A.x,B.x) >= min(C.x,D.x) && 24 max(C.x,D.x) >= min(A.x,B.x) && 25 max(A.y,B.y) >= min(C.y,D.y) && 26 max(C.y,D.y) >= min(A.y,B.y) && 27 crossProd(C,D,A)*crossProd(C,B,D)>=0 && 28 crossProd(A,B,C)*crossProd(A,D,B)>=0) returntrue; 29returnfalse; 30} 3132bool pointInRectangle(point S,point A,point B) {//判断点是否在矩形内 33if (max(A.x,B.x) >= S.x && 34 min(A.x,B.x) = S.y && 36 min(A.y,B.y)