My problem is:
I want two Locator
s to simulate a vector in the following sense:
The first
Locator
is the base and the second the tip of the vector.When I move the tip, the base does not move (hence the vector changes).
When I move the base, the vector is unchanged, and therefore the tip (and the
Locator
) moves.
How can I achieve that?
I have tried storing the previous value of the base and then test if the current value is different. And if so updated the position of the tip. I can however not make that work, when using 'Module'. I suspect that there is a more elegant solution.
Answer
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}]
}
, PlotRange -> 2]
In case of multiple vectors one may want to save space and make code more transparent so we can use extended version of Dynamic
second argument to achieve this:
Deploy@Graphics[{
Locator@Dynamic[x, {(w = y - x;) &, (x = #; y = x + w) &, None}],
Locator@Dynamic[y],
Dynamic@Arrow[{x, y}]
}
, PlotRange -> 2]
Now we are working only with base, moreover w
can be scoped to particular Locator
.
There is huge advantage of the second method, well, not exactly the method but the usage of f_start
and f_end
. You can calculate w
once, not all the time you are dragging the Locator
.
Comments
Post a Comment