from pysketcher import *

H = 7.
W = 6.

drawing_tool.set_coordinate_system(xmin=0, xmax=W,
                                   ymin=0, ymax=H,
                                   axis=True)
drawing_tool.set_linecolor('blue')
#drawing_tool.set_grid(True)

def set_dashed_thin_blackline(*objects):
    """Set linestyle of an object to dashed, black, width=1."""
    for obj in objects:
        obj.set_linestyle('dashed')
        obj.set_linecolor('black')
        obj.set_linewidth(1)


L = 5*H/7          # length
P = (W/6, 0.85*H)  # rotation point
a = 40             # angle

path = Arc(P, L, -90, a)
angle = Arc_wText(r'$\theta$', P, L/4, -90, a, text_spacing=1/30.)
vertical = Line(P, P-point(0,L))

rod = Line(P, P + L*point(sin(radians(a)), -cos(radians(a))))
# or shorter (and more reliable)
mass_pt = path.end
rod = Line(P, mass_pt)

mass = Circle(center=mass_pt, radius=L/20.)
mass.set_filled_curves(color='blue')
mass_symbol = Text('$m$', mass_pt + L/10*unit_vec(rod.end-rod.start))

length = Distance_wText(P, mass_pt, '$L$')
# Displace length indication
length.translate(L/15*point(cos(radians(a)), sin(radians(a))))
gravity = Gravity(start=P+point(0.8*L,0), length=L/3)

set_dashed_thin_blackline(vertical, path)

dims = Compose({'vertical': vertical, 'theta': angle, 'path': path,
                'g': gravity, 'L': length, 'm': mass_symbol})

fig = Compose({'body': mass, 'rod': rod, 'dims': dims})

#drawing_tool.ax.set_xlim(4,10)
#drawing_tool.ax.set_ylim(1,8)
fig.draw()
drawing_tool.display()
drawing_tool.savefig('tmp_pendulum1.png')

# Draw body diagram
#raw_input('Press Return to make body diagram: ')
import time; time.sleep(2)
drawing_tool.erase()

drawing_tool.set_linecolor('black')
mg_force = Force(mass_pt, mass_pt + L/3*point(0,-1), '$mg$', text_pos='end')
rod_force = Force(mass_pt, mass_pt + L/3*unit_vec(rod.start-rod.end),
                  '$S$', text_pos='end')
vertical2 = Line(rod_force.end, rod_force.end + point(0,-L/3))
set_dashed_thin_blackline(vertical2)
angle2 = Arc_wText(r'$\theta$', rod_force.end, L/6, -90, a, text_spacing=1/30.)
path2 = Arc(P, L, -90+a-a/2., a)
path2.set_arrow('<-')
path2.set_linestyle('dashed')

body_diagram = Compose({'mg': mg_force, 'S': rod_force,
                        'vertical': vertical2, 'theta': angle2,
                        'path': path2, 'body': mass, 'm': mass_symbol})
body_diagram.draw()
drawing_tool.display('Body diagram')
drawing_tool.savefig('tmp_pendulum2.png')

time.sleep(1)
drawing_tool.adjust_coordinate_system(body_diagram.minmax_coordinates(), 90)
drawing_tool.display('Body diagram')
drawing_tool.savefig('tmp_pendulum3.png')

raw_input()
