Hot answers tagged

13

Interpolate the x and y values separately and use PeriodicInterpolation by appending the first value of each list at the end: plot[pts_] := Module[{xs, ys}, {xs, ys} = Append[#, #[[1]]] & /@ Transpose[pts]; With[{ip = ListInterpolation[#, {{0, 1}}, InterpolationOrder -> 3, PeriodicInterpolation -> True] & /@ {xs, ys}}, ...


11

One way to detect crossings is to form a straight line between the previous position of the locator and the new position, and then check if there is an intersection between that straight line and $x^3$. I wrote this function to count the number of line intersection of a straight line with endpoints p1 and p2. The function which in this case is $x^3$ can be ...


10

In Mathematica 10 there is a new way to constrain locators that is short and simple. First define your region: circles = Table[Circle[{0, 0}, r], {r, 1, 15, 2}]; lines = Table[Line[{{-15 Cos[the], -15 Sin[the]}, {15 Cos[the], 15 Sin[the]}}], {the, 0, Pi, Pi/6}]; grid = RegionUnion[circles, lines]; And then use the second argument of Dynamic like in Michael ...


10

Use Nearest[]: pts = {{1.0, 12.}, {1.9, 10.}, {2.6, 8.2}, {3.4, 6.9}, {5.0, 5.9}}; nf = Nearest[pts]; DynamicModule[{loc = pts[[{1, -1}]]}, LocatorPane[Dynamic[loc, (loc = Flatten[nf[#], 1];) &], ListPlot[pts, Frame -> True]], SaveDefinitions -> True]


9

The option for a Manipulate control, that mimics the functionality of the second argument to Dynamic is TrackingFunction. f[x_] := Sin[x] Manipulate[ Column[{Show[Plot[f[x], {x, 0, 2 Pi}, Axes -> False, Frame -> True]], p}], {{p, {0, 0}}, Locator, TrackingFunction -> (p = {First@#, f@First@#}; &)}] Using only Manipulate and no Dynamic ...


9

Here's something to get you started with a new design. First we make the function that actually makes a DynamicModule. It supports having more than just a sphere as a background, but I haven't really played with that. It also supports the Dynamic vocabulary appropriately. I included a "PointNormalizer" in case you want to pass in a Region as the background ...


9

is this what you mean? ClearAll[r, a, b, d]; potentialsurface = 1/r^12 - 2/r^6 + 1/d^12 - 2/d^6 - (2 Exp[-(r - d)] + 2 Exp[-(d - r)] - 2 Exp[-(2 (r - d) - 2 (d - r))*Cos[d]]); Manipulate[ Module[{z}, z = N[1/a^12 - 2/a^6 + 1/b^12 - 2/b^6 - (2 Exp[-(a - b)] + 2 Exp[-(b - a)] - 2 Exp[-(2 (a - b) - 2 (b - a))*Cos[b]])]; Grid[{{...


8

I would link one of the controls to the other. Make one (t) update the other (p) inside Dyanmic. Make the other update the one inside the body of Manipulate. Then track the other (p). I chose to track p because creating a custom slider is easier than creating a custom locator. f[x_] := Sin[x]; Manipulate[With[{x0 = Clip[p[[1]], {0.001, 20}]}, t = x0; ...


8

As mentioned in the comments, using Interpolation[] is likely your best bet. It behaves strangely with certain location positions, but still makes for a useful demonstration. Manipulate[LocatorPane[Dynamic[pts, (pts = #; pts = Function[pnt, {.25 Round[4 pnt[[1]]], .1 Round[10 pnt[[2]]]}] /@ pts) &], data = (Sort@pts); fx = ...


8

In this implementation, the function locator[x] takes the slider value as input. You can move the red box around as a locator and change its size with the slider. source = Import["ExampleData/rose.gif"]; locator[x_] := SetAlphaChannel[Image[Graphics[{Red, Rectangle[]}], ImageSize -> 10 x], 0.2]; Manipulate[Image[source, ImageSize -> 500], {{pt, {...


8

This is not a complete answer (nor a robust one, likely), but here's one option (basic method stolen from @Heike's answer here). Some notes on adding new points: For adding points, I used a right-click event so as not to interfere with the built-in pan/zoom/rotate for Graphics3D (I'm sure there's an obvious "nicer" way to do this, but this works for now). ...


7

Maybe this will help. I can adapt my answer here, using the second argument of Dynamic to set the position of the Locator to a point on a parametric curve. We can use FindMinimum to find the point on the parametric curve that is closest to the mouse. To speed things up, it helps to have good initial points for FindMinimium. To do that, we save the values ...


7

See if this works for you. I still have not figured why a second evaluation is needed. Will look into it later. i.e. after you evaluate first time and the manipulate comes up, you need to hit the cell one more time. Then it works well. I've seen this before and do not remember now the reason for it. Here is the code. To make it work fast, this is what I ...


7

You can attach multiple controls to a variable: Manipulate[Graphics[{Polygon[{pt1, pt2, pt3}]}, PlotRangeClipping -> False, Frame -> True, PlotRange -> {{0, 10}, {0, 10}}], {{pt1, {0, 0}}}, {{pt2, {0, 1}}}, {{pt3, {1, 1}}}, Row[{Control@{{pt1, {0, 0}}, {0, 0}, {10, 10}, Slider2D}, Control@{{pt2, {0, 1}}, {0, 0}, {10, 10}, Slider2D}, ...


7

I don't think you can change the compositing operator but you can dynamically change the appearance. A simple example: im = LinearGradientImage[]; DynamicModule[{pt = {10, 10}}, LocatorPane[Dynamic[pt], im, Appearance -> Graphics[{ Dynamic @ GrayLevel[1 - PixelValue[im, pt]], Disk[]}, ImageSize -> 20]]]


7

z= Import["http://www.mecourse.com/landinig/software/cdeconv/tric.png"]; Manipulate[Column@{Show[z, Graphics[{Opacity[.3, Yellow], Polygon[pts[[FindShortestTour[pts][[-1]]]]]}], ImageSize->400], u = ImageValue[z, If[Length@pts >= 3, Region`RegionCentroid[Polygon[pts[[FindShortestTour[pts][[-1]]]]]], Mean[pts]]]}, {{pts, {{1, 1}}}, Locator, ...


6

Manipulate does not remember the previous value of its dynamic variables. So we save their values in another variable. Then whenever the Manipulate begins another cycle, we compare the new values to the saved values. To lock the sliders together we need a function y=f[x] and its inverse x=g[y]. You have to supply both of these relationships; Mathematica ...


6

Is this what you are looking for? pic = Image@RandomReal[1, {5, 10}] x = {1.5, 1.5}; y = {2.5, 3.5}; max = Dimensions@ImageData@pic - .5; Column[{ LocatorPane[Dynamic@{x, y}, Colorize@Image[pic, ImageSize -> 1000], {{0.5, 0.5}, max, {1, 1}}], Dynamic@x, Dynamic@y, Dynamic@...


6

Does this work for you? DynamicModule[ {pts = Array[pt, 5], active}, Column@{ Dynamic@active, Dynamic@pts, LocatorPane[ Dynamic[ pts, { (active = First@FirstPosition[pts - #, _?(# != {0, 0} &)]; pts = #)&, (active = None) & } ], Graphics[{Yellow, Disk[{0, 0}, 2]}, PlotRange -> 2] ] } ] ...


6

You may use MousePosition set to the "Graphics" of the LocatorPane with Nearest. pts = {{1, 1}/2, {-1, 1}/2, {1, -1}/2}; selectedLocator = None; LocatorPane[ Dynamic[pts, { (selectedLocator = FirstPosition[pts, First@Nearest[pts, MousePosition[{"Graphics", LocatorPane}]]]) &, Automatic, None }], Graphics[{Yellow, Disk[{0, 0}, 2]}, ...


6

Here's a way to make a locator pane that allows you to move your points in the plane of the current ViewMatrix: pt = List @@@ ColorData["ThermometerColors"] /@ Subdivide[50]; transf = {{1.1`, 0.4`, 0.`, -0.8`}, {-0.2`, 0.5`, 1.`, -0.7`}, {-0.4`, 1.`, -0.5`, 3.4`}, {0.`, 0.`, 0.`, 1.`}}; proj = {{2.2`, 0.`, 0.5`, 0.`}, {0.`, 2.2`, 0.5`, 0.`}, {0.`, ...


5

Manipulate[x = ConstantArray[0, 9 {1, 1}]; Row[{EventHandler[Dynamic[tds = Reverse[Transpose[x]]; MatrixPlot[tds, PlotRangePadding -> 0, Mesh -> All, ImageSize -> {300, 300}, ColorRules -> {1 -> Black, 0 -> None}]], {"MouseClicked" :> (pos = Ceiling[MousePosition["Graphics"]]; x = ReplacePart[...


5

I think it is fun and maybe even useful for domestic purposes so this is my approach. The following code allows you to easily manage rotation of objects. So it is reffering to your second question. I have not put this in neat DynamicModule because it is not finished, I consider it a sample piece of code :) x = {0, 0}; y = {1, 1}; (*starting positions*) Do[...


5

I think it's an interesting question - even if I wouldn't use Mathematica for this purpose. I'm assuming you're not interested so much in the interactive drawing tools (you can group things together, but there's no numerical interactivity like you'd find in a CAD program). Sticking with 2D to start with: chair = {Gray, Rectangle[{0, 0}, {.5, .5}]}; table = {...


5

Your code seems to intend to do something a little different than what I understood from the question - so I hope this is right. :) Fixing the code require two changes, remove Deploy -- it makes the EventHandler inoperative; and put Dynamic around the argument pt -- one of the important attributes of Dynamic is that it is HoldFirst, which allows the symbol ...


5

Try this (based on your previous Q&A) f[t_] := t {Cos[10 t], Sin[10 t]} v = 3; ptt = Append[f[#], v #] &[1.0]; plot = ParametricPlot[f[t], {t, 0, 2}]; ptfn = Nearest@Table[Append[f[t], v t], {t, 0, 2, 0.01}]; Show[plot, Graphics[{Locator[ Dynamic[Most[ptt], (ptt = First@ptfn@Append[#, Last[ptt]]) &]]}]] I add t as the third element to ...


5

This seems to be a duplicate but I can't find it :). Meanwhile, you can use second argument of Dynamic. x = {0, 0}; y = {1, 1}; w = y - x; Deploy@Graphics[{ Locator@Dynamic[x, (x = #; y = x + w;) &], Locator@Dynamic[y, (y = #; w = y - x;) &], Dynamic@Arrow[{x, y}] } ,...


5

Just replace GraphicsRow to Row! Manipulate[ Row@{ListLinePlot[{x, {0, 0}}, PlotRange -> {{0, 2}, {0, 2}}, ImageSize -> 200], ListLinePlot[{x, {2, 2}}, PlotRange -> {{0, 2}, {0, 2}}, ImageSize -> 200]}, {{x, {0, 0}, {2, 2}}, Locator}] Also instead of ControlType -> Locator you can use just Locator. P.S. Does anybody know how to ...


5

Here's a slight variation in Nasser's that's too long to explain in a comment. I changed several things. Think of each thing as a suggestion. The use of ControlActive is helpful in such cases as this, where the updating during active control manipulation needs to be quicker. Here the number of lines drawn is reduced during dragging, which speeds up ...


5

shape1 := Graphics[{#, Circle[{0, 0}, 1.5], Disk[]}, ImageSize -> 10] &; shape2 := Graphics[{Lighter@#, Disk[]}, ImageSize -> 10] &; ClearAll[lOF]; lOF[nOfOverlays_, colors_List, opts : OptionsPattern[]] := DynamicModule[{layer = 1, pts = ConstantArray[{{100, 100}, {700, 700}}, nOfOverlays], col = ...


Only top voted, non community-wiki answers of a minimum length are eligible