Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Figures' Linear Transformations | Linear Algebra
Mathematics for Data Analysis and Modeling

bookChallenge: Figures' Linear Transformations

Tarea
test

Swipe to show code editor

Linear transformations of the figures are commonly used in computer graphics. There are 2 main types of linear transformations:

  1. Rotation transformation rotates a figure around a specific point or axis.
  2. Scale transformation resizes a figure by changing its size along each axis.

Your task is to apply all these transformations to a rectangle one by one. As a result, we will have a composition of transformations:

  1. Сreate rotation matrix that rotates a figure by np.pi / 3 degrees.
  2. Create a scaling matrix with the parameters scale_x = 2 and scale_y = 0.5.
  3. Apply the rotation_matrix to the square.
  4. Apply the scaling_matrix to the result of the previous transformation.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 5
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 = ___, ___

# Construct the rotation and scaling matrices
rotation = rotation_matrix(angle)
scaling = scaling_matrix(scale_x, scale_y)

# Apply the transformations to the square
rotated_square = rotation @ ___
transformed_square = scaling @ ___

# Plot the transformed square
plot_square(transformed_square.T, 'red')

# Set the aspect ratio to equal and adjust the axis limits
plt.gca().set_aspect('equal', adjustable='box')
plt.xlim(-3, 3)
plt.ylim(-3, 3)
toggle bottom row
some-alt