(* Copyright 1993 Alfred Gray *)

BeginPackage["CSPROGS`"]

     centerlines::usage="centerlines has the same syntax as
ParametricPlot3D. It plots a parametric patch in R^3, but adds lines
to the center of each quadrilateral."

     filledcurve::usage="filledcurve parametrically plots a plane curve and fills it in.  It has the same syntax as ParametricPlot."

    graphics2dtosurfrev::usage="graphics2dtosurfrev constructs a
surface of revolution from a Graphics2D list of lines."

    linetolines::usage="linetolines converts a broken line segment
Line[{pt1,...,ptn}] into a list of line segments connecting consecutive
pairs of the points pt1,...,ptn."

    paramcurveplot1::usage="paramcurveplot1 has the same syntax as
ParametricPlot3D. It plots the parameter lines u->x[u,v0] of a
parametric patch x:U->R^3."

    paramcurveplot2::usage="paramcurveplot2 has the same syntax as
ParametricPlot3D. It plots the parameter lines v->x[u0,v] of a
parametric patch x:U->R^3."

    wireframe::usage="wireframe has the same syntax as ParametricPlot3D.
It plots a wireframe of a parametric patch in R^3."

Begin["`Private`"]
    centerlines[surface_,{u_,u0_,u1_},{v_,v0_,v1_},
        opts___]:=
        Module[{plottmp,grtmp},
                plottmp = ParametricPlot3D[
                surface//Evaluate,{u,u0,u1},{v,v0,v1},
                DisplayFunction->Identity,opts];
                grtmp = plottmp /.
     (Polygon[pts_]:>
     {Polygon[Append[pts[[{1,2}]],Plus@@pts/4]],
      Polygon[Append[pts[[{2,3}]],Plus@@pts/4]],
      Polygon[Append[pts[[{3,4}]],Plus@@pts/4]],
      Polygon[Append[pts[[{4,1}]],Plus@@pts/4]]});
        Show[grtmp,DisplayFunction->$DisplayFunction]]

    filledcurve[curve_,{u_,u0_,u1_},opts___]:=
        Module[{plottmp,grtmp},
            plottmp = ParametricPlot[
            curve//Evaluate,{u,u0,u1},AspectRatio->Automatic,
            DisplayFunction->Identity,opts];
            grtmp = plottmp /.(Line[pts_]:> Polygon[pts]);
        Show[grtmp,DisplayFunction->$DisplayFunction]]
    
    graphics2dtosurfrev[n_][Line[{{a_,b_},{c_,d_}}]]:=
        Table[Polygon[{
            {a*Cos[2*Pi*(k-1)/n],a*Sin[2*Pi*(k-1)/n],b},
            {a*Cos[2*Pi*k/n],a*Sin[2*Pi*k/n],b},
            {c*Cos[2*Pi*k/n],c*Sin[2*Pi*k/n],d},
            {c*Cos[2*Pi*(k-1)/n],c*Sin[2*Pi*(k-1)/n],d}}],
        {k,n}]

    linetolines[Line[pts_]]:=Table[Line[{pts[[i]],
        pts[[i + 1]]}],{i,Length[pts] - 1}]

    paramcurveplot1[surface_,{u_,u0_,u1_},{v_,v0_,v1_},
        plotpoints->{upoints_,vpoints_},opts___]:=
        ParametricPlot3D[Table[
        surface,{v,v0,v1,(v1-v0)/(vpoints - 1)}]//Evaluate,
        {u,u0,u1},PlotPoints->upoints,opts]

    paramcurveplot2[surface_,{u_,u0_,u1_},{v_,v0_,v1_},
        plotpoints->{upoints_,vpoints_},opts___]:=
        ParametricPlot3D[Table[
        surface,{u,u0,u1,(u1-u0)/(upoints - 1)}]//Evaluate,
        {v,v0,v1},PlotPoints->vpoints,opts]

    wireframe[surface_,{u_,u0_,u1_},{v_,v0_,v1_},opts___]:=
        Module[{plottmp,grtmp},
                plottmp = ParametricPlot3D[
                surface,{u,u0,u1},{v,v0,v1},
                DisplayFunction->Identity,opts];
                grtmp = plottmp /.(Polygon[pts_]:> 
                Line[Append[pts,First[pts]]]);
        Show[grtmp,DisplayFunction->$DisplayFunction]]


End[]

EndPackage[]

