I want to find the intersection point of two line segments determined by the position of four locators.
l11 = {0, 0};
l12 = {4, 4};
l21 = {0, 4};
l22 = {4, 0};
l1 = Line[{Dynamic[l11], Dynamic[l12]}];
l2 = Line[{Dynamic[l21], Dynamic[l22]}];
Graphics[{Locator[Dynamic[l11]],
l1,
Locator[Dynamic[l12]],
Locator[Dynamic[l21]],
l2,
Locator[Dynamic[l22]],
PointSize[3], RegionIntersection[{l1, l2}]},
PlotRange -> 10, Axes -> True]
Mathematica gives an error:
Line[{{0, 0}, {4, 4}}] is not a correctly specified region
As far as I understood, the error is caused by my Line expressions containing Dynamic expressions.
How can I fix my code to show the intersection of two such line segments?
Answer
You can fix your code by rewriting it only in terms of the four points where the locators are positioned.
DynamicModule[{l11 = {0., 0.}, l12 = {4., 4.}, l21 = {0., 4.}, l22 = {4., 0.}},
Dynamic @
Graphics[
{Locator[Dynamic[l11]], Line[{l11, l12}], Locator[Dynamic[l12]],
Locator[Dynamic[l21]], Line[{l21, l22}], Locator[Dynamic[l22]],
Red, AbsolutePointSize[8],
RegionIntersection[{Line[{l11, l12}], Line[{l21, l22}]}]},
PlotRange -> 10,
Axes -> True]]

Comments
Post a Comment