# L-18 MCS 507 Mon 2 Oct 2023 : fourbarforms.py

"""
We use sympy to produce the solution to drive a planar fourbar mechanisms.

The crank C of length L and angle t has coordinates (L*cos(t),L*sin(t)).  
The point D(x,y) is at distance r from B(a,0) => the equation f,
f = (x-a)^2 + y^2 - r^2 = 0.
The point D(x,y) is at distance R from C(L*cos(t),L*sin(t)) => 
the equation g, g = (x - L*cos(t))^2 + (y - L*sin(t))^2 - R^2 = 0.
Replacing cos(t) and sin(t) by c and s respectively, adding
h = c^2 + s^2 - 1 = 0 we obtain a system of algebraic equations
in (x,y) and with parameters (a,r,R,L).
"""

import sympy as sp
from sympy import sin, cos
sp.var('a,x,y,r,R,L,t,c,s')
f = (x-a)**2 + y**2 - r**2;
g = (x-L*cos(t))**2 + (y-L*sin(t))**2 - R**2;
gcs = sp.Subs(g,(cos(t),sin(t)),(c,s)).doit()
h = c**2 + s**2 - 1
print('f =', f)
print('g =', g)
print('gcs =', gcs)
print('h =', h)
F = [f,gcs,h]
# xsol0 = sp.solve(F,x)
# This crashes: only zero dimensional Groebner bases are supported...
# But then the results below are not really meaningful?
gb = sp.groebner(F,order='lex')
print(gb)
