Particles, Verlet cloth, the Game of Life and Boids flocks.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-VS Code. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in VS Code. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
וקטורים בלי pygameVectors without pygame
כתבו מחלקה Vec(x, y) עם __add__, __sub__, __mul__ (במספר), length(), normalized() (ValueError לאורך 0) ו-__repr__ בצורה Vec(3.0, 4.0). ערכים נשמרים כ-float.
Write a Vec(x, y) class with __add__, __sub__, __mul__ (by a number), length(), normalized() (ValueError for length 0) and __repr__ like Vec(3.0, 4.0). Values are stored as floats.
איך הפלט אמור להיראות?What should the output look like?
כתבו fall_time(height, dt, g=9.8) שמסמלצת נפילה מגובה height (מהירות התחלתית 0, אוילר חצי-מרומז) ומחזירה את הזמן (בשניות, מעוגל ל-3) עד שהגובה ≤ 0. השוו לנוסחה המדויקת sqrt(2h / g): ככל ש-dt קטן, הם קרובים יותר.
Write fall_time(height, dt, g=9.8) simulating a fall from height (initial velocity 0, semi-implicit Euler) and returning the time (seconds, rounded to 3) until the height ≤ 0. Compare to the exact formula sqrt(2h / g): the smaller dt, the closer.
איך הפלט אמור להיראות?What should the output look like?
0.1 2.0 0.02
0.01 2.02 0.0
0.001 2.02 0.0
main.py
רמזHint 1:v += g * dt; y -= v * dt; t += dtv += g * dt; y -= v * dt; t += dt
רמזHint 2:לולאת while עד y <= 0.A while loop until y <= 0.
כתבו spring_steps(k, damping, x0, steps, dt=1/60) שמחזירה רשימת מיקומים (חוק הוק, v += -k*x*dt; v *= damping; x += v*dt), ו-settle_time(k, damping, x0, tolerance=1.0, dt=1/60) שמחזירה את מספר הצעד הראשון שאחריו |x| נשאר קטן מ-tolerance ב-60 הצעדים הבאים (עד 5000), או None.
Write spring_steps(k, damping, x0, steps, dt=1/60) returning a list of positions (Hooke's law, v += -k*x*dt; v *= damping; x += v*dt), and settle_time(k, damping, x0, tolerance=1.0, dt=1/60) returning the first step after which |x| stays below tolerance for the next 60 steps (up to 5000), or None.
איך הפלט אמור להיראות?What should the output look like?
453 165 None
main.py
רמזHint 1:מריצים 5060 צעדים פעם אחת ובודקים חלונות.Run 5060 steps once and check windows.
רמזHint 2:all(abs(v) < tolerance for v in xs[i:i + 60])all(abs(v) < tolerance for v in xs[i:i + 60])
כתבו orbit_drift(dt, steps, GM=4000.0, r=100.0) שמסמלצת כוכב לכת במסלול מעגלי (מתחיל ב-(0, -r) עם מהירות sqrt(GM/r) בציר x, שמש ב-(0,0)) ומחזירה את השינוי היחסי באנרגיה (|E_end - E_start| / |E_start|), כש-E = 0.5·v² - GM/r. dt קטן אמור לתת סחיפה קטנה יותר.
Write orbit_drift(dt, steps, GM=4000.0, r=100.0) simulating a planet on a circular orbit (starting at (0, -r) with speed sqrt(GM/r) along x, the sun at (0,0)) and returning the relative energy change (|E_end - E_start| / |E_start|), where E = 0.5·v² - GM/r. A smaller dt should drift less.
איך הפלט אמור להיראות?What should the output look like?
0.5 0.00000
0.1 0.00000
0.02 0.00000
main.py
רמזHint 1:עבודה עם x, y, vx, vy רגילים.Work with plain x, y, vx, vy.
Physics Lab: עולם כדוריםPhysics Lab: a world of balls
פרויקט מלווהCompanion project
כתבו מחלקה World(w, h, r=9, gravity=0.25, damping=0.999) עם add(x, y, vx, vy) ו-step(): לכל כדור כבידה, ריסון ותנועה, הקפצה מהקירות (×0.9) ומהרצפה (×0.85), והתנגשויות בין כדורים (החלפת רכיבי מהירות לאורך הקו + הרחקה). kinetic() מחזירה את סכום 0.5·v² מעוגל ל-2. כדור הוא רשימה [x, y, vx, vy].
Write a World(w, h, r=9, gravity=0.25, damping=0.999) class with add(x, y, vx, vy) and step(): gravity, damping and motion for each ball, bounces off walls (×0.9) and the floor (×0.85), and ball-to-ball collisions (swap velocity components along the line + separate). kinetic() returns the sum of 0.5·v² rounded to 2. A ball is a list [x, y, vx, vy].
איך הפלט אמור להיראות?What should the output look like?
[-2.0, 2.0] 3.99
physics_lab.py
רמזHint 1:ההתנגשות כמו באתגר של מפגש 37.The collision as in session 37's challenge.
רמזHint 2:מחליפים רק אם הם מתקרבים (swap < 0).Swap only if they're approaching (swap < 0).
אוילר צובר שגיאות. כתבו simulate(method, dt, t_end) לנפילה חופשית עם התנגדות אוויר (a = g - c·v², g=9.8, c=0.05) שמחזירה את המהירות ב-t_end, כש-method הוא "euler" או "rk4". ב-RK4 מחשבים ארבעה שיפועים: k1 = f(v), k2 = f(v + k1·dt/2), k3 = f(v + k2·dt/2), k4 = f(v + k3·dt) ו-v += dt·(k1 + 2k2 + 2k3 + k4)/6. המהירות הסופית המדויקת היא sqrt(g/c).
Euler accumulates errors. Write simulate(method, dt, t_end) for a free fall with air drag (a = g - c·v², g=9.8, c=0.05) returning the velocity at t_end, where method is "euler" or "rk4". In RK4 compute four slopes: k1 = f(v), k2 = f(v + k1·dt/2), k3 = f(v + k2·dt/2), k4 = f(v + k3·dt) and v += dt·(k1 + 2k2 + 2k3 + k4)/6. The exact terminal velocity is sqrt(g/c).
איך הפלט אמור להיראות?What should the output look like?
euler error 0.424475
rk4 error 0.000228
14.0 14.0
main.py
רמזHint 1:f = lambda v: 9.8 - 0.05 * v * vf = lambda v: 9.8 - 0.05 * v * v
כתבו chain(n, steps, k=30, damping=0.98, rest=20, dt=1/60, g=9.8): שרשרת של n משקולות תלויות זו מתחת לזו מנקודה קבועה ב-y=0, מחוברות בקפיצים באורך מנוחה rest. כל משקולת (מסה 1) מושפעת מכבידה ומשני הקפיצים שלה. מחזירה את מיקומי y של המשקולות (מעוגלים ל-1) אחרי steps צעדים. במנוחה, כל קפיץ נמתח לפי המשקל שמתחתיו.
Write chain(n, steps, k=30, damping=0.98, rest=20, dt=1/60, g=9.8): a chain of n weights hanging one below another from a fixed point at y=0, joined by springs of rest length rest. Each weight (mass 1) feels gravity and its two springs. Return the weights' y positions (rounded to 1) after steps steps. At rest, each spring stretches by the weight below it.
איך הפלט אמור להיראות?What should the output look like?
[21.0, 41.6, 62.0] [21.0, 41.6, 62.0]
main.py
רמזHint 1:כוח קפיץ בין a ל-b: k * ((yb - ya) - rest).Spring force between a and b: k * ((yb - ya) - rest).
רמזHint 2:הנקודה העליונה קבועה: y=0, לא זזה.The top point is fixed: y=0, never moves.