Défi : Transformations Linéaires des Figures
Tâche
Swipe to start coding
Les transformations linéaires des figures sont couramment utilisées en infographie. Il existe 2 principaux types de transformations linéaires :
- Transformation de rotation qui fait pivoter une figure autour d'un point ou d'un axe spécifique.
- Transformation d'échelle qui redimensionne une figure en modifiant sa taille le long de chaque axe.
Votre tâche est d'appliquer toutes ces transformations à un rectangle une par une. En conséquence, nous aurons une composition de transformations :
- Créez une matrice de rotation qui fait pivoter une figure de
np.pi / 3
degrés. - Créez une matrice de mise à l'échelle avec les paramètres
scale_x = 2
etscale_y = 0.5
. - Appliquez la
rotation_matrix
au carré. - Appliquez la
scaling_matrix
au résultat de la transformation précédente.
Solution
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
import matplotlib.pyplot as plt
# Function to construct a rotation matrix
def rotation_matrix(angle):
cos_theta = np.cos(angle)
sin_theta = np.sin(angle)
return np.array([[cos_theta, -sin_theta],
[sin_theta, cos_theta]])
# Function to construct a scaling matrix
def scaling_matrix(scale_x, scale_y):
return np.array([[scale_x, 0],
[0, scale_y]])
# Function to plot a square
def plot_square(square, color):
square = np.vstack((square, square[0])) # Close the square by repeating the first point
plt.plot(square[:, 0], square[:, 1], color=color)
# Define a square using its corners as a matrix
square = np.array([
[0, 0],
[1, 0],
[1, 1],
[0, 1],
[0, 0] # Close the square
]).T # Transpose to have x and y as separate rows
# Plot the original square
plot_square(square.T, 'blue')
# Define the rotation angle and scaling factors
angle = np.pi / 3
scale_x, scale_y = 2, 0.5
Tout était clair ?
Merci pour vos commentaires !
Section 2. Chapitre 5
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
import matplotlib.pyplot as plt
# Function to construct a rotation matrix
def rotation_matrix(angle):
cos_theta = np.cos(angle)
sin_theta = np.sin(angle)
return np.array([[cos_theta, -sin_theta],
[sin_theta, cos_theta]])
# Function to construct a scaling matrix
def scaling_matrix(scale_x, scale_y):
return np.array([[scale_x, 0],
[0, scale_y]])
# Function to plot a square
def plot_square(square, color):
square = np.vstack((square, square[0])) # Close the square by repeating the first point
plt.plot(square[:, 0], square[:, 1], color=color)
# Define a square using its corners as a matrix
square = np.array([
[0, 0],
[1, 0],
[1, 1],
[0, 1],
[0, 0] # Close the square
]).T # Transpose to have x and y as separate rows
# Plot the original square
plot_square(square.T, 'blue')
# Define the rotation angle and scaling factors
angle = ___
scale_x, scale_y = ___, ___
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion