Advertisement
Guest User

lab_1

a guest
Feb 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from sys import stdin, exit as sys_exit
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5.  
  6. class graphic_object:
  7.    
  8.     def __init__(self, object_matrix):
  9.         self.object_matrix = object_matrix
  10.         self.points_count = self.object_matrix.shape[0]
  11.            
  12.     def __mul__(self, other):
  13.         return graphic_object(self.object_matrix.dot(other))
  14.                
  15.     def __rmul__(self, other):
  16.         return graphic_object(self.object_matrix.dot(other))
  17.                
  18.     def draw(self):
  19.         m = self.object_matrix
  20.         print(m[1,2])
  21.         for i in range(self.points_count - 1):
  22.             plt.plot([m[i, 0], m[i + 1, 0]], [m[i, 1], m[i+1, 1]])
  23.         plt.plot([m[self.points_count - 1, 0], m[0, 0]], [m[0, 1], m[self.points_count - 1, 1]])
  24.     plt.axis([-10, 10, -10, 10])
  25.     ax = plt.gca()
  26.     ax.spines['left'].set_position('center')
  27.     ax.spines['bottom'].set_position('center')
  28.     ax.spines['top'].set_visible(False)
  29.     ax.spines['right'].set_visible(False)
  30.         plt.show()
  31.  
  32.        
  33.     def map_axis_x(self):
  34.         mapping_matrix = np.matrix(
  35.             [[-1, 0, 0],
  36.              [0, 1, 0],
  37.              [0, 0, 1]])
  38.         return self*mapping_matrix
  39.      
  40.     def map_axis_y(self):
  41.         mapping_matrix = np.matrix(
  42.             [[1, 0, 0],
  43.              [0, -1, 0],
  44.              [0, 0, 1]])
  45.         return self*mapping_matrix
  46.      
  47.     def scale(self, x_scale, y_scale):
  48.         scale_matrix = np.matrix(
  49.             [[x_scale, 0, 0],
  50.              [0, y_scale, 0],
  51.              [0,0,1]])
  52.         return self * scale_matrix
  53.        
  54.  
  55. x = graphic_object(np.matrix(
  56.             [[1, 3, 1],
  57.              [2, 2, 1],
  58.              [0, 1, 1],
  59.              [1, 4, 1]]))
  60.  
  61. x.draw()
  62. answ = 0
  63. z = graphic_object(np.matrix([]))
  64. z = x
  65. while answ!=5:
  66.     print ("""Выберите действие:
  67.     1: отобразить по оси X без изменения масштаба
  68.     2: отобразить по оси Y без изменения масштаба
  69.     3: отобразить по оси X c изменением масштаба
  70.     4: отобразить по оси Y с изменением масштаба  
  71.     5: завершить работу""")
  72.     answ = int(input())
  73.     if answ == 1:
  74.         y = z.map_axis_x()
  75.     if answ == 2:
  76.         y = z.map_axis_y()
  77.     if answ == 3:
  78.         y = z.map_axis_x()
  79.         print ('Введите изменение масштаба по оси X')
  80.         sc_x = float(input())
  81.         print ('Введите изменение масштаба по оси Y')
  82.         sc_y = float(input())
  83.         y = z.scale(sc_x,sc_y)
  84.     if answ == 4:
  85.         y = z.map_axis_y()
  86.         print ('Введите изменение масштаба по оси X')
  87.         sc_x = float(input())
  88.         print ('Введите изменение масштаба по оси Y')
  89.         sc_y = float(input())
  90.         y = z.scale(sc_x,sc_y)
  91.     y.draw()
  92.     z = y
  93. sys_exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement