Skip to content

Instantly share code, notes, and snippets.

@kunalgoyal9
Last active March 12, 2021 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunalgoyal9/4cbfe6ee703bde8f2aeea3d954c2251c to your computer and use it in GitHub Desktop.
Save kunalgoyal9/4cbfe6ee703bde8f2aeea3d954c2251c to your computer and use it in GitHub Desktop.
Get area code from LP
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def onSegment(p, q, r):
if ( (q.x <= max(p.x, r.x)) and (q.x >= min(p.x, r.x)) and(q.y <= max(p.y, r.y)) and (q.y >= min(p.y, r.y))):
return True
return False
def orientation(p, q, r):
# to find the orientation of an ordered triplet (p,q,r)
# function returns the following values:
# 0 : Colinear points
# 1 : Clockwise points
# 2 : Counterclockwise
# See https://www.geeksforgeeks.org/orientation-3-ordered-points/amp/
# for details of below formula.
val = (float(q.y - p.y) * (r.x - q.x)) - (float(q.x - p.x) * (r.y - q.y))
if (val > 0):
# Clockwise orientation
return 1
elif (val < 0):
# Counterclockwise orientation
return 2
else:
# Colinear orientation
return 0
def doIntersect(p1,q1,p2,q2):
# Find the 4 orientations required for
# the general and special cases
o1 = orientation(p1, q1, p2)
o2 = orientation(p1, q1, q2)
o3 = orientation(p2, q2, p1)
o4 = orientation(p2, q2, q1)
# General case
if ((o1 != o2) and (o3 != o4)):
return True
# Special Cases
# p1 , q1 and p2 are colinear and p2 lies on segment p1q1
if ((o1 == 0) and onSegment(p1, p2, q1)):
return True
# p1 , q1 and q2 are colinear and q2 lies on segment p1q1
if ((o2 == 0) and onSegment(p1, q2, q1)):
return True
# p2 , q2 and p1 are colinear and p1 lies on segment p2q2
if ((o3 == 0) and onSegment(p2, p1, q2)):
return True
# p2 , q2 and q1 are colinear and q1 lies on segment p2q2
if ((o4 == 0) and onSegment(p2, q1, q2)):
return True
# If none of the cases
return False
def is_single_lined(boxes):
boxes.sort()
left_box = boxes[0]
right_box = boxes[-1]
l_left = (left_box[0], left_box[1], left_box[0], left_box[3])
l_right = (right_box[0], right_box[1], right_box[0], right_box[3])
l_center = (l_left[0], (l_left[3]-l_left[1])/2 + l_left[1], l_right[0], (l_right[3]-l_right[1])/2 + l_right[1])
l_cs = Point(l_center[0], l_center[1])
l_ce = Point(l_center[2], l_center[3])
top_boxes = []
bottom_boxes = []
for box in boxes[1:-1]:
l_iter = (box[0], box[1], box[0], box[3])
p1 = Point(l_iter[0], l_iter[1])
q1 = Point(l_iter[2], l_iter[3])
if doIntersect(l_cs, l_ce, p1, q1):
bottom_boxes.append(box)
else:
top_boxes.append(box)
if len(top_boxes) != 0 and len(bottom_boxes) != 0:
# double line plate
# retur False
return [top_boxes, bottom_boxes]
else:
# single line plate
max_dis = -1000
first_part = []
second_part = []
check = False
for idx, pbox in enumerate(boxes[:-1]):
nbox = boxes[idx+1]
dis = nbox[0] - pbox[2]
if(dis > 20):
check = True
if(!check):
first_part.append(pbox)
else:
second_part.append(pbox)
# max_dis = max(max_dis, dis)
if(!check):
first_part.append(boxes[-1])
else:
second_part.append(boxes[-1])
# return max_dis
return [first_part, second_part]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment