(* :Title: Triangeluen elementuak*)

(* :Author: Carlos Gorria *)

(* :Keywords: Triangeluak*)

(* :Summary: Triangeluen elementuak *)

BeginPackage["Garatu`Triangeluak`"]

Triangeluak::usage = "Garatu`Triangeluak`" 

Barizentroa::usage = "Barizentroa[pt] pt puntuek mugatzen duten triangelua eta beronek zehazten duen barizentroa"

Zirkunzentroa::usage = "Zirkunzentroa[pt] pt puntuek mugatzen duten triangelua eta beronek zehazten duen zirkunzentroa eta kanpokaldetiko zirkunferentzia ukitzailea"

Ortozentroa::usage = "Ortozentroa[pt] pt puntuek mugatzen duten triangelua eta beronek zehazten duen ortozentroa"

Inzentroa::usage = "Inzentroa[pt] pt puntuek mugatzen duten triangelua eta beronek zehazten duen inzentroa eta barrukaldetiko zirkunferentzia ukitzailea"

Begin[ "`Private`" ]

Unprotect[Barizentroa,Zirkunzentroa,Ortozentroa,Inzentroa]

Barizentroa[pt_]:=Module[{pe,bar,zabal,luz,i},
    pe={{(pt[[2]][[1]]+pt[[3]][[1]])/2,(pt[[2]][[2]]+pt[[3]][[2]])/2},
        {(pt[[1]][[1]]+pt[[3]][[1]])/2,(pt[[1]][[2]]+pt[[3]][[2]])/2},
        {(pt[[2]][[1]]+pt[[1]][[1]])/2,(pt[[2]][[2]]+pt[[1]][[2]])/2}};
    bar={(pt[[1]][[1]]+pt[[2]][[1]]+pt[[3]][[1]])/3,
        (pt[[1]][[2]]+pt[[2]][[2]]+pt[[3]][[2]])/3};
    luz=Max[Table[pt[[i]][[1]],{i,1,3}]]-Min[Table[pt[[i]][[1]],{i,1,3}]];
    zabal=Max[Table[pt[[i]][[2]],{i,1,3}]]-Min[Table[pt[[i]][[2]],{i,1,3}]];
    Show[{Graphics[{{RGBColor[1,1,0],Polygon[{pt[[1]],pt[[2]],pt[[3]]}]},
            {Thickness[0.008],Line[{pt[[1]],pt[[2]]}],
              Line[{pt[[1]],pt[[3]]}],Line[{pt[[3]],pt[[2]]}]}}],
        Graphics[Table[Text[StyleForm[pt[[i]],FontSize->12,FontWeight->"Bold"],
       {pt[[i]][[1]]+0.1,pt[[i]][[2]]-0.4},{0,-1},{1,0}],{i,1,3}]],
        Graphics[{Disk[bar,{0.06,0.06}],
            Text[StyleForm[bar,FontSize->14,FontWeight->"Bold"],
              {bar[[1]]+0.5,bar[[2]]},{0,-1},{1,0}]}],
        Graphics[{Thickness[0.004],Table[Line[{pt[[i]],pe[[i]]}],{i,1,3}]}]},
      PlotRange->All,AspectRatio->zabal/luz];]

Zirkunzentroa[pt_]:=Module[{pe,mald1,mald2,zirkx,zirky,
      zirkunzen,zirkuradioa,zabal,luz,x,i},
    pe={{(pt[[2]][[1]]+pt[[3]][[1]])/2,(pt[[2]][[2]]+pt[[3]][[2]])/2},
        {(pt[[1]][[1]]+pt[[3]][[1]])/2,(pt[[1]][[2]]+pt[[3]][[2]])/2},
        {(pt[[2]][[1]]+pt[[1]][[1]])/2,(pt[[2]][[2]]+pt[[1]][[2]])/2}};
    mald1=-(pt[[2]][[1]]-pt[[3]][[1]])/(pt[[2]][[2]]-pt[[3]][[2]]);
    mald2=-(pt[[1]][[1]]-pt[[3]][[1]])/(pt[[1]][[2]]-pt[[3]][[2]]);
    zirkx=x/.(Solve[pe[[1]][[2]]+(x-pe[[1]][[1]])*mald1==
                pe[[2]][[2]]+(x-pe[[2]][[1]])*mald2,x])[[1]];
    zirky=pe[[1]][[2]]+(zirkx-pe[[1]][[1]])*mald1;
    zirkunzen={zirkx,zirky};
    zirkuradioa=Sqrt[(zirkunzen[[1]]-pt[[1]][[1]])^2+
          (zirkunzen[[2]]-pt[[1]][[2]])^2];
    luz=Max[Table[pt[[i]][[1]],{i,1,3}]]-Min[Table[pt[[i]][[1]],{i,1,3}]];
    zabal=Max[Table[pt[[i]][[2]],{i,1,3}]]-Min[Table[pt[[i]][[2]],{i,1,3}]];
    Show[{Graphics[{{RGBColor[1,1,0],Polygon[{pt[[1]],pt[[2]],pt[[3]]}]},
            {Thickness[0.008],Line[{pt[[1]],pt[[2]]}],
              Line[{pt[[1]],pt[[3]]}],Line[{pt[[3]],pt[[2]]}]}}],
        Graphics[{Table[Text[StyleForm[pt[[i]],FontSize->12,FontWeight->"Bold"],
                {pt[[i]][[1]]+0.1,pt[[i]][[2]]-0.4},{0,-1},{1,0}],{i,1,3}],
            Text[StyleForm[zirkunzen,FontSize->12,FontWeight->"Bold"],
              {zirkx+0.5,zirky},{0,-1},{1,0}]}],
        Graphics[{Disk[zirkunzen,{0.06,0.06}],Circle[zirkunzen,zirkuradioa]}],
        Graphics[{Thickness[0.005],Table[Line[{zirkunzen,pe[[i]]}],
              {i,1,3}]}]},PlotRange->All,AspectRatio->1];]

