// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(.6);

// Global code will be evaluated once.
const turtle = new Turtle();
const polygons = new Polygons();
const text = new Text();
turtle.radians();

// The walk function will be called until it returns false.
function walk(i) {
    let scale = 100;
    let x = Math.PI / 6;
    let sin_x = Math.sin(x);
    let cos_x = Math.cos(x);
    let sin_2x = Math.sin(2 * x);
    let cos_2x = Math.cos(2 * x);
    let sin_squared_x = sin_x * sin_x;
    let cos_squared_x = cos_x * cos_x;
    let sin_x_cos_x = sin_x * cos_x;
    
    let half_width = cos_squared_x * scale / 2;
    let half_height = sin_2x * scale / 2
    
    let hatchOne = {angle: Math.PI / -4, distance: 1.3};
    let hatchTwo = {angle: Math.PI / 4, distance: 1.5};
    
    //topleft
    drawPoints( [
        [0, 0],
        [cos_2x, 0],
        [0, sin_2x]
    ], hatchOne, half_width, half_height, scale);
    
    //topright
    drawPoints( [
        [cos_2x, 0],
        [cos_squared_x, 0],
        [cos_squared_x, sin_x_cos_x]
    ], hatchOne, half_width, half_height, scale);

    //bottomright
    drawPoints( [
        [cos_squared_x, sin_x_cos_x],
        [cos_squared_x, sin_2x],
        [cos_2x, sin_2x],
        [cos_2x, sin_2x * 2 / 3]
    ], hatchOne, half_width, half_height, scale);

    //bottomleft
    drawPoints( [
        [0, sin_2x],
        [cos_2x, sin_2x * 2 / 3],
        [cos_2x, sin_2x]
    ], hatchTwo, half_width, half_height, scale);

    //midleft
    drawPoints( [
        [0, sin_2x],
        [cos_2x, sin_2x * 2 / 3],
        [cos_2x, 0]
    ], hatchTwo, half_width, half_height, scale);

    //midright
    drawPoints( [
        [cos_2x, sin_2x * 2 / 3],
        [cos_2x, 0],
        [cos_squared_x, sin_x_cos_x]
    ], hatchOne, half_width, half_height, scale);
    
    turtle.jump(-half_width + 7, half_height);
    turtle.seth(Math.PI * 1.5);
    turtle.circle(-7, x + x);
    turtle.seth(0);
    
    turtle.jump(half_width, - 7);
    turtle.circle(7, -x);
    turtle.seth(0);
    
    turtle.jump((cos_2x * scale) - half_width + 5, half_height);
    turtle.seth(Math.PI * 1.5);
    turtle.forward(5);
    turtle.left(Math.PI / 2);
    turtle.forward(5);
    turtle.jump(half_width, half_height - 5);
    turtle.forward(5);
    turtle.left(Math.PI / 2);
    turtle.forward(5);
    turtle.jump(half_width -5, -half_height);
    turtle.forward(5);
    turtle.left(Math.PI / 2);
    turtle.forward(5);
    turtle.jump((cos_squared_x * scale) - half_width, 0);
    turtle.seth((Math.PI * 1.5) - x);
    turtle.up();
    turtle.forward(5);
    turtle.down();
    turtle.left(Math.PI / 2);
    turtle.forward(5);
    turtle.left(Math.PI / 2);
    turtle.forward(5);
    

    for(let i = 0; i < 4; i++) {
        write(-21, -half_height - 4, 'cos(2x)', .15, 0);
        write(-21, -half_height - 4, 'cos(2x)', .15, 0);
        
        write(18, -half_height - 4, 'sin (x)', .15, 0);
        write(25, -half_height - 5.5, '2', .075, 0);
        
        write(-half_width - 4, 8, 'sin(2x)', .15, Math.PI * 1.5);
    
        write(half_width + 4, -7, 'sin(x)cos(x)', .15, Math.PI * 1.5);
        write(half_width + 4, -7 + half_height, 'sin(x)cos(x)', .15, Math.PI * 1.5);

        write(-3, half_height + 4, 'cos (x)', .15, 0);
        write(5.5, half_height + 2.5, '2', .075, 0);

        write(-3, half_height - 24, 'cos(x)', .15, -x);

        write(21, -half_height + 23, 'sin(x)', .15, 2 * x);

        write(-17, 0, '1', .15, -2 * x);

        write(-half_width + 6, half_height - 7.5, 'x', .15, 0);
        write(-half_width + 8, half_height - 3, 'x', .15, 0);

        write(half_width - 3, -10, 'x', .15, 0);



        write(-90, 70, 'sin(2x) = 2sin(x)cos(x)', .15, 0);

        write(-90, 78, 'cos(2x) = cos (x) - sin (x)', .15, 0);
        write(-55, 76.5, '2', .075, 0);
        write(-30.5, 76.5, '2', .075, 0);

        write(-90, 86, '1 = cos (x) + sin (x)', .15, 0);
        write(-69.5, 84.5, '2', .075, 0);
        write(-45, 84.5, '2', .075, 0);
        
        write(-80, -85, 'Trigonometric identities', .44, 0);
        write(-40, -72, '-a visual proof- ', .3, 0);
    }
}

function write(x, y, txt, scale, heading) {
    let h = turtle.h();
    turtle.seth(heading);
    turtle.jump(x, y);
    text.print(turtle, txt, scale); 
    turtle.seth(h);
}

function drawPoints(points, hatch, translateX, translateY, scale) {
    points = points.map(p => [(p[0] * scale) - translateX, (p[1] * scale) - translateY]);

    let p = polygons.create();
    p.addPoints(...points);
    p.addOutline();
    p.addHatching(hatch.angle, hatch.distance);
    polygons.draw(turtle, p, true);
}

////////////////////////////////////////////////////////////////
// Polygon Clipping utility code - Created by Reinder Nijhoff 2019
// https://turtletoy.net/turtle/a5befa1f8d
////////////////////////////////////////////////////////////////
function Polygons(){let t=[];const s=class{constructor(){this.cp=[],this.dp=[],this.aabb=[]}addPoints(...t){let s=1e5,e=-1e5,h=1e5,i=-1e5;(this.cp=[...this.cp,...t]).forEach(t=>{s=Math.min(s,t[0]),e=Math.max(e,t[0]),h=Math.min(h,t[1]),i=Math.max(i,t[1])}),this.aabb=[(s+e)/2,(h+i)/2,(e-s)/2,(i-h)/2]}addSegments(...t){t.forEach(t=>this.dp.push(t))}addOutline(){for(let t=0,s=this.cp.length;t<s;t++)this.dp.push(this.cp[t],this.cp[(t+1)%s])}draw(t){for(let s=0,e=this.dp.length;s<e;s+=2)t.jump(this.dp[s]),t.goto(this.dp[s+1])}addHatching(t,e){const h=new s;h.cp.push([-1e5,-1e5],[1e5,-1e5],[1e5,1e5],[-1e5,1e5]);const i=Math.sin(t)*e,n=Math.cos(t)*e,a=200*Math.sin(t),p=200*Math.cos(t);for(let t=.5;t<150/e;t++)h.dp.push([i*t+p,n*t-a],[i*t-p,n*t+a]),h.dp.push([-i*t+p,-n*t-a],[-i*t-p,-n*t+a]);h.boolean(this,!1),this.dp=[...this.dp,...h.dp]}inside(t){let s=0;for(let e=0,h=this.cp.length;e<h;e++)this.segment_intersect(t,[.13,-1e3],this.cp[e],this.cp[(e+1)%h])&&s++;return 1&s}boolean(t,s=!0){if(s&&Math.abs(this.aabb[0]-t.aabb[0])-(t.aabb[2]+this.aabb[2])>=0&&Math.abs(this.aabb[1]-t.aabb[1])-(t.aabb[3]+this.aabb[3])>=0)return this.dp.length>0;const e=[];for(let h=0,i=this.dp.length;h<i;h+=2){const i=this.dp[h],n=this.dp[h+1],a=[];for(let s=0,e=t.cp.length;s<e;s++){const h=this.segment_intersect(i,n,t.cp[s],t.cp[(s+1)%e]);!1!==h&&a.push(h)}if(0===a.length)s===!t.inside(i)&&e.push(i,n);else{a.push(i,n);const h=n[0]-i[0],p=n[1]-i[1];a.sort((t,s)=>(t[0]-i[0])*h+(t[1]-i[1])*p-(s[0]-i[0])*h-(s[1]-i[1])*p);for(let h=0;h<a.length-1;h++)(a[h][0]-a[h+1][0])**2+(a[h][1]-a[h+1][1])**2>=.001&&s===!t.inside([(a[h][0]+a[h+1][0])/2,(a[h][1]+a[h+1][1])/2])&&e.push(a[h],a[h+1])}}return(this.dp=e).length>0}segment_intersect(t,s,e,h){const i=(h[1]-e[1])*(s[0]-t[0])-(h[0]-e[0])*(s[1]-t[1]);if(0===i)return!1;const n=((h[0]-e[0])*(t[1]-e[1])-(h[1]-e[1])*(t[0]-e[0]))/i,a=((s[0]-t[0])*(t[1]-e[1])-(s[1]-t[1])*(t[0]-e[0]))/i;return n>=0&&n<=1&&a>=0&&a<=1&&[t[0]+n*(s[0]-t[0]),t[1]+n*(s[1]-t[1])]}};return{list:()=>t,create:()=>new s,draw:(s,e,h=!0)=>{for(let s=0;s<t.length&&e.boolean(t[s]);s++);e.draw(s),h&&t.push(e)}}}

