I'm writing a little function to quickly draw some bezier-curves and lines on diagrams. The function opens a DialogInput. The user can use Locators to draw a line onto the diagram. When the user presses "Add curve", the graphics primitive of the drawn line is appended to the "primitives"-list. The user can add as many curves as they like. The function can either return the graphics primitives or the control points.
As the user adds more lines onto the diagram, I want all added lines to appear on the screen, but I couldn't make it work. Currently, the screen only shows the line that is currently being edited.
In the code, you can see that the Dynamic[]-function doesn't work with the graphics primitives. I tried multiple different approaches. The AppendTo[]-function also doesn't like to append anything to a dynamic expression. So at the time, I am out of ideas to make it work.
Options[curveLocator] = {
"Background" ->
Graphics[{}, PlotRange -> {{-1, 1}, {-1, 1}}, Frame -> True],
"Locators" -> 4
};
curveLocator[OptionsPattern[]] :=
DialogInput[
DynamicModule[{n, points, allPoints, primitives, function}, (
function = BezierCurve;
n = OptionValue["Locators"];
points = Table[{0, 0}, {i, 1, n}];
primitives = {};
allPoints = {};
Column[Flatten@{
(*Graphics*)
Show[Flatten@{
OptionValue["Background"],
Graphics[Flatten@{
Locator[Dynamic[points[[#]]]] & /@ Range[n],
(*This works perfectly*)
Dynamic[function@points],
(*This doesn't work*)
Thin,
Dynamic[primitives]
}]
},
ImageSize -> Medium
],
(*Controls*)
Text[Style["Control points", Bold, 16]],
Row[{Text["Punkte " <> ": "],
InputField[Dynamic[points], FieldSize -> {20, 5}]}],
Text[Style["Grafikdirektive", Bold, 16]],
Row[{
PopupMenu[Dynamic[function],
{BezierCurve, Line, Point}
],
InputField[Dynamic[function]]
}],
Row[{
Button["Add curve", (
AppendTo[primitives, Dynamic[function@points]];
AppendTo[allPoints, Dynamic[points]];
)],
Dynamic[
Text["Added curves: " <> ToString@Length@primitives]]
}],
Row[{
Button["Return curves as graphics primitives",
DialogReturn@primitives],
Button["Return curves as points", DialogReturn@allPoints]
}]
}]
)]
]
If you have any advice for me, please let me know.
Thanks, Fabian