2
$\begingroup$

So I was trying to use this package billiard but some functions it uses are outdated so I was trying to update it (I tried to use the update button but didn't work). I basically did copy-paste and replaced the old functions with new ones. But when I try to use Needs[] in a notebook I got Needs::nocont: Context 1 was not created when Needs was evaluated. Then I used $Packages to see if the package was installed and I got this

CloudObject`Private`billiards`Private`Private`Private`billiards`\
Private`Private`billiards`,"CloudObject`Private`billiards`Private`Private`Private`billiards"

which is strange to me because the other packages I had installed appear like Example`. Here is the code

BeginPackage["billiards`"]

play::usage=
"play[expression, options] animates the motion of a billiard ball\n
 on a table whose shape is given by the equation 'expression == 0'.\n
 expression should be given in terms of global variables x and y.\n 
 Examples:\n
    play[] -- animates circular table of radius 1, ball starting\n
\tfrom {0,.2} at angle Pi/4, for 25 collisions.\n
    play[x^2 + 2y^2 -1, NumPts-> 40] -- elliptical table, longer\n
\tanimation, other values the same.
\n\n
 Bugs:\tUser supplied boundary test function not implemented.\n
\tNo sanity checking or error handling.\n
\tShould highlight foci and boundary regions of interest.\n"

Options[play] = 
    {   StartPt   -> {0,.2},
        StartV    -> {N[Sqrt[2]/2], N[Sqrt[2]/2]},
        NumPts    -> 25,
        BdryTest  -> bordertest,
        Title     -> "Billiards"
    }

StartPt::usage="Billiard ball starts here. (Default: {0,.2})"
StartV::usage="Initial direction of motion. (Default: {Sqrt[2]/2,Sqrt[2]/2})"
NumPts::usage="How many bounces to compute. (Default: 25)"
Title::usage="Title of animation. (Default: Billiards)"
BdryTest::usage="Not yet implemented. Test to determine hitting of wall."

Begin["`Private`"]

Clear[x,y]
normed[a_,b_] := {a,b}/Sqrt[a^2 + b^2]; (* returns unit vector *)
size[{a_,b_}] := a^2 + b^2;     (* square of length *)

play[ function_:Automatic, opts___ ] :=
    Module[ {expr  = function,
         start,
         n,j,
         v, numPts, title,
         next, oneframe, border,
         w, pt,oldv, curve,i},

    If[function === Automatic,
        expr   = Global`x^2 + Global`y^2 -1];

    start  = StartPt   /. {opts} /. Options[play];
    v      = StartV    /. {opts} /. Options[play];
    numPts = NumPts    /. {opts} /. Options[play];
    inside = BdryTest  /. {opts} /. Options[play];
    title  = Title     /. {opts} /. Options[play];

    (* Binary search: returns interior point within sqrt(epsilon)
                      of bounding curve *)
    bsearch[start_, v_List] := Module[ 
        {hi, lo, delta = .1, epsilon = .00000001},
        lo = hi = start;
        While[ inside[expr,hi], hi += delta v ];
        While[ size[hi - lo] > epsilon,
               mid = (hi + lo)/2;
               If[ inside[expr, mid], lo = mid, hi = mid ] ];
        Return[lo]
        ];

    (* Plots border curve for superposition with billiard path *)
    border[conic_] := 
        ContourPlot[conic == 0, 
            {Global`x, 0, 4},
            {Global`y, 0, 4},
            AspectRatio -> Automatic, 
            PlotRange -> {{0,4}, {0,4}},
            AxesOrigin -> {0,0},
            DisplayFunction -> Identity];

    (* returns $\partial f/\partial y, -\partial f/\partial x$ *)
    der[expr_] := normed[D[expr,Global`y], - D[expr,Global`x]];

    (* tests if f(pt) is negative (i.e., within bounding curve)*)
    bordertest[expr_, {a_,b_}] := 
        (expr /. {Global`x->a, Global`y->b}) < 0;

    (* next[i] is the i'th point of the billiard path. *)
    Clear[next];
    next[1]   = start;
    next[i_] := next[i] = Module[{},
    pt = bsearch[next[i - 1], v];
    w = der[expr] /. {Global`x -> pt[[1]], Global`y -> pt[[2]]};
        v = Apply[normed,2 (v . w) w - v];
        Return[pt]
        ];

    curve = border[expr];       (* Create bounding curve *)

    (* returns Graphics object depicting first n-1 bounces *)
    oneframe[n_] := Graphics[Line[Table[next[i], {i, 1, n}]]];

    (* Creates numPts frames of path and bounding curve *)
    all = Table[{oneframe[j], curve}, {j,1,numPts}];

    (* rigamarole to change animation window name *)
    saveDisplayTitle = $DisplayTitle;
	$DisplayTitle    = title;

    (* Display the animation *)
    (* If on NeXT or Mac, show in Front End window *)
    If[
        Length[StringPosition[$MachineType, {"NeXT", "Mac"}]]>0,
		Show[#, DisplayFunction->$DisplayFunction]& /@ all,
        ShowAnimation[all, DisplayFunction -> $DisplayFunction, 
            PlotRange -> {{0,4},{0,4}}];
    ];

    (* restore the default window name *)
    $DisplayTitle    = saveDisplayTitle;
]

End[]
EndPackage[]


$\endgroup$
  • 2
    $\begingroup$ It should be BeginPackage["billiards`"], not BeginPackage["`billiards`"] in the first line (notice the missing `) $\endgroup$ – Lukas Lang Aug 21 at 21:02
  • $\begingroup$ Thanks!, I do not get error now but the code doesn't display anything, at least it's an advance $\endgroup$ – Valdes Aug 22 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.