KEMBAR78
Code 4 | PDF
0% found this document useful (0 votes)
18 views2 pages

Code 4

The document contains a Python script that simulates the trajectory of a projectile using Matplotlib. It calculates the motion based on an initial velocity and angle, animating the path and displaying an image of a ball. The animation is saved as a GIF file named 'animasi_bola.gif'.

Uploaded by

FATURRAHMAN 98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Code 4

The document contains a Python script that simulates the trajectory of a projectile using Matplotlib. It calculates the motion based on an initial velocity and angle, animating the path and displaying an image of a ball. The animation is saved as a GIF file named 'animasi_bola.gif'.

Uploaded by

FATURRAHMAN 98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

NAMA : Muhammad febrian faturrahman

KLS : IK-5

NIM : 0701242114

CODE SIMULASI 4

import matplotlib.pyplot as plt


from matplotlib.animation import FuncAnimation, PillowWriter
import matplotlib.image as mpimg
import matplotlib.transforms as transforms
import math
def hitung_lintasan(alfa, t):
Vo = 50
g = 10
cosa = math.cos(alfa * math.pi / 180)
sina = math.sin(alfa * math.pi / 180)
x = Vo * cosa * t
y = Vo * sina * t - 0.5 * g * t**2
return x, y
def PhysProb():
fig, ax = plt.subplots()
ax.set(xlim=(-20, 300), ylim=(-20, 160))
ax.set_aspect(1)
plt.axis('off')
Vo = 50
alfa = 75
g = 10
tmax = 2 * Vo * math.sin(alfa * math.pi / 180) / g
n_frames = 100
t_values = [i * tmax / n_frames for i in range(n_frames)]
x_vals = []
y_vals = []
for t in t_values:
x, y = hitung_lintasan(alfa, t)
x_vals.append(x)
y_vals.append(y)
garis, = ax.plot([], [], color='blue')
# Load gambar PNG sekali saja
img = mpimg.imread('bola.png') # Pastikan file ini ada
def init():
garis.set_data([], [])
return garis,
def update(frame):

ax.clear()
ax.set(xlim=(-20, 300), ylim=(-20, 160))
ax.set_aspect(1)
plt.axis('off')
ax.plot(x_vals[:frame+1], y_vals[:frame+1], color='blue')
x = x_vals[frame]
y = y_vals[frame]
# Sudut rotasi (dalam derajat)
total_rotasi = 360
sudut = frame / (n_frames - 1) * total_rotasi
# Buat transformasi rotasi dan translasi
trans_data = transforms.Affine2D().rotate_deg_around(x, y, sudut).translate(0, 0) +
ax.transData
# Tampilkan gambar sebagai pengganti pemukul
ax.imshow(img, extent=[x-40, x+40, y-30, y+30], transform=trans_data)
anim = FuncAnimation(fig, update, frames=n_frames, init_func=init, interval=100)
anim.save("animasi_bola.gif", writer=PillowWriter(fps=10))
plt.close()
PhysProb()

You might also like