Advertisement
3diagramsperpage

python_ellipse_grid

Apr 1st, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2.  
  3. '''
  4.  
  5.  PYTHON_ELLIPSE_GRID generates grid points inside an ellipse.
  6.  
  7.    This a python implementation of John Burkardt's ellipse_grid c++ code available at http://people.sc.fsu.edu/~jburkardt/c_src/ellipse_grid/ellipse_grid.html
  8.  
  9.  
  10.  Licensing:
  11.    
  12.    This code is distributed under the GNU LGPL license.
  13.  
  14.  
  15.  Author:
  16.  
  17.    Original Version: John Burkardt, Python Implementation: Felix Hoffmann, http://3diagramsperpage.wordpress.com/
  18.  
  19.  
  20.  Discussion:
  21.  
  22.    The ellipse is specified as
  23.  
  24.      ( ( X - C1 ) / R1 )^2 + ( ( Y - C2 ) / R2 )^2 = 1
  25.  
  26.    The user supplies a number N.  There will be N+1 grid points along
  27.    the shorter axis.
  28.  
  29.  
  30.  Parameters:
  31.  
  32.    Input, N, the number of subintervals on the shorter half axis.
  33.  
  34.    Input, r1, length of the first half axis.
  35.  
  36.    Input, r2, length of the second half axis.
  37.  
  38.    Input, c1, x-coordinate of the center of the ellipse.
  39.    
  40.    Input, c2, y-coordinate of the center of the ellipse.
  41.    
  42.    Output, xy, list of grid points.
  43.  
  44.  
  45. '''
  46.  
  47.  
  48.  
  49.  
  50.  
  51. def i4_ceiling(x):
  52.  
  53.     int_x = int(x)
  54.  
  55.     if int_x < x:
  56.         int_x += 1
  57.  
  58.     return int_x
  59.  
  60.  
  61. def plot_the_ellipse(xy, r1, r2):
  62.  
  63.     from matplotlib.patches import Ellipse
  64.  
  65.     xy_x = [x for k,x in enumerate(xy) if k%2==0]
  66.     xy_y = [y for k,y in enumerate(xy) if k%2==1]
  67.  
  68.    
  69.     fig = plt.figure(facecolor='white')
  70.     ax = fig.add_subplot(111, aspect='equal')
  71.     E = Ellipse([0,0], 2*r1, 2*r2)
  72.     E.set_alpha(0.1)
  73.     ax.add_artist(E)
  74.  
  75.     ax.scatter(xy_x, xy_y)
  76.  
  77.     fig.show()
  78.    
  79.  
  80. def generate_ellispe_grid(N, r1, r2, c1 = 0, c2 = 0, plot_ellipse = True):
  81.  
  82.     xy = []
  83.  
  84.     if r1 < r2:
  85.    
  86.         h = 2.0 * r1/(float(2*N+1))
  87.         ni = N
  88.         nj = i4_ceiling(float(r2)/float(r1)) * N        
  89.    
  90.     else:
  91.  
  92.         h = 2.0 * r2/(float(2*N+1))
  93.         nj = N
  94.         ni = i4_ceiling(float(r1)/float(r2)) * N  
  95.    
  96.  
  97.  
  98.     p = 0
  99.     j = 0
  100.  
  101.     while j <= nj:
  102.  
  103.         i = 0
  104.         x = c1
  105.         y = c2 + j*h
  106.  
  107.         xy.append(x)
  108.         xy.append(y)
  109.    
  110.         p += 1
  111.  
  112.         if 0 < j:
  113.  
  114.           xy.append(x)
  115.           xy.append(2.0 * c2 - y)
  116.           p += 1
  117.  
  118.         while True:
  119.  
  120.             i += 1
  121.             x = c1 + i*h
  122.  
  123.             if 1.0 < ((x - c1)/float(r1))**2 + ((y - c2)/float(r2))**2:
  124.                 break
  125.  
  126.             xy.append(x)
  127.             xy.append(y)
  128.             p += 1
  129.             xy.append(2.0 *c1 - x)
  130.             xy.append(y)
  131.             p += 1
  132.        
  133.             if 0 < j:
  134.  
  135.                 xy.append(x)
  136.                 xy.append(2.0 * c2 - y)
  137.                 p += 1
  138.                 xy.append(2.0 * c1 - x)
  139.                 xy.append(2.0 * c2 - y)
  140.                 p += 1
  141.  
  142.         j += 1
  143.    
  144.  
  145.     if plot_ellipse:
  146.         plot_the_ellipse(xy, r1, r2)
  147.  
  148.     print len(xy)/2.
  149.     return xy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement