(*This Mathematica code was written by John Zweck (zweck@utdallas.edu) and generates a mathematical curve/surface that can be printed using a 3D printer. This model can (probably) be printed by Shapeways.*)

(*If you evaluate this Mathematica notebook,the curve/surface will be rendered using computer graphics.*)

(*If you uncomment the export line at the end of the code and set the directory path appropriately for your system you will obtain an STL file that can be sent to a 3D printer.*)

(*The STL file can also be viewed using the open source software Meshlab.*)

(*You may modify this code in any way you like.*)

(*This project was supported by the National Science Foundation through a Supplement to the UTeach Dallas Robert Noyce Scholarship Program (DUE 1544159).*)

(* If you put four of these models together you have the top half of two intersecting cylinders. This model is designed to sit nicely on a table *)


RadiusCylinder=40;
HeightCylinder = 60;

MeshedCylinder[u_,v_]:={RadiusCylinder*Cos[u],RadiusCylinder*Sin[u],v};

LeftHalfEllipse[t_] := {RadiusCylinder*Sin[t],RadiusCylinder*Cos[t],RadiusCylinder*Cos[t]};
RightHalfEllipse[t_] := {RadiusCylinder*Sin[t],-RadiusCylinder*Cos[t],RadiusCylinder*Cos[t]};
MinorAxisLine[t_] := {RadiusCylinder*t,0,0};
LeftMajorAxisLine[t_] := {0,RadiusCylinder*t,RadiusCylinder*t};
RightMajorAxisLine[t_] := {0,RadiusCylinder*t,-RadiusCylinder*t};

radius = 2;          (*Diameter of tubes is 2r in mm *)
numPoints=100;  (*Number of sides of cross section of polygonal tube*)

umin=Pi/2;
umax=3*Pi/2;
vmin=-HeightCylinder;
vmax=0;

NumVerticalLines=8;
NumCircles=6;

curvesVerticalLines=Table[MeshedCylinder[i,v],{i, umin, umax,(umax-umin)/NumVerticalLines}];

curvesCircles=Table[MeshedCylinder[u,j],{j,vmin, vmax,(vmax-vmin)/NumCircles}];

tubesVerticalLines=ParametricPlot3D[curvesVerticalLines,{v,vmin,vmax},RegionFunction->Function[{x,y,z}, z < -Abs[y]],
 PlotStyle->Tube[radius],PlotPoints->numPoints,PlotRange->All];

tubesCircles=ParametricPlot3D[curvesCircles,{u,umin,umax},RegionFunction->Function[{x,y,z}, z < -Abs[y]],
PlotStyle->Tube[radius],PlotPoints->numPoints,PlotRange->All];


tubeLeftHalfEllipse=ParametricPlot3D[LeftHalfEllipse[i],{i,Pi,3*Pi/2},PlotStyle->Tube[radius],PlotPoints->numPoints,PlotRange->All];

tubeRightHalfEllipse=ParametricPlot3D[RightHalfEllipse[i],{i,Pi,3*Pi/2},PlotStyle->Tube[radius],PlotPoints->numPoints,PlotRange->All];

tubeMinorAxisLine=ParametricPlot3D[MinorAxisLine[i],{i,-1,0},PlotStyle->Tube[radius],PlotPoints->numPoints,PlotRange->All];

tubeLeftMajorAxisLine=ParametricPlot3D[LeftMajorAxisLine[i],{i,-1,0},PlotStyle->Tube[radius],PlotPoints->numPoints,PlotRange->All];

tubeRightMajorAxisLine=ParametricPlot3D[RightMajorAxisLine[i],{i,0,1},PlotStyle->Tube[radius],PlotPoints->numPoints,PlotRange->All];

Vertex1 =Graphics3D[Sphere[{0,0,0},radius]];
Vertex2 =Graphics3D[Sphere[MeshedCylinder[Pi,0],radius]];
Vertex3 =Graphics3D[Sphere[RightHalfEllipse[Pi],radius]];
Vertex4 =Graphics3D[Sphere[LeftHalfEllipse[Pi],radius]];

output=Show[tubesVerticalLines,tubesCircles,tubeLeftHalfEllipse, tubeRightHalfEllipse,tubeMinorAxisLine,tubeLeftMajorAxisLine,tubeRightMajorAxisLine,Vertex1,Vertex2,Vertex3,Vertex4]

(*Export["/IntersectingHalfCylinders.stl",output];*)