Ortozentroa[pt_]:=Module[{mald1,mald2,ortx,orty,
      ortozen,zabal,luz,x,i},
    mald1=-(pt[[2]][[1]]-pt[[3]][[1]])/(pt[[2]][[2]]-pt[[3]][[2]]);
    mald2=-(pt[[1]][[1]]-pt[[3]][[1]])/(pt[[1]][[2]]-pt[[3]][[2]]);
    ortx=x/.(Solve[pt[[1]][[2]]+(x-pt[[1]][[1]])*mald1==
                pt[[2]][[2]]+(x-pt[[2]][[1]])*mald2,x])[[1]];
    orty=pt[[1]][[2]]+(ortx-pt[[1]][[1]])*mald1;
    ortozen={ortx,orty};
    luz=Max[Table[pt[[i]][[1]],{i,1,3}]]-Min[Table[pt[[i]][[1]],{i,1,3}]];
    zabal=Max[Table[pt[[i]][[2]],{i,1,3}]]-Min[Table[pt[[i]][[2]],{i,1,3}]];
    Show[{Graphics[{{RGBColor[1,1,0],
              Polygon[{pt[[1]],pt[[2]],pt[[3]]}]},
            {Thickness[0.008],Line[{pt[[1]],pt[[2]]}],
              Line[{pt[[1]],pt[[3]]}],Line[{pt[[3]],pt[[2]]}]}}],
        Graphics[{Table[Text[StyleForm[pt[[i]],FontSize->12,FontWeight->"Bold"],
                {pt[[i]][[1]]+0.1,pt[[i]][[2]]-0.4},{0,-1},{1,0}],{i,1,3}],
            Text[StyleForm[ortozen,FontSize->12,FontWeight->"Bold"],
              {ortx,orty-0.5},{0,-1},{1,0}]}],
        Graphics[{Disk[ortozen,{0.06,0.06}]}],
        Graphics[{Thickness[0.005],Table[Line[{ortozen,pt[[i]]}],
              {i,1,3}]}]},PlotRange->All,AspectRatio->zabal/luz];]

