Advertisement
Guest User

3D Plots [LAST FUNC LEFT]

a guest
May 20th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.09 KB | None | 0 0
  1. from mpl_toolkits.mplot3d import axes3d
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4.  
  5.  
  6. def ellipsoid():
  7.     # DONE!
  8.     a, b, c = map(float, input("Введите коэффициенты a, b и c через пробел: ").split())
  9.     coefs = (a, b, c)
  10.     rx, ry, rz = (coefs)
  11.  
  12.     # Установка всех сферических углов:
  13.     u = np.linspace(0, 2 * np.pi, 100)
  14.     v = np.linspace(0, np.pi, 100)
  15.  
  16.     # Декартовы координаты, которые соответствуют сферическим углам
  17.     # (уравнение эллипсоида):
  18.     x = rx * np.outer(np.cos(u), np.sin(v))
  19.     y = ry * np.outer(np.sin(u), np.sin(v))
  20.     z = rz * np.outer(np.ones_like(u), np.cos(v))
  21.  
  22.     ax.plot_wireframe(x, y, z, rstride=6, cstride=6)
  23.  
  24.     # Регулировка осей, чтобы все они имели одинаковый промежуток
  25.     max_radius = max(rx, ry, rz)
  26.     for axis in 'xyz':
  27.         getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  28.  
  29.  
  30. def oneline_hyperboloid():
  31.     # DONE!
  32.     a, b, c = map(float, input("Введите коэффициенты a, b и c через пробел: ").split())
  33.     coefs = (a, b, c)
  34.     rx, ry, rz = (coefs)  # 1 / np.sqrt(coefs)
  35.     u = np.linspace(-2, 2, 100)
  36.     v = np.linspace(0, 2 * np.pi, 60)
  37.     [u, v] = np.meshgrid(u, v)
  38.     axys = input("Введите направляющую ось x, y или z: ")
  39.     axys.lower()
  40.     while axys != "x" and axys != "y" and axys != "z":
  41.         axys = input("Ошибка, введите x, y или z: ")
  42.     if axys == "z":
  43.         x = rx * np.cosh(u) * np.cos(v)
  44.         y = ry * np.cosh(u) * np.sin(v)
  45.         z = rz * np.sinh(u)
  46.     elif axys == "y":
  47.         z = rx * np.cosh(u) * np.cos(v)
  48.         y = ry * np.cosh(u) * np.sin(v)
  49.         x = rz * np.sinh(u)
  50.     elif axys == "x":
  51.         x = rx * np.cosh(u) * np.cos(v)
  52.         z = ry * np.cosh(u) * np.sin(v)
  53.         y = rz * np.sinh(u)
  54.  
  55.     ax.plot_wireframe(x, y, z, rstride=6, cstride=6)
  56.  
  57.     max_radius = max(rx * 4, ry * 4, rz * 4)
  58.     for axis in 'xyz':
  59.         getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  60.  
  61.  
  62. def twoline_hyperboloid():
  63.     # DONE!
  64.     a, b, c = map(float, input("Введите коэффициенты a, b и c через пробел: ").split())
  65.     coefs = (a, b, c)
  66.     rx, ry, rz = coefs
  67.     u = np.linspace(-3, 3, 100)
  68.     v = np.linspace(0, 2 * np.pi, 100)
  69.     u, v = np.meshgrid(u, v)
  70.     axys = input("Введите направляющую ось x, y или z: ")
  71.     axys.lower()
  72.     while axys != "x" and axys != "y" and axys != "z":
  73.         axys = input("Ошибка, введите x, y или z: ")
  74.     if axys == "z":
  75.         x = a * np.sinh(u) * np.cos(v)
  76.         y = b * np.sinh(u) * np.sin(v)
  77.         z = c * np.cosh(u)
  78.         ax.plot_wireframe(x / 10, y / 10, z / 10, rstride=10, cstride=10)
  79.         ax.plot_wireframe(x / 10, y / 10, -z / 10, rstride=10, cstride=10)
  80.     elif axys == "y":
  81.         z = a * np.sinh(u) * np.cos(v)
  82.         y = b * np.sinh(u) * np.sin(v)
  83.         x = c * np.cosh(u)
  84.         ax.plot_wireframe(x / 10, y / 10, z / 10, rstride=10, cstride=10)
  85.         ax.plot_wireframe(-x / 10, y / 10, z / 10, rstride=10, cstride=10)
  86.     elif axys == "x":
  87.         x = a * np.sinh(u) * np.cos(v)
  88.         z = b * np.sinh(u) * np.sin(v)
  89.         y = c * np.cosh(u)
  90.         ax.plot_wireframe(x / 10, y / 10, z / 10, rstride=10, cstride=10)
  91.         ax.plot_wireframe(x / 10, -y / 10, -z / 10, rstride=10, cstride=10)
  92.  
  93.     max_radius = max(rx, ry, rz)
  94.     for axis in 'xyz':
  95.         getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  96.  
  97.  
  98. def konus():
  99.     # DONE!
  100.     a, b, c = map(float, input("Введите коэффициенты a, b и c через пробел: ").split())
  101.     coefs = (a, b, c)
  102.     rx, ry, rz = coefs
  103.     u = np.linspace(0, 2 * np.pi, 100)
  104.     v = np.linspace(0, 2, 100)
  105.     t, r = np.meshgrid(u, v)
  106.     axys = input("Введите направляющую ось x, y или z: ")
  107.     axys.lower()
  108.     while axys != "x" and axys != "y" and axys != "z":
  109.         axys = input("Ошибка, введите x, y или z: ")
  110.     if axys == "z":
  111.         x = r * a * np.cos(t)
  112.         y = r * b * np.sin(t)
  113.         z = -r * c
  114.  
  115.         ax.plot_wireframe(x/2, y/2, z/2, rstride=6, cstride=6)
  116.         ax.plot_wireframe(x/2, y/2, -z/2, rstride=6, cstride=6)
  117.  
  118.     elif axys == "y":
  119.         z = r * a * np.cos(t)
  120.         y = r * b * np.sin(t)
  121.         x = -r * c
  122.  
  123.         ax.plot_wireframe(x / 2, y / 2, z / 2, rstride=6, cstride=6)
  124.         ax.plot_wireframe(-x / 2, y / 2, z / 2, rstride=6, cstride=6)
  125.  
  126.     elif axys == "x":
  127.         x = r * a * np.cos(t)
  128.         z = r * b * np.sin(t)
  129.         y = -r * c
  130.  
  131.         ax.plot_wireframe(x / 2, y / 2, z / 2, rstride=6, cstride=6)
  132.         ax.plot_wireframe(x / 2, -y / 2, z / 2, rstride=6, cstride=6)
  133.  
  134.     max_radius = max(rx, ry, rz)
  135.     for axis in 'xyz':
  136.         getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  137.  
  138.  
  139. def elliptic_paraboloid():
  140.     # problems with z axys camera
  141.     a, b, c = map(float, input("Введите коэффициенты a, b и c через пробел: ").split())
  142.     u = np.linspace(0, 2 * np.pi, 100)
  143.     v = np.linspace(0, 2, 100)
  144.     t, r = np.meshgrid(u, v)
  145.  
  146.     rx = r * a
  147.     ry = r * b
  148.     rz = r * c
  149.  
  150.     axys = input("Введите направляющую ось x, y или z: ")
  151.     axys.lower()
  152.     while axys != "x" and axys != "y" and axys != "z":
  153.         axys = input("Ошибка, введите x, y или z: ")
  154.     if axys == "z":
  155.         x = rx * np.cos(t)
  156.         y = ry * np.sin(t)
  157.         z = rz ** 2
  158.  
  159.     elif axys == "y":
  160.         z = rx * np.cos(t)
  161.         y = ry * np.sin(t)
  162.         x = rz ** 2
  163.  
  164.     elif axys == "x":
  165.         x = rx * np.cos(t)
  166.         z = ry * np.sin(t)
  167.         y = rz ** 2
  168.  
  169.     ax.plot_wireframe(x, y, z, rstride=10, cstride=10)
  170.     max_radius = (2 * max(a, b, c)) ** 2
  171.     for axis in 'xyz':
  172.         getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  173.  
  174. def hyperbolic_paraboloid():
  175.     # not sure
  176.     a, b = map(float, input("Введите коэффициенты a и b через пробел: ").split())
  177.     x = np.linspace(-2, 2, 100)
  178.     y = np.linspace(-2, 2, 100)
  179.     x, y = np.meshgrid(x, y)
  180.     z = (x ** 2 / a ** 2) - (y ** 2 / b ** 2)
  181.  
  182.     ax.plot_wireframe(x, y, z, rstride=10, cstride=10)
  183.  
  184.  
  185. def elliptic_cylinder():
  186.     # DONE!
  187.     a, b = map(float, input("Введите коэффициенты a и b через пробел: ").split())
  188.     axys = input("Введите направляющую ось x, y или z: ")
  189.     axys.lower()
  190.     while axys != "x" and axys != "y" and axys != "z":
  191.         axys = input("Ошибка, введите x, y или z: ")
  192.     if axys == "z":
  193.         x = np.linspace(-2, 2, 100)
  194.         z = np.linspace(0, 10, 100)
  195.         x, z = np.meshgrid(x, z)
  196.         y = np.sqrt(2 ** 2 - x ** 2)
  197.  
  198.         x *= a / 2
  199.         y *= b / 2
  200.         z /= 10
  201.  
  202.         ax.plot_wireframe(x, y, z, rstride=10, cstride=10)
  203.         ax.plot_wireframe(x, -y, z, rstride=10, cstride=10)
  204.  
  205.         max_radius = max(a, b, 1)
  206.         for axis in 'xyz':
  207.             getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  208.         ax.set_zlim(0, 1)
  209.  
  210.     elif axys == "x":
  211.         x = np.linspace(-2, 2, 100)
  212.         y = np.linspace(0, 10, 100)
  213.         x, y = np.meshgrid(x, y)
  214.         z = np.sqrt(2 ** 2 - x ** 2)
  215.  
  216.         x *= a / 2
  217.         z *= b / 2
  218.         y /= 10
  219.  
  220.         ax.plot_wireframe(x, y, z, rstride=10, cstride=10)
  221.         ax.plot_wireframe(x, y, -z, rstride=10, cstride=10)
  222.  
  223.         max_radius = max(a, b, 1)
  224.         for axis in 'xyz':
  225.             getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  226.         ax.set_ylim(0, 1)
  227.     elif axys == "y":
  228.         y = np.linspace(-2, 2, 100)
  229.         x = np.linspace(0, 10, 100)
  230.         y, x = np.meshgrid(y, x)
  231.         z = np.sqrt(2 ** 2 - y ** 2)
  232.  
  233.         y *= a / 2
  234.         z *= b / 2
  235.         x /= 1
  236.  
  237.         ax.plot_wireframe(x, y, z, rstride=10, cstride=10)
  238.         ax.plot_wireframe(x, y, -z, rstride=10, cstride=10)
  239.  
  240.         max_radius = max(a, b, 1)
  241.         for axis in 'xyz':
  242.             getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  243.         ax.set_xlim(0, 1)
  244.  
  245.  
  246. def hyperbolic_cylinder():
  247.     # DONE!
  248.     a, b = map(float, input("Введите коэффициенты a и b через пробел: ").split())
  249.     coefs = (a, b)
  250.     rx, ry = coefs
  251.     u = np.linspace(-2, 2, 100)
  252.     v = np.linspace(-2, 2, 100)
  253.     [u, v] = np.meshgrid(u, v)
  254.     axys = input("Введите направляющую ось x, y или z: ")
  255.     axys.lower()
  256.     while axys != "x" and axys != "y" and axys != "z":
  257.         axys = input("Ошибка, введите x, y или z: ")
  258.     if axys == "z":
  259.         x = b * np.sinh(u)
  260.         y = a * np.cosh(u)
  261.         z = v
  262.  
  263.         ax.plot_wireframe(x, y, z, rstride=10, cstride=10)
  264.         ax.plot_wireframe(x, -y, z, rstride=10, cstride=10)
  265.  
  266.         max_radius = max(rx * 4, ry * 4)
  267.         for axis in 'xyz':
  268.             getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  269.         ax.set_zlim(-2, 2)
  270.     elif axys == "x":
  271.         x = b * np.sinh(u)
  272.         z = a * np.cosh(u)
  273.         y = v
  274.  
  275.         ax.plot_wireframe(x, y, z, rstride=10, cstride=10)
  276.         ax.plot_wireframe(x, y, -z, rstride=10, cstride=10)
  277.  
  278.         max_radius = max(rx * 4, ry * 4)
  279.         for axis in 'xyz':
  280.             getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  281.         ax.set_ylim(-2, 2)
  282.     elif axys == "y":
  283.         y = b * np.sinh(u)
  284.         z = a * np.cosh(u)
  285.         x = v
  286.  
  287.         ax.plot_wireframe(x, y, z, rstride=10, cstride=10)
  288.         ax.plot_wireframe(x, y, -z, rstride=10, cstride=10)
  289.  
  290.         max_radius = max(rx * 4, ry * 4)
  291.         for axis in 'xyz':
  292.             getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  293.         ax.set_xlim(-2, 2)
  294.  
  295.  
  296. def parabolic_cylinder():
  297.     a = float(input("Введите коэффициент a: "))
  298.     u = np.linspace(-2, 2, 100)
  299.     v = np.linspace(-2, 2, 100)
  300.     [u, v] = np.meshgrid(u, v)
  301.     axys = input("Введите направляющую ось x, y или z: ")
  302.     axys.lower()
  303.     while axys != "x" and axys != "y" and axys != "z":
  304.         axys = input("Ошибка, введите x, y или z: ")
  305.     if axys == "z":
  306.         x = u**2/np.pi
  307.         y = u
  308.         z = a * v
  309.  
  310.         ax.plot_wireframe(x, y, z, rstride=10, cstride=10)
  311.  
  312.         max_radius = a
  313.         for axis in 'xyz':
  314.             getattr(ax, 'set_{}lim'.format(axis))((-max_radius, max_radius))
  315.         # ax.set_xlim(-2, 2)
  316.  
  317.  
  318.  
  319. def choose_type(type):
  320.     if type == 1:
  321.         ellipsoid()
  322.     elif type == 2:
  323.         oneline_hyperboloid()
  324.     elif type == 3:
  325.         twoline_hyperboloid()
  326.     elif type == 4:
  327.         konus()
  328.     elif type == 5:
  329.         elliptic_paraboloid()
  330.     elif type == 6:
  331.         hyperbolic_paraboloid()
  332.     elif type == 7:
  333.         elliptic_cylinder()
  334.     elif type == 8:
  335.         hyperbolic_cylinder()
  336.     elif type == 9:
  337.         parabolic_cylinder()
  338.  
  339. # Инициализируем фигуру
  340. fig = plt.figure()
  341.  
  342. ax = fig.add_subplot(111, projection='3d')
  343.  
  344. # Выбираем тип поверхности, после чего считываем коэффициенты
  345. print("Пожалуйста, выберите тип поверхности:\n1. Эллипсоид\n2. Однополсоный гиперболоид\n3. Двуполосный "
  346.       "гиперболоид\n4. Коническая поверхность\n5. Эллиптический параболлоид\n6. Гиперболический параболлоид\n7. "
  347.       "Эллиптический цилиндр\n8. Гиперболический цилиндр\n9. Параболлический цилиндр")
  348. type = int(input())
  349.  
  350. choose_type(type)
  351.  
  352. # Настройка отрисовки
  353. ax.set_xlabel("X")
  354. ax.set_ylabel("Y")
  355. ax.set_zlabel("Z")
  356.  
  357. plt.title("")
  358.  
  359. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement