(*  DBD02 - Golden Cut Ratio, Feb 2009
  Youyou Yang, s089966 *)


(* General fuctions: rotate, mirror, flip, identity*)

Rot[α_]:=N[(({{Cos[α], -Sin[α]}, {Sin[α], Cos[α]}}))];
MIRROR :=(({{-1, 0}, {0, 1}}));
FLIP:=(({{0, 1}, {1, 0}}));
ID:=(({{1, 0}, {0, 1}})); 


(* Fucntions for moving {0,0} coodinate system to other points, T series for basic pattern, S series for Nesting the basic pattern*)

t0[{x_,y_}]:={x,y}+{-1,0 }; 
t1[{x_,y_}]:={x,y}+{(1/2),((Sqrt[3])/2) };
t2[{x_,y_}]:={x,y}+{1,0 };
t3[{x_,y_}]:={x,y}+{(1/2),0.42 };
t4[{x_,y_}]:={x,y}+{0.63,-0.3 };

T0[fg_]:=t0 /@ fg;
T1[fg_]:=t1 /@ fg;
T2[fg_]:=t2 /@ fg;
T3[fg_]:=t3 /@ fg;
T4[fg_]:=t4 /@ fg;

s0[{x_,y_}]:={x,y}+{-(3/2),-((Sqrt[3])/2) };
s1[{x_,y_}]:={x,y}+{3,0 };
s2[{x_,y_}]:={x,y}+{0,(Sqrt[3]) };

S1[fg_]:=s1 /@ fg;
S2[fg_]:=s2 /@ fg;


(*Solution for the points that 2 circles intersect together -> Get the correct angle to draw the arc*)
Solve[{x^2+y^2==1,(x-(7/10))^2+(y-((Sqrt[3])/2))^2==(1/25)},{x,y}]


(*The pattern: right half part of the Clown*)
C1=Table[{Cos[ i] ,Sin [i] },{i,0,ArcCos[(23/31)],(π/(60
))}];
C2=Table[{Cos[ i] +(3/2),Sin [i]-((Sqrt[3])/2)  },{i,((2π)/3),((2π)/3)+ArcCos[(23/31)],(π/(60
))}];
C3=Table[{(1/5)Cos[i]+(1/5),(1/5)Sin[i]},{i,π,2π-ArcCos[(65/310)],(π/60)}];
C31=(Rot[((2π)/3)].#)&/@C3;
C32=(MIRROR.Rot[((2π)/3)].#)&/@C3;
C33=(MIRROR.Rot[(π/3)].#)&/@C3;
C34=(FLIP.Rot[π].#)&/@C3;
ball=Table[{(1/30)Cos[ i] ,(1/30)Sin [i]  },{i,0,2π,(π/(60
))}];
hat1={Rot[((2π)/3)].{(23/31)-(1/2),((12 (Sqrt[3]))/31)-((Sqrt[3])/2)}+{(1/2),((Sqrt[3])/2) },{0.5,1.12}};
hat12={Rot[((2π)/3)].{(23/31)-(1/2),((12 (Sqrt[3]))/31)-((Sqrt[3])/2)}+{(1/2),-((Sqrt[3])/2) },{0.5,1.12-(Sqrt[3])}};
hat2={{0.5,-0.5},{0.63,-0.3 }+{(1/30)Cos[ ((4π)/3)] ,(1/30)Sin [((4π)/3)]  }};
eye1={{0.52,1.045},{0.57,1.045}};
eye2={{0.545,1.07},{0.545,1.02}};
mouth1={{0.5,0.97},{0.56,0.97},{0.54,0.93},{0.5,0.93}};
mouth2={{0.5,0.95},{0.55,0.95}};


(*Divide the patten into 2 purposes: Cutting and Integrating*)
clownrcut={C1,C2,T1[C3],T1[C31],T2[C32],mouth1,hat1,hat12,T4[ball]};
clownrin={T2[C33],T3[C34],eye1,eye2,mouth2,hat2};


(*Mirror the right half part to get the left half part*)
mirror[xy_] := MIRROR.(t0[xy]);
Mirror[crv_]:=mirror/@ crv;
MIrror[crvlist_]:=Mirror/@ crvlist;
clownlcut=MIrror[clownrcut];
clownlin=MIrror[clownrin];

(*Union 2 parts together to get a complete Clown*)
Clowncut=Union[clownrcut,clownlcut];
Clownin=Union[clownrin,clownlin];
Clown=Union[Clowncut,Clownin];
Show[Graphics[Line[Clown]]]

(*Rotate the Clown to 120 degrees*)
rotate120[xy_] := Rot[((2π)/3)].xy;
Rotate120[crv_] := rotate120 /@ crv;
ROtate120[crvlist_] := Rotate120 /@ crvlist;
Clown120cut=ROtate120[Clowncut];
Clown120in=ROtate120[Clownin];

(*Rotate the Clown to 240 degrees*)
rotate240[xy_] := Rot[((4π)/3)].xy;
Rotate240[crv_] := rotate240 /@ crv;
ROtate240[crvlist_] := Rotate240 /@ crvlist;
Clown240cut=ROtate240[Clowncut];
Clown240in=ROtate240[Clownin];


(*Union 3 Clowns together -> Triclown *)
Triclowncut1=Union[Clowncut,Clown120cut,Clown240cut];
Triclownin1=Union[Clownin,Clown120in,Clown240in];
Triclown=Union[Triclowncut1,Triclownin1];
Show[Graphics[Line[Triclown]]]

(*Nest the Triclown *)
Allclowncut1:=Table[Line[Nest[(S1 /@ #)&,Nest[(S2 /@ #)&,Triclowncut1,i],j]],{i,0,3},{j,0,3}];
Allclownin1:=Table[Line[Nest[(S1 /@ #)&,Nest[(S2 /@ #)&,Triclownin1,i],j]],{i,0,3},{j,0,3}];

(*Shift the Triclown -> Fill the space between each Triclown*)
shift[xy_]:=ID.(s0[xy]);
Shift[crv_]:=shift /@ crv;
SHift[crvlist_] :=Shift /@ crvlist;
Triclowncut2=SHift[Triclowncut1];
Triclownin2=SHift[Triclownin1];

(*Nest the shifted Triclown *)
Allclowncut2:=Table[Line[Nest[(S1 /@ #)&,Nest[(S2 /@ #)&,Triclowncut2,i],j]],{i,0,3},{j,0,3}];
Allclownin2:=Table[Line[Nest[(S1 /@ #)&,Nest[(S2 /@ #)&,Triclownin2,i],j]],{i,0,3},{j,0,3}];

(*Union 2 nested Triclown sets*)
Allclowncut=Union[Allclowncut1,Allclowncut2];
Show[Graphics[{Red,Allclowncut,Black,Allclownin1,Allclownin2}]]