椭圆与单位

比较用弧形生成的椭圆与多边形近似

此示例需要 basic_units.py

  1. from basic_units import cm
  2. import numpy as np
  3. from matplotlib import patches
  4. import matplotlib.pyplot as plt
  5. xcenter, ycenter = 0.38*cm, 0.52*cm
  6. width, height = 1e-1*cm, 3e-1*cm
  7. angle = -30
  8. theta = np.deg2rad(np.arange(0.0, 360.0, 1.0))
  9. x = 0.5 * width * np.cos(theta)
  10. y = 0.5 * height * np.sin(theta)
  11. rtheta = np.radians(angle)
  12. R = np.array([
  13. [np.cos(rtheta), -np.sin(rtheta)],
  14. [np.sin(rtheta), np.cos(rtheta)],
  15. ])
  16. x, y = np.dot(R, np.array([x, y]))
  17. x += xcenter
  18. y += ycenter
  1. fig = plt.figure()
  2. ax = fig.add_subplot(211, aspect='auto')
  3. ax.fill(x, y, alpha=0.2, facecolor='yellow',
  4. edgecolor='yellow', linewidth=1, zorder=1)
  5. e1 = patches.Ellipse((xcenter, ycenter), width, height,
  6. angle=angle, linewidth=2, fill=False, zorder=2)
  7. ax.add_patch(e1)
  8. ax = fig.add_subplot(212, aspect='equal')
  9. ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1)
  10. e2 = patches.Ellipse((xcenter, ycenter), width, height,
  11. angle=angle, linewidth=2, fill=False, zorder=2)
  12. ax.add_patch(e2)
  13. fig.savefig('ellipse_compare')

椭圆与单位示例

  1. fig = plt.figure()
  2. ax = fig.add_subplot(211, aspect='auto')
  3. ax.fill(x, y, alpha=0.2, facecolor='yellow',
  4. edgecolor='yellow', linewidth=1, zorder=1)
  5. e1 = patches.Arc((xcenter, ycenter), width, height,
  6. angle=angle, linewidth=2, fill=False, zorder=2)
  7. ax.add_patch(e1)
  8. ax = fig.add_subplot(212, aspect='equal')
  9. ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1)
  10. e2 = patches.Arc((xcenter, ycenter), width, height,
  11. angle=angle, linewidth=2, fill=False, zorder=2)
  12. ax.add_patch(e2)
  13. fig.savefig('arc_compare')
  14. plt.show()

椭圆与单位示例2

下载这个示例