Created
May 11, 2018 16:50
-
-
Save chengluyu/f8ad1266fcd6821f9e19a8150c1bc04c to your computer and use it in GitHub Desktop.
Sutherland Hodgman Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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