Advertisement
qberik

Untitled

Oct 8th, 2022
1,478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.57 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from math import *
  4.  
  5. # Настройки
  6.  
  7. # количество сегментов в линиях
  8. # чем больше чем точнее и тем медленнее
  9. number_of_segments = 100
  10.  
  11. # цвета
  12. fill_color = 'blue'
  13. point_in_set_color = 'green'
  14. point_not_in_set_color = 'red'
  15.  
  16.  
  17. ##########
  18. # этап 1 #
  19. ##########
  20. # считываем точку
  21.  
  22. px,py = map( float, input('Введите координаты точки: ' ).split() )
  23.  
  24. # определяем в множестве ли точка?
  25. is_poing_in_set = ( ( ( abs(px)<=5 and abs(py)<=1 ) |
  26.                    ( abs(py)<=5 and abs(px)<=1 ) ) ^
  27.                    ( px**2 + py**2 <= 16 ) )
  28.  
  29. # выбор цвета в зависимости он местонахождения
  30. point_color = point_in_set_color if is_poing_in_set else point_not_in_set_color
  31.  
  32. # печать сообщения о местонахождении
  33. if is_poing_in_set:
  34.     print( "Точка находится во множестве" )
  35. else:
  36.     print( "Точка во множестве нет" )
  37.  
  38. ##########
  39. # этап 2 #
  40. ##########
  41. # рисуем границы графиков или чёрные полоски
  42.  
  43. # для сохранения соотношения сторон графика
  44. fig = plt.figure()
  45. ax = fig.add_subplot(111,aspect='equal')
  46.  
  47. # функция, которая по заданному фи даёт координаты x,y
  48. # для окружности с заданным адрессом
  49. def cirlce_xy(r,phi):
  50.   return r*np.cos(phi), r*np.sin(phi)
  51.  
  52. # диапазон углов от нуля до 2 пи с шагом 0.1
  53. phis=np.arange(0, pi * 2 ,0.01)
  54. r = 4
  55. ax.plot( *cirlce_xy(r,phis), c='k',ls='-' )
  56.  
  57.  
  58. # функция которая рисует линию
  59. def line( point1, point2 ):
  60.     ax.plot(   np.linspace( point1[0], point2[0], number_of_segments), # иксы
  61.                 np.linspace( point1[1], point2[1], number_of_segments), # игрики
  62.         color='k' ) # чёрный цвет
  63.  
  64.  
  65. # ресуем крест по координатам
  66. line( ( -5, 1 ), ( -1,  1)  )
  67. line( (  5, 1 ), (  1,  1)  )
  68.  
  69. line( ( -5,-1 ), ( -1, -1)  )
  70. line( (  5,-1 ), (  1, -1)  )
  71.  
  72. line( ( 1, -5 ), ( 1, -1 )  )
  73. line( ( 1,  5 ), ( 1,  1 )  )
  74.  
  75. line( (-1, -5 ), (-1, -1 )  )
  76. line( (-1,  5 ), (-1,  1 )  )
  77.  
  78. line( ( -1, 5 ), ( 1, 5 ) )
  79. line( ( -1,-5 ), ( 1,-5 ) )
  80.  
  81. line( ( 5, -1 ), ( 5, 1 ) )
  82. line( (-5, -1 ), (-5, 1 ) )
  83.  
  84.  
  85. ##########
  86. # этап 3 #
  87. ##########
  88. # заливка областей принадлежащих области
  89.  
  90. # сначала заливаем 4 внутренних сегмента круга
  91.  
  92. # верхний правый
  93. x = np.linspace( 1, r * cos( 1 /r ), number_of_segments )
  94. y = np.linspace( 1, 1, number_of_segments )
  95. y1 = np.cos( np.arcsin( x / r  ) ) * r
  96. plt.fill_between( x, y, y1, color=fill_color)
  97.  
  98. # нижний правый
  99. x = np.linspace( 1, r * cos( 1 /r ), number_of_segments )
  100. y = np.linspace(-1,-1, number_of_segments )
  101. y1 =-np.cos( np.arcsin( x / r  ) ) * r
  102. plt.fill_between( x, y, y1, color=fill_color)
  103.  
  104. # верхний левый
  105. x = np.linspace(  -r * cos( 1 /r ), -1 , number_of_segments )
  106. y = np.linspace( 1, 1, number_of_segments )
  107. y1 = np.cos( np.arcsin( x / r  ) ) * r
  108. plt.fill_between( x, y, y1, color=fill_color)
  109.  
  110. # нижний левый
  111. x = np.linspace( -r * cos( 1 /r ), -1, number_of_segments )
  112. y = np.linspace(-1,-1, number_of_segments )
  113. y1 =-np.cos( np.arcsin( x / r  ) ) * r
  114. plt.fill_between( x, y, y1, color=fill_color)
  115.  
  116.  
  117. # теперь рисуем части креста вне окружности
  118.  
  119. # верхний
  120. x = np.linspace( -1, 1, number_of_segments )
  121. y = np.linspace( 5, 5, number_of_segments )
  122. y1 = np.cos( np.arcsin( x / r  ) ) * r
  123. plt.fill_between( x, y, y1, color=fill_color)
  124.  
  125. # нижний
  126. x = np.linspace( -1, 1, number_of_segments )
  127. y = np.linspace(-5,-5, number_of_segments )
  128. y1 =-np.cos( np.arcsin( x / r  ) ) * r
  129. plt.fill_between( x, y, y1, color=fill_color)
  130.  
  131.  
  132. # верхний
  133. x = np.linspace( -1, 1, number_of_segments )
  134. y = np.linspace( 5, 5, number_of_segments )
  135. y1 = np.cos( np.arcsin( x / r  ) ) * r
  136. plt.fill_betweenx( x, y, y1, color=fill_color)
  137.  
  138. # нижний
  139. x = np.linspace( -1, 1, number_of_segments )
  140. y = np.linspace(-5,-5, number_of_segments )
  141. y1 =-np.cos( np.arcsin( x / r  ) ) * r
  142. plt.fill_betweenx( x, y, y1, color=fill_color)
  143.  
  144. ##########
  145. # этап 4 #
  146. ##########
  147. # рисуем точку
  148. plt.plot( px ,py, color=point_color, marker = 'o', markersize = 10 )
  149.  
  150. plt.show()
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement