# Tactiverge Academy — companion code for "Linkages and mechanisms"
# Lesson: https://tactiverge.win/academy/mechanical-design/mechanical-systems/linkages-and-mechanisms/
# Licence: MIT (see https://tactiverge.win/open-engineering/). Examples appear in lesson order.

# ── Example 1 ────────────────────────────────────────
import math

def grashof(links):
    """Return True if the shortest link can rotate fully (Grashof condition)."""
    s, p, q, l = sorted(links)
    return s + l <= p + q

def slider_position(r, L, theta_deg):
    """Slider-crank: distance from crank pivot to slider pin, in the same units as r and L."""
    t = math.radians(theta_deg)
    return r * math.cos(t) + math.sqrt(L**2 - (r * math.sin(t))**2)

print(grashof([2, 7, 6, 8]))    # links in cm
print(grashof([3, 9, 4, 5]))
for angle in (0, 90, 180):
    print(angle, round(slider_position(3, 10, angle), 2))
