Skip to content

Instantly share code, notes, and snippets.

@ashgti
Created November 10, 2011 20:41
Show Gist options
  • Select an option

  • Save ashgti/1356144 to your computer and use it in GitHub Desktop.

Select an option

Save ashgti/1356144 to your computer and use it in GitHub Desktop.
from matplotlib import pyplot
from matplotlib.patches import Circle
from shapely.geometry import Polygon, LineString
from descartes.patch import PolygonPatch
import math
import numpy as np
COLOR = {
True: '#6699cc',
False: '#ff3333'
}
fig = pyplot.figure(1, dpi=90)
# 3: invalid polygon, ring touch along a line
ax = fig.add_subplot(121)
def v_color(ob):
return COLOR[ob.is_valid]
def plot_coords(ax, ob):
x, y = ob.xy
ax.plot(x, y, 'o', color='#999999', zorder=1)
def plot_line(ax, ob):
x, y = ob.xy
ax.plot(x, y, color=v_color(ob), alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2)
ext = [(0, 0), (0, 11), (10, 11), (10, 0), (0, 0)]
polygon = Polygon(ext)
plot_coords(ax, polygon.exterior)
patch = PolygonPatch(polygon, facecolor=v_color(polygon), edgecolor=v_color(polygon), alpha=0.5, zorder=2)
ax.add_patch(patch)
ax.set_title('a) valid')
xrange = [-1, 11]
yrange = [-1, 12]
ax.set_xlim(*xrange)
ax.set_xticks(range(*xrange) + [xrange[-1]])
ax.set_ylim(*yrange)
ax.set_yticks(range(*yrange) + [yrange[-1]])
ax.set_aspect(1)
polygon.boundary.
line = LineString([(0.5,0), (0.5,15)])
p = []
plot_line(ax, line)
done = False
# while not done:
for x in range(1, 10):
b = line.parallel_offset(x * 1.0, 'right')
print polygon.intersection(b)
plot_line(ax, b)
pyplot.show()
def distance(p1, p2):
a = p2[0] - p1[0]
b = p2[1] - p1[1]
return math.sqrt(a ** 2 + b ** 2)
def find_next_point_in_direction(current_point, lines):
pass
def find_next_point_along_line(current_point, line):
pass
def decompose(polygon, s_pos=(0,0),
width=1.5,
direction=0.0,
seperation=1.0,
turn_radius=1.0):
"""
Direction = radians (0 = east, 90 = north)
Seperation = distance
"""
(xa, ya) = polygon.exterior.coords.xy
(xb, yb) = polygon.exterior.coords.xy
xb = np.roll(xb, -1)
yb = np.roll(yb, -1)
longest_length = 0.0
longest = None
for (p1, p2) in zip(zip(xa, ya), zip(xb, yb)):
dist = distance(p1, p2)
if longest_length < dist:
longest = (p1, p2)
longest_length = dist
print longest_length
print longest
# grow in which direction?
print
first_point = (np.min(polygon.exterior.coords.xy[0]), np.min(polygon.exterior.coords.xy[1]))
print first_point
decompose(polygon)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment