I am trying to write some code that takes a user specified closed polygon, and then a user specified horizontal line to cut the polygon with the horizontal line to create a new shape. I also need to determine the area of the shape, and its perimeter. This is what I have so far:
I do not know how to create the new shape as I need to insert new points to form a new polygon? PS.. Have tried to insert this as a code block, but for some reason it re-formats it all the time ??? I have found bits of code to do some bits on Stack Overflow... Any assistance much appreciated...
enter code here
"""
ROUTINE TO FIND MULTIPLE INTERSECTION POINTS FOR A HORIZONTAL LINE CUTTING THROUGH AN ARBITRARY CROSS SECTIONAL SHAPE
This routine should start with a poly defined by the user,
take the Y_specified, to adjust the polygon shape, (Effectively cut the top off)
and then calculate the area of the polygon
Since Y_Specified may be between points of the defined polygon, the intersection points need to be found
"""
import pylab as pl
import numpy as np
def line(p1, p2):
# This is how a line is defined
A = (p1[1] - p2[1])
B = (p2[0] - p1[0])
C = (p1[0]*p2[1] - p2[0]*p1[1])
return A, B, -C
def intersection(L1, L2):
# This finds the intersection of 2 lines
# Should I limit the Range here ???
D = L1[0] * L2[1] - L1[1] * L2[0]
Dx = L1[2] * L2[1] - L1[1] * L2[2]
Dy = L1[0] * L2[2] - L1[2] * L2[0]
if D != 0:
x = Dx / D
y = Dy / D
return x,y
else:
return False
# This defined a closed Hexagonal sort of shape... 30 wide and 15 high
poly = [[0,10],[10,0],[20,0],[30,10],[18,15],[12,15],[0,10]]
# This defines a W shape with a lid !!
poly = [[0,10],[10,0],[12,0],[15,13],[18,0],[20,0],[30,10],[18,15],[12,15],[0,10]]
x = tuple(x[0] for x in poly)
y = tuple(x[1] for x in poly)
print x
print y
pl.plot( x,y, 'go-')
pl.suptitle('CROSS SECTIONAL SHAPE', fontsize=14, fontweight='bold')
pl.title('To be cut and replot it')
pl.xlabel('X-Co-ord')
pl.ylabel('Y-Co-ord')
pl.show()
print len(poly)
# This is the cut line
Y_Specified = 12.0
Orig_ymax = max(poly, key=lambda x: x[1])
Orig_ymax = Orig_ymax[1]
Orig_xmin = min(poly, key=lambda x: x[0])
Orig_xmin = Orig_xmin[0]
Orig_xmax = max(poly, key=lambda x: x[0])
Orig_xmax = Orig_xmax[0]
Orig_max_width = Orig_xmax - Orig_xmin
# DEFINE THE HORIZONTAL LINE
L2_PT1 = [Orig_xmin,Y_Specified]
L2_PT2 = [Orig_xmax,Y_Specified]
L2 = line(L2_PT1, L2_PT2 )
x2=[]
y2=[]
for n,i in enumerate(poly):
if n+1< len(poly):
L1_PT1 = poly[n]
L1_PT2 = poly[n+1]
L1 = line(L1_PT1, L1_PT2)
R = intersection(L1, L2) # This correctly identifies the intersection points within the width... but also outside ???
if R:
print "Intersection detected:", R
print R[0],R[1]
x2.append(R[0])
y2.append(R[1])
else:
print "No single intersection point detected"
print x2
print y2
# Now need to remove points from the original poly above the Y_Specified and insert the new intersection points
# That is if Poly[1] > Y_Specified (Delete it) and insert new point to create the newly formed smaller hexagonal shape ???
pl.plot( x,y, 'go-')
pl.plot(x2,y2, 'ro-')
# The new polygon needs to be defined by the green line cut off by the red line...???
# HOW CAN THIS BE DONE ???
pl.show()