# How to manipulate a circle in GeoGebra style?

I want to emulate the GeoGebra app. I want to get an effect like this in Mathematica.

I don't know how to move whole circle without changing the radius of the circle.

My sample code：

Manipulate[
Graphics[{Circle[p, Norm[p2 - p]]}, PlotRange -> 5, Frame -> 1],
{{p, {0, 0}}, Locator},
{{p2, {2, 2}}, Locator}]

• Related, though almost the converse of this question: (56390) – Mr.Wizard Sep 15 '14 at 7:43
• Closely related 30354 – Kuba Sep 15 '14 at 13:26

This is what I find more intuitive:

circle[] := DynamicModule[{a = {0, 0}, b = {1, 0}, r = 1, w},
{
Dynamic@Circle[a, r],
Locator[Dynamic[a, {(w = b - a) &, (a = #; b = a + w) &, None}]],
Locator[Dynamic[b, (b = #; r = Norm[b - a]) &]]

}]

Graphics[circle[], Frame -> True, PlotRange -> 2]


And this is what fits well OP's example:

circle2[] := DynamicModule[{a = {0, 0}, b = {1, 0}, r = 1, s, p, hand},
{Thick,
EventHandler[
Dynamic@hand@Circle[a, r],
{"MouseDown" :> {s = {p[], a, b}; },
"MouseDragged" :> {{a, b} = (p[] - s[[1]] + #) & /@ Rest[s]}

}],
Locator[Dynamic[a, (a = #; r = Norm[b - a]) &]],
Locator[Dynamic[b, (b = #; r = Norm[b - a]) &]]}

,
Initialization :> (
p[] := MousePosition["Graphics"];
)]

Graphics[circle2[], Frame -> True, PlotRange -> 2]


You can improve styling ofc.

• This is nice, but it doesn't have the behavior the OP asks for. – m_goldberg Sep 15 '14 at 15:09
• Still seems wrong. Dragging the center locator doesn't change the radius. Dragging on the circle's circumference doesn't move it. – m_goldberg Sep 15 '14 at 16:47
• I have now. It meets the specs. I like it. But I don't like circle. I think it detracts from your answer to leave it in. – m_goldberg Sep 15 '14 at 17:30
• @m_goldberg Ok, removed :) – Kuba Sep 15 '14 at 17:37
• Personally, I preferred the original circle based version - which garnered my upvote. While it didn't replicate the GeoGebra behavior exactly, it certainly got the same idea across and with much simpler code. Furthermore, it's not clear from the question that an exact replication was necessary - at least not to me. – Mark McClure Sep 15 '14 at 17:43

I do nor know how to implement what you want to do in a Manipulate expression using locators, because I don't know how to handle mouse events in a Manipulate expression. However, if you are willing to accept an answer using EventHandler, the behavior you ask for isn't very difficult to implement.

With[{δ = .2},
DynamicModule[{p1 = {0, 0}, p2 = {2, 2}, mouse, action, dp},
EventHandler[
Dynamic @ Graphics[{
{PointSize[Large], Point[{p1, p2}]},
{Thick, Circle[p1, Norm[p2 - p1]]}},
Frame -> True,
PlotRange -> 5],
{"MouseDown" :>
(mouse = MousePosition["Graphics"];
action =
Which[
Norm[p1 - mouse] < δ , "p1",
Norm[p2 - mouse] < δ , "p2",
Abs[Norm[mouse - p1] - Norm[p2 - p1]] < δ , "circle"]),
"MouseDragged" :>
(mouse = MousePosition["Graphics"];
Switch[action,
"p1", p1 = mouse,
"p2", p2 = mouse,
"circle", dp = p2 - p1; p1 = mouse; p2 = p1 + dp])}]]]


The plan behind this code is

• A mouse-down event detects what visual object the mouse is near. This in turn sets what action should be done during the drag.

• A mouse-dragged event carries out the selected action.

• I like that Mouse-Action stuff! Cool. – user9660 Sep 15 '14 at 15:37
x = {3, 3};
y = {5, 3};
LocatorPane[Dynamic[{x, y}],
Dynamic@Graphics[{{Gray, Circle[x, Abs[y[[1]] - x[[1]]]]}, {Blue,
PointSize[0.02], Point[{x, {y[[1]], x[[2]]}}]}}, Axes -> True,
PlotRange -> {{-2, 8}, {-2, 8}}, AxesOrigin -> {0, 0}],
Appearance -> None]