Differential Equations - Locators

A Theoretical Introduction to Locators

I.

A clever feature of the Wolfram Language is support for two-dimensional sliders, which allow you to use both directions of mouse movement to control two values simultaneously. (Ordinary one-dimensional sliders in a sense waste one of the two degrees of freedom a mouse is capable of.) To get a 2D slider, use pairs of numbers for both the min and max, as in {var,{Subscript[x, min],Subscript[y, min]},{Subscript[x, max],Subscript[y, max]}}. The value of the variable will also be an {x,y} pair. In this trivial example, just look at the value of the variable to get a feel for how the control works.

Wolfram Language programming can add arbitrary graphical elements to the output. For example, here we have lines to the selected point, with a second linear slider determining the number of lines.

Manipulate[
 Graphics[{Line[Table[{{Cos[t], Sin[t]}, pt}, {t, 2. Pi/n, 2. Pi, 2. Pi/n}]]},  PlotRange -> 1],

{{n, 30}, 1, 200, 1}, {pt, {-1, -1}, {1, 1}}]

Run these in Mathematica:

Locators1Wolfram Technology at Howard University | Howard University ...

II.

For creating interactive graphics examples, one of the most important features of Manipulate is the ability to place a control point, called a Locator, inside graphics that appear in the output area. 

Consider the previous example with lines going to a center point. While using a 2D slider is a fine way to control the center point, you might prefer to be able to simply click and drag the center point itself. This can be done by adding Locator to the control specification for the pt variable. In this case it is not necessary to specify a min and max range, because it can be taken automatically from the graphic. (It is, however, necessary to specify an initial value.)

Manipulate[
 Graphics[{Line[Table[{{Cos[t], Sin[t]}, pt}, {t, 2. Pi/n, 2. Pi, 2. Pi/n}]]},  PlotRange -> 1],

{{n, 30}, 1, 200, 1}, {{pt, {0, 0}}, Locator}]

Now you can click anywhere in the graphic and the center point of the lines will follow the mouse as long as you keep the mouse button down. (It is not necessary to click exactly on the center; it will jump to wherever you click, anywhere in the graphic.)

You can have multiple Locator controls by listing them individually, and it is perfectly fine to have a Manipulate with no controls outside the content area, so you can create purely graphical examples.

Manipulate[

                   Graphics[{Pink, Polygon[{pt1, pt2, pt3}], PlotRange -> 1}],
 {{pt1, {0, 0}}, Locator}, {{pt2, {0, 1}}, Locator}, {{pt3, {1, 0}},  Locator}]

When there are multiple locators, you can still click anywhere in the graphic, and the nearest Locator will jump to where you click and start tracking the mouse.

You can of course combine Locator controls with normal Manipulate variables. For example, you can use some sliders and color choosers to control the appearance of the polygon.

Manipulate[
 Graphics[{FaceForm[face], EdgeForm[{edge, Thickness[thickness]}], 
   Polygon[pts]}, PlotRange -> 1, Background -> background],
 {face, Green},
 {edge, Red},
 {background, Cyan},
 {{thickness, 0.02}, 0, 0.1},
 {{pts, {{0, 0}, {.5, 0}, {0, .5}}}, Locator, 
  LocatorAutoCreate -> True}]

Run these in Mathematica:

Locators2Wolfram Technology at Howard University | Howard University ...

III.

Let's now look at 3d graphics.

As in 2D, you can use the low-level graphics language just as easily as higher-level plotting commands. In this example you can see how the Wolfram Language handles spheres that intersect with each other and with the bounding box.

Manipulate[
 Graphics3D[{Sphere[{0, 0, 0}, r1], Sphere[{c[[1]], c[[2]], 0}, r2]},  PlotRange -> 2],

{{r1, 1}, 0, 2}, {{r2, 1}, 0,  2}, {c, {-2, -2}, {2, 2}}]

Run these in Mathematica:

Locators3Wolfram Technology at Howard University | Howard University ...