Tugas 5 - Transformasi Matriks#

Transformasi matriks adalah operasi yang mengubah vektor dalam ruang menggunakan matriks. Contoh umum transformasi termasuk rotasi, skala, refleksi, dan translasi.

Jenis-jenisTransformasi Matriks#

1. Rotasi#

Rotasi matriks mengubah arah vektor dengan memutar vektor di sekitar titik asal. Untuk rotasi 2D dengan sudut θ:

\[\begin{split} R(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{pmatrix} \end{split}\]
import numpy as np

def rotate_vector(v, theta):
    theta = np.radians(theta)
    R = np.array([
        [np.cos(theta), -np.sin(theta)],
        [np.sin(theta), np.cos(theta)]
    ])
    return R @ v

# Contoh penggunaan
v = np.array([1, 0])
rotated_v = rotate_vector(v, 90)
print("Vektor asli:", v)
print("Vektor setelah rotasi 90 derajat:", rotated_v)

2. scaling#

Skala matriks mengubah ukuran vektor dengan memperbesar atau memperkecil vektor.

\[\begin{split} S = \begin{pmatrix} s_x & 0 \\ 0 & s_y \end{pmatrix} \end{split}\]

Di mana \((s_x)\) dan \((s_y)\) adalah faktor skala sepanjang sumbu-x dan sumbu-y.

def scale_vector(v, sx, sy):
    S = np.array([
        [sx, 0],
        [0, sy]
    ])
    return S @ v

# Contoh penggunaan
v = np.array([1, 1])
scaled_v = scale_vector(v, 2, 3)
print("Vektor asli:", v)
print("Vektor setelah skala (2, 3):", scaled_v)

3. Refleksi (Reflection)#

Refleksi matriks mengubah vektor dengan mencerminkan vektor terhadap sumbu tertentu.

Rumus (Refleksi terhadap sumbu-x): $\( F_x = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix} \)$

Rumus (Refleksi terhadap sumbu-y): $\( F_y = \begin{pmatrix} -1 & 0 \\ 0 & 1 \end{pmatrix} \)$

def reflect_vector(v, axis='x'):
    if axis == 'x':
        M = np.array([
            [1, 0],
            [0, -1]
        ])
    elif axis == 'y':
        M = np.array([
            [-1, 0],
            [0, 1]
        ])
    else:
        raise ValueError("Axis must be 'x' or 'y'")
    return M @ v

# Contoh penggunaan
v = np.array([1, 1])
reflected_v_x = reflect_vector(v, 'x')
reflected_v_y = reflect_vector(v, 'y')
print("Vektor asli:", v)
print("Vektor setelah refleksi terhadap sumbu-x:", reflected_v_x)
print("Vektor setelah refleksi terhadap sumbu-y:", reflected_v_y)

4. Translasi (Translation)#

Translasi matriks menggeser vektor dengan menambahkan vektor translasi. Ini tidak dapat dilakukan dengan matriks 2x2 biasa, melainkan menggunakan matriks homogen 3x3.

def translate_vector(v, tx, ty):
    v_homogeneous = np.array([v[0], v[1], 1])
    T = np.array([
        [1, 0, tx],
        [0, 1, ty],
        [0, 0, 1]
    ])
    translated_v = T @ v_homogeneous
    return translated_v[:2]

# Contoh penggunaan
v = np.array([1, 1])
translated_v = translate_vector(v, 3, 2)
print("Vektor asli:", v)
print("Vektor setelah translasi (3, 2):", translated_v)

5. Transformasi Geser (Shear)#

Transformasi geser mengubah bentuk vektor dengan menggesernya sepanjang sumbu-x atau sumbu-y.

Rumus (Shear horizontal): $\( Sh_x = \begin{pmatrix} 1 & k_x \\ 0 & 1 \end{pmatrix} \)$

Rumus (Shear vertikal): $\( Sh_y = \begin{pmatrix} 1 & 0 \\ k_y & 1 \end{pmatrix} \)\( Di mana \)(k_x)\( dan \)(k_y)$ adalah faktor geser horizontal dan vertikal.

import numpy as np

# Define the vector
v = np.array([1, 1])