Inzentroa[pt_]:=Module[{bidesk,ang,mald1,mald2,mald11,mald12,
      mald21,mald22,inzx,inzy,inzen,inradioa,p13x,p13y,luz,zabal,x,i},
    mald11=Which[pt[[2,2]]-pt[[1,2]]>=0&&pt[[2,1]]-pt[[1,1]]>=0,
        ArcTan[(pt[[2,2]]-pt[[1,2]])/(pt[[2,1]]-pt[[1,1]])],
        pt[[2,2]]-pt[[1,2]]>=0&&pt[[2,1]]-pt[[1,1]]<0,
        Pi+ArcTan[(pt[[2,2]]-pt[[1,2]])/(pt[[2,1]]-pt[[1,1]])],
        pt[[2,2]]-pt[[1,2]]<0&&pt[[2,1]]-pt[[1,1]]<0,
        Pi+ArcTan[(pt[[2,2]]-pt[[1,2]])/(pt[[2,1]]-pt[[1,1]])],
        pt[[2,2]]-pt[[1,2]]<0&&pt[[2,1]]-pt[[1,1]]>=0,
        2*Pi+ArcTan[(pt[[2,2]]-pt[[1,2]])/(pt[[2,1]]-pt[[1,1]])]];
    mald12=Which[pt[[3,2]]-pt[[1,2]]>=0&&pt[[3,1]]-pt[[1,1]]>=0,
        ArcTan[(pt[[3,2]]-pt[[1,2]])/(pt[[3,1]]-pt[[1,1]])],
        pt[[3,2]]-pt[[1,2]]>=0&&pt[[3,1]]-pt[[1,1]]<0,
        Pi+ArcTan[(pt[[3,2]]-pt[[1,2]])/(pt[[3,1]]-pt[[1,1]])],
        pt[[3,2]]-pt[[1,2]]<0&&pt[[3,1]]-pt[[1,1]]<0,
        Pi+ArcTan[(pt[[3,2]]-pt[[1,2]])/(pt[[3,1]]-pt[[1,1]])],
        pt[[3,2]]-pt[[1,2]]<0&&pt[[3,1]]-pt[[1,1]]>=0,
        2*Pi+ArcTan[(pt[[3,2]]-pt[[1,2]])/(pt[[3,1]]-pt[[1,1]])]];
    mald1=Tan[(mald11+mald12)/2];
    mald21=Which[pt[[1,2]]-pt[[2,2]]>=0&&pt[[1,1]]-pt[[2,1]]>=0,
        ArcTan[(pt[[1,2]]-pt[[2,2]])/(pt[[1,1]]-pt[[2,1]])],
        pt[[1,2]]-pt[[2,2]]>=0&&pt[[1,1]]-pt[[2,1]]<0,
        Pi+ArcTan[(pt[[1,2]]-pt[[2,2]])/(pt[[1,1]]-pt[[2,1]])],
        pt[[1,2]]-pt[[2,2]]<0&&pt[[1,1]]-pt[[2,1]]<0,
        Pi+ArcTan[(pt[[1,2]]-pt[[2,2]])/(pt[[1,1]]-pt[[2,1]])],
        pt[[1,2]]-pt[[2,2]]<0&&pt[[1,1]]-pt[[2,1]]>=0,
        2*Pi+ArcTan[(pt[[1,2]]-pt[[2,2]])/(pt[[1,1]]-pt[[2,1]])]];
    mald22=Which[pt[[3,2]]-pt[[2,2]]>=0&&pt[[3,1]]-pt[[2,1]]>=0,
        ArcTan[(pt[[3,2]]-pt[[2,2]])/(pt[[3,1]]-pt[[2,1]])],
        pt[[3,2]]-pt[[2,2]]>=0&&pt[[3,1]]-pt[[2,1]]<0,
        Pi+ArcTan[(pt[[3,2]]-pt[[2,2]])/(pt[[3,1]]-pt[[2,1]])],
        pt[[3,2]]-pt[[2,2]]<0&&pt[[3,1]]-pt[[2,1]]<0,
        Pi+ArcTan[(pt[[3,2]]-pt[[2,2]])/(pt[[3,1]]-pt[[2,1]])],
        pt[[3,2]]-pt[[2,2]]<0&&pt[[3,1]]-pt[[2,1]]>=0,
        2*Pi+ArcTan[(pt[[3,2]]-pt[[2,2]])/(pt[[3,1]]-pt[[2,1]])]];
    mald2=Tan[(mald21+mald22)/2];
    inzx=x/.(Solve[pt[[1,2]]+(x-pt[[1,1]])*mald1==
                pt[[2,2]]+(x-pt[[2,1]])*mald2,x])[[1]];
    inzy=pt[[1,2]]+(inzx-pt[[1,1]])*mald1;
    inzen={inzx,inzy};
    p13x=(x/.Solve[inzy-(x-inzx)*(pt[[3,1]]-pt[[1,1]])/(pt[[3,2]]-pt[[1,2]])==
                pt[[1,2]]+(x-pt[[1,1]])*(pt[[3,2]]-pt[[1,2]])/(pt[[3,1]]-pt[[1,1]]),x])[[1]];
    p13y=pt[[1,2]]+(p13x-pt[[1,1]])*(pt[[3,2]]-pt[[1,2]])/(pt[[3,1]]-pt[[1,1]]);
    inradioa=Sqrt[(inzx-p13x)^2+(inzy-p13y)^2];
    luz=Max[Table[pt[[i]][[1]],{i,1,3}]]-Min[Table[pt[[i]][[1]],{i,1,3}]];
    zabal=Max[Table[pt[[i]][[2]],{i,1,3}]]-Min[Table[pt[[i]][[2]],{i,1,3}]];
    Show[{Graphics[{{RGBColor[1,1,0],
              Polygon[{pt[[1]],pt[[2]],pt[[3]]}]},
            {Thickness[0.008],Line[{pt[[1]],pt[[2]]}],
              Line[{pt[[1]],pt[[3]]}],Line[{pt[[3]],pt[[2]]}]}}],
        Graphics[{Table[Text[StyleForm[pt[[i]],FontSize->12,FontWeight->"Bold"],
                {pt[[i]][[1]]+0.1,pt[[i]][[2]]-0.4},{0,-1},{1,0}],{i,1,3}],
            Text[StyleForm[N[inzen],FontSize->12,FontWeight->"Bold"],
              {inzx,inzy-0.5},{0,-1},{1,0}]}],
        Graphics[{Disk[inzen,{0.06,0.06}],Circle[inzen,inradioa]}],
        Graphics[{Thickness[0.005],Table[Line[{inzen,pt[[i]]}],
              {i,1,3}]}]},PlotRange->All,AspectRatio->zabal/luz];]

End[ (* "`Private`" *) ];

EndPackage[]   (* Garatu`Triangeluak` *)

(*:Examples:
pt={{0,4},{3,2},{-2,0}};

Barizentroa[pt]

Zirkunzentroa[pt]

Ortozentroa[pt]

Inzentroa[pt]
*)