////////////////////////////////////////////////////////////////
// Text utility code. Created by Reinder Nijhoff 2019
// https://turtletoy.net/turtle/1713ddbe99
////////////////////////////////////////////////////////////////
function Text(){const s="br>eoj^jl<jqirjskrjq>brf^fe<n^ne>`ukZdz<qZjz<dgrg<cmqm>`thZhw<lZlw<qao_l^h^e_caccdeefggmiojpkqmqporlshsercp>^vs^as<f^h`hbgdeeceacaab_d^f^h_k`n`q_s^<olmmlolqnspsrrspsnqlol>]wtgtfsereqfphnmlpjrhsdsbraq`o`makbjifjekckaj_h^f_eaecffhimporqssstrtq>eoj`i_j^k_kajcid>cqnZl\\j_hcghglhqjulxnz>cqfZh\\j_lcmhmllqjuhxfz>brjdjp<egom<ogem>]wjajs<ajsj>fnkojpiojnkokqis>]wajsj>fnjniojpkojn>_usZaz>`ti^f_dbcgcjdofrisksnrpoqjqgpbn_k^i^>`tfbhak^ks>`tdcdbe`f_h^l^n_o`pbpdofmicsqs>`te^p^jfmfogphqkqmppnrkshserdqco>`tm^clrl<m^ms>`to^e^dgefhekenfphqkqmppnrkshserdqco>`tpao_l^j^g_ebdgdlepgrjsksnrppqmqlpingkfjfggeidl>`tq^gs<c^q^>`th^e_dadceegfkgnhpjqlqopqorlshserdqcocldjfhigmfoepcpao_l^h^>`tpeohmjjkikfjdhcecddaf_i^j^m_oapepjoomrjshserdp>fnjgihjikhjg<jniojpkojn>fnjgihjikhjg<kojpiojnkokqis>^vrabjrs>]wagsg<amsm>^vbarjbs>asdcdbe`f_h^l^n_o`pbpdofngjijl<jqirjskrjq>]xofndlcicgdfeehekfmhnknmmnk<icgefhfkgmhn<ocnknmpnrntluiugtdsbq`o_l^i^f_d`bbad`g`jambodqfrislsorqqrp<pcokompn>asj^bs<j^rs<elol>_tc^cs<c^l^o_p`qbqdpfoglh<chlhoipjqlqopqorlscs>`urcqao_m^i^g_eadccfckdnepgrismsorqprn>_tc^cs<c^j^m_oapcqfqkpnopmrjscs>`sd^ds<d^q^<dhlh<dsqs>`rd^ds<d^q^<dhlh>`urcqao_m^i^g_eadccfckdnepgrismsorqprnrk<mkrk>_uc^cs<q^qs<chqh>fnj^js>brn^nnmqlrjshsfreqdndl>_tc^cs<q^cl<hgqs>`qd^ds<dsps>^vb^bs<b^js<r^js<r^rs>_uc^cs<c^qs<q^qs>_uh^f_daccbfbkcndpfrhslsnrppqnrkrfqcpan_l^h^>_tc^cs<c^l^o_p`qbqepgohlici>_uh^f_daccbfbkcndpfrhslsnrppqnrkrfqcpan_l^h^<koqu>_tc^cs<c^l^o_p`qbqdpfoglhch<jhqs>`tqao_l^h^e_caccdeefggmiojpkqmqporlshsercp>brj^js<c^q^>_uc^cmdpfrisksnrppqmq^>asb^js<r^js>^v`^es<j^es<j^os<t^os>`tc^qs<q^cs>asb^jhjs<r^jh>`tq^cs<c^q^<csqs>cqgZgz<hZhz<gZnZ<gznz>cqc^qv>cqlZlz<mZmz<fZmZ<fzmz>brj\\bj<j\\rj>asazsz>fnkcieigjhkgjfig>atpeps<phnfleiegfehdkdmepgrislsnrpp>`sd^ds<dhffhekemfohpkpmopmrkshsfrdp>asphnfleiegfehdkdmepgrislsnrpp>atp^ps<phnfleiegfehdkdmepgrislsnrpp>asdkpkpiognfleiegfehdkdmepgrislsnrpp>eqo^m^k_jbjs<gene>atpepuoxnylzizgy<phnfleiegfehdkdmepgrislsnrpp>ate^es<eihfjemeofpips>fni^j_k^j]i^<jejs>eoj^k_l^k]j^<kekvjyhzfz>are^es<oeeo<ikps>fnj^js>[y_e_s<_ibfdegeifjijs<jimfoeretfuius>ateees<eihfjemeofpips>atiegfehdkdmepgrislsnrppqmqkphnfleie>`sdedz<dhffhekemfohpkpmopmrkshsfrdp>atpepz<phnfleiegfehdkdmepgrislsnrpp>cpgegs<gkhhjfleoe>bsphofleieffehfjhkmlompopporlsisfrep>eqj^jokrmsos<gene>ateeeofrhsksmrpo<peps>brdejs<pejs>_ubefs<jefs<jens<rens>bseeps<pees>brdejs<pejshwfydzcz>bspees<eepe<esps>cqlZj[i\\h^h`ibjckekgii<j[i]i_jakbldlfkhgjkllnlpkrjsiuiwjy<ikkmkojqirhthvixjylz>fnjZjz>cqhZj[k\\l^l`kbjcieigki<j[k]k_jaibhdhfihmjilhnhpirjskukwjy<kkimiojqkrltlvkxjyhz>^vamakbhdgfghhlknlplrksi<akbidhfhhillnmpmrlsisg>brb^bscsc^d^dsese^f^fsgsg^h^hsisi^j^jsksk^l^lsmsm^n^nsoso^p^psqsq^r^rs".split(">").map(s=>[s.charCodeAt(0)-106,s.charCodeAt(1)-106,s.substr(2).split("<").map(s=>{const e=[];for(let p=0;p<s.length;p+=2)e.push(s.substr(p,2).split("").map(s=>s.charCodeAt(0)-106));return e})]);return new class{print(e,p,j=1,h=0,r=1){e.radians();let f=[e.x(),e.y()],o=e.h(),i=f;p.split("").map(p=>{const c=p.charCodeAt(0)-32;if(c<0)f=i=this.rotAdd([0,48*j],i,o);else if(c>96)f=this.rotAdd([16*j,0],i,o);else{const p=s[c],i=p[0]*j,d=p[1]*j;p[2].map(s=>{e.up(),s.map(s=>{e.goto(this.rotAdd([(s[0]-s[1]*h)*j-i,s[1]*j],f,o)),e.down()})}),f=this.rotAdd([(d-i)*r,0],f,o)}})}rotAdd(s,e,p){return[Math.cos(p)*s[0]-Math.sin(p)*s[1]+e[0],Math.cos(p)*s[1]+Math.sin(p)*s[0]+e[1]]}}}