def _area_polygon(beam_poly,det_poly,sam_poly,plot=False,fig=None): """ Compute areas for polgon sample Parameters: ----------- * beam_pol is the beam polygon * det_poly is the detector polygon if det_poly = None, then just compute the beam and sample overlap ie A_int/A_beam = spill fraction """ if plot: if fig != None: pyplot.figure(fig) pyplot.clf() A_beam = poly_area(beam_poly) if sam_poly == None: inner_poly = beam_poly else: inner_poly = inner_polygon(beam_poly,sam_poly) if det_poly != None: inner_poly = inner_polygon(inner_poly,det_poly) A_int = poly_area(inner_poly) # make plots if plot: plot_polygon(beam_poly,fmt='ro-',label='Beam') if sam_poly != None: plot_polygon(sam_poly,fmt='bo-',label='Sample') if det_poly != None: plot_polygon(det_poly,fmt='ko-',label='Detector') plot_points(inner_poly,fmt='go-') plot_polygon(inner_poly,fmt='g--',linewidth=4,label='Intersection') pyplot.xlabel('Xs') pyplot.ylabel('Ys') ll = round(num.sqrt(100.*A_int)) pyplot.xlim(-ll,ll) pyplot.ylim(-ll,ll) pyplot.grid() pyplot.legend() return (A_beam,A_int)
import matplotlib.cm as cm fig, ax = plt.subplots(1) xres, yres = 320, 240 img, img_ = get_rgb_image(xres, yres) point = [(0., 0.), (-1., 0.), (-1., -1.)] import math scale = 30 center = xres/2, yres/2 for i in xrange(15): pts = [(int(center[0] + p[0]*scale), int(center[1] + p[1]*scale)) for p in point] draw_polygon(img, pts, [i*10, 20+i*15, 50 + i*10]) plot_polygon(ax, pts, 'w') x, y = point[2] norm = math.sqrt(x*x + y*y) new_point = (x + -y/norm, y + x/norm) # orthogonal to x,y , point[1] = point[2] point[2] = new_point ax.imshow(img, cm.gray, interpolation='nearest') plt_show()
import json import polygon import matplotlib.patches import matplotlib.pyplot as plt def get_vertices(poly): return polygon.get_vertices(poly['nsides'], poly['radius'], poly['center'], poly['start_angle']) if __name__ == '__main__': #polys = [{'nsides': 6, 'radius' : 3.0, 'center': (0.0, 0.0), 'start_angle': 30}, # {'nsides': 4, 'radius' : 1.0, 'center': (1.0, 1.0), 'start_angle': 0} #] path = 'polygons.json' with open(path) as fhandle: jsonpolys = json.load(fhandle) polys = jsonpolys['polygons'] fig, ax = plt.subplots() for poly in polys: x, y = get_vertices(poly) polygon.plot_polygon(ax, x, y) ax.autoscale() ax.set_aspect('equal') plt.show()
from polygon import draw_polygon, plot_polygon import matplotlib.pyplot as plt import matplotlib.cm as cm fig, ax = plt.subplots(1) xres, yres = 160, 120 img = get_grayscale_image(xres, yres) poly1 = [(0, 0), (100, 0), (100, 50), (75, 75), (50, 50), (25, 75), (0, 50)] poly2 = [(0, 50), (25, 75), (50, 50), (75, 75), (100, 50), (100, 100), (0, 100)] plot_polygon(ax, poly1, 'y') plot_polygon(ax, poly2, 'w') draw_polygon(img, poly1, 200) draw_polygon(img, poly2, 100) ## issues. Either first or last scanline should have exactly one pixel poly3 = [(42, 22), (32, 32), (42, 42), (52, 32)] draw_polygon(img, poly3, 255) plot_polygon(ax, poly3, 'r') poly4 = [(10, 10), (30, 10), (30, 20), (20, 20), (12, 18)] draw_polygon(img, poly4, 255) plot_polygon(ax, poly4, 'g')