1
$\begingroup$

I am attempting to create a dynamic contour plot of an ellipse with two locators as the foci within a locator pane. The following code has been implemented:

a:= Sqrt[c^2 + b^2]
b = 1;
c = 0;

ellipse := (x^2)/(a^2) + (y^2)/(b^2)
plot := Dynamic[ContourPlot[ellipse == 1, {x,-2,2}, {y,-2,2}]

LocatorPane[Dynamic[{ {c,0}, {-c,0} }], plot]

When I evaluate this, the plot behaves pretty much as desired except that only one of the locators can be moved.

This is because I placed -c inside Dynamic as opposed to just the variable c. Ordinarily, I would just do something like Dynamic[{-c,0}, (c = -#)&], however, LocatorPane requires that I include the full list of locators within a single Dynamic.

I want to find a way to apply a function to Dynamic that causes the first component of the second locator to take on the value of -c while the first locator is using c.

Any help?

$\endgroup$
1
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ Commented Feb 19, 2015 at 14:50

1 Answer 1

2
$\begingroup$
b = 1;
ellipse[x_, y_, pt_] := (x^2)/(pt[[1]]^2 + b^2) + (y^2)/(b^2)
DynamicModule[{pt = {0, 0}}, 
 LocatorPane[Dynamic[pt], 
             Dynamic@ContourPlot[ellipse[x, y, pt] == 1, {x, -2, 2}, {y, -2, 2}, 
                                Epilog -> {PointSize[Large], Point[Dynamic[{First[pt], 0}]], 
                                           Point[Dynamic[{-First[pt], 0}]]}], Appearance -> None]]

Mathematica graphics

$\endgroup$
9
  • $\begingroup$ Clean! +1. Btw. why Epilog needs Dynamic@Locator in: x = {0, 0}; { Graphics[{Locator@Dynamic@x}], Graphics[{}, Epilog -> {Locator@Dynamic@x}], Dynamic[x] } $\endgroup$
    – Kuba
    Commented Feb 19, 2015 at 15:19
  • $\begingroup$ @Kuba Thanks:)! You need the Dynamic[] to update and the Locator[] to know what to display. You may change the Locator[] by Point[], for example. $\endgroup$ Commented Feb 19, 2015 at 15:23
  • $\begingroup$ @Kuba Oh, I see now. Not sure, let me think (or post a question!) $\endgroup$ Commented Feb 19, 2015 at 16:18
  • $\begingroup$ @Kuba There was a question about "how deep" Dynamic "sees" when it acts on a expression. I believe it is related to that $\endgroup$ Commented Feb 19, 2015 at 16:24
  • $\begingroup$ @belisarius any idea which one? :) $\endgroup$
    – Kuba
    Commented Feb 20, 2015 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.