# Define the rotation matrix for 90 degrees clockwise
theta = np.radians(90)
R = np.array([[np.cos(theta), -np.sin(theta)],
              [np.sin(theta), np.cos(theta)]])

# Apply rotation
v_rotated = R @ v

# Define the scaling matrix
S = np.array([[2, 0],
              [0, 3]])

# Apply scaling
v_scaled = S @ v_rotated

print("Original vector:", v)
print("Rotated vector:", v_rotated)
print("Scaled vector:", v_scaled)
Original vector: [1 1]
Rotated vector: [-1.  1.]
Scaled vector: [-2.  3.]
import numpy as np
import matplotlib.pyplot as plt

def plot_vectors(vectors, colors, labels, title):
    plt.figure()
    for vector, color, label in zip(vectors, colors, labels):
        plt.scatter(vector[0], vector[1], color=color, label=label)
        plt.quiver(0, 0, vector[0], vector[1], angles='xy', scale_units='xy', scale=1, color=color)
    plt.xlim(-10, 10)
    plt.ylim(-10, 10)
    plt.axhline(0, color='grey', lw=0.5)
    plt.axvline(0, color='grey', lw=0.5)
    plt.grid(True)
    plt.legend()
    plt.title(title)
    plt.show()

def rotate_vector(v, theta):
    theta = np.radians(theta)
    R = np.array([
        [np.cos(theta), -np.sin(theta)],
        [np.sin(theta), np.cos(theta)]
    ])
    return R @ v

def scale_vector(v, sx, sy):
    S = np.array([
        [sx, 0],
        [0, sy]
    ])
    return S @ v

def reflect_vector(v, axis='x'):
    if axis == 'x':
        M = np.array([
            [1, 0],
            [0, -1]
        ])
    elif axis == 'y':
        M = np.array([
            [-1, 0],
            [0, 1]
        ])
    else:
        raise ValueError("Axis must be 'x' or 'y'")
    return M @ v

def translate_vector(v, tx, ty):
    v_homogeneous = np.array([v[0], v[1], 1])
    T = np.array([
        [1, 0, tx],
        [0, 1, ty],
        [0, 0, 1]
    ])
    translated_v = T @ v_homogeneous
    return translated_v[:2]

# Contoh penggunaan dan visualisasi
v = np.array([3, 4])

# Rotasi
theta = 45
v_rotated = rotate_vector(v, theta)
plot_vectors([v, v_rotated], ['red', 'blue'], ['Ori', f'Rotasi {theta}'], f'Rotasi {theta} Derajat')
_images/92a696849b211509ad9ea43dedbb9162a0b9d16d38fffd2cf3e6af97b4e227d9.png
# Skala
sx, sy = 2, 3
v_scaled = scale_vector(v, sx, sy)
plot_vectors([v, v_scaled], ['red', 'blue'], ['Ori', f'Skala ({sx}, {sy})'], f'Skala ({sx}, {sy})')
_images/5a021e4c19df2fa505e2242e2d1297933214b2482c7dc743122a91bfa3fa5cde.png
# Refleksi
v_reflected_x = reflect_vector(v, 'x')
v_reflected_y = reflect_vector(v, 'y')
plot_vectors([v, v_reflected_x], ['red', 'blue'], ['Ori', 'Refleksi x'], 'Refleksi Terhadap Sumbu-x')
plot_vectors([v, v_reflected_y], ['red', 'blue'], ['Ori', 'Refleksi y'], 'Refleksi Terhadap Sumbu-y')
_images/56b863a492cac49b07b8c243568f51d0674c46cfb7c0cdfe9f7b94f6e37f3044.png _images/f804b9972c1c8098263d7bac55fbf6004076291916b333119eefcb84116753b9.png
# Translasi
tx, ty = 3, 2
v_translated = translate_vector(v, tx, ty)
plot_vectors([v, v_translated], ['red', 'blue'], ['Ori', f'Translasi ({tx}, {ty})'], f'Translasi ({tx}, {ty})')
_images/c371ef3f14c6b7d0f2fe74c419c084e1e5d76fc740f954f1bf91865cabfae561.png