Skip to content

Instantly share code, notes, and snippets.

@chengluyu
Created May 11, 2018 16:50
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 chengluyu/f8ad1266fcd6821f9e19a8150c1bc04c to your computer and use it in GitHub Desktop.
Save chengluyu/f8ad1266fcd6821f9e19a8150c1bc04c to your computer and use it in GitHub Desktop.
Sutherland Hodgman Algorithm
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
class Segment:
def __init__(self, p, q):
self.p = p
self.q = q
def show_polygon(ax, vertices, **kwargs):
verts = vertices + [(0, 0)]
codes = [Path.MOVETO] + [Path.LINETO] * (len(vertices) - 1) + [Path.CLOSEPOLY]
path = Path(verts, codes)
patch = patches.PathPatch(path, **kwargs)
ax.add_patch(patch)
if __name__ == '__main__':
subject_polygon = [
(15.6, 284.38716),
(50.6, 389.3872),
(70.6, 389.3872),
(80.6, 354.3872),
(90.6, 389.3872),
(110.6, 389.3872),
(145.6, 284.38716),
(125.6, 284.38716),
(100.6, 369.3872),
(90.6, 334.38716),
(70.6, 334.38716),
(60.6, 369.3872),
(35.6, 284.38716),
(15.6, 284.38716)
]
clip_polygon = [
(10.6, 334.38716),
(30.6, 294.38716),
(85.6, 279.38716),
(155.6, 334.38716),
(75.6, 389.3872),
(10.6, 334.38716)
]
fig = plt.figure()
ax = fig.add_subplot(111)
show_polygon(ax, subject_polygon, facecolor='#c8c8c8', edgecolor='#000000')
show_polygon(ax, clip_polygon, facecolor='#ff000020', edgecolor='#ff0000')
ax.axis('equal')
ax.set_xlim(5, 160)
ax.set_ylim(260, 400)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment