1
$\begingroup$

Consider two figures: a rectangle with coordinates $(x_{min},y_{min}), (x_{min}+dx,y_{min}+dy)$, and a circle. The latter has a radius $R$ and is located at the center of the coordinate frame. How to find the intersection points between these figures?

My attempt is the following (given in terms of some parameters):

ROffAxisArb[\[Theta]_, ztodet_] = ztodet*Tan[\[Theta]];
rec[xtodet_, ytodet_, dxdet_, dydet_] := 
 Rectangle[{xtodet, ytodet}, {xtodet + dxdet, ytodet + dydet}]
circ[\[Theta]_, ztodet_] := 
 Circle[{0, 0}, ROffAxisArb[\[Theta], ztodet]]
solEasy[xtodet_, ytodet_, ztodet_, dxdet_, dydet_, \[Theta]_] := 
 Solve[{x, y} \[Element] 
    rec[xtodet, ytodet, dxdet, dydet] && {x, y} \[Element] 
    circ[\[Theta], ztodet], {x, y}]
(*Longitudinal distance to detector*)
ZtoDet = 8.7;
(*Transverse distance to the beginning of detector - y*)
YtoDet = 2;
(*Transverse distance to the beginning of detector - X*)
XtoDet = -1;
(*Transverse size of the detector - width*)
DxDet = 8;
(*Transverse size of the detector - height*)
DyDet = 2;
solEasy[XtoDet, YtoDet, ZtoDet, DxDet, DyDet, 0.6]

It gives some expression $y = f(x)$ for some domain of $x$, which looks meaningless:

{{y -> ConditionalExpression[
    0.000283439 Sqrt[4.40966*10^8 - 1.24475*10^7 x^2], 
    4.40751 <= x <= 5.60591]}}

In contrast, I would expect either two points $\{x_{1},y_{1}\},\{x_{2},y_{2}\}$, or one point, or zero. For instance, graphically for $\theta = 0.6$ I get

lineStyle1 = {Thick, Blue};
lineStyle2 = {Thick, Blue};
lineStyle3 = {Thick, Darker@Darker@Green, Dashing[0.02]};
line1 = Line[{{XtoDet, YtoDet}, {XtoDet, YtoDet + DyDet}}];
line2 = Line[{{XtoDet + DxDet, YtoDet}, {XtoDet + DxDet, 
     YtoDet + DyDet}}];
Plot[{f[x, ROffAxisArb[0.6, ZtoDet]], 
  If[XtoDet < x < XtoDet + DxDet, YtoDet, -10], 
  If[XtoDet < x < XtoDet + DxDet, 
   YtoDet + DyDet, -10]}, {x, -1.2 (Abs[XtoDet] + Abs[DxDet]), 
  1.2 (Abs[XtoDet] + DxDet)}, 
 PlotRange -> {{-1.2 (Abs[XtoDet] + DxDet), 
    1.2 (Abs[XtoDet] + DxDet)}, {0, 1.2 (YtoDet + DyDet)}}, 
 Frame -> True, FrameStyle -> Directive[Black, 25], PlotRange -> All, 
 PlotStyle -> {{Thick, Red}, {Thick, Blue}, {Thick, Blue}}, 
 ImageSize -> Large, 
 FrameLabel -> {"x", 
   "y"},(*PlotLegends\[Rule]Placed[Style[#,24]&/@{"Subscript[\[Theta],\
 min]","Subscript[\[Theta], max]","Subscript[\[Theta], \
max,1]","Subscript[\[Theta], max,2]"},Right],*)AspectRatio -> 1, 
 Epilog -> {Directive[lineStyle1], line1, Directive[lineStyle2], 
   line2}]

enter image description here

Could you please tell me how to get just two points (or one point, or nothing) as a solution?

$\endgroup$

1 Answer 1

4
$\begingroup$

You should generally try to use Mathematica's built-in functions as much as possible. For example, if you let

rectangle = Rectangle[{1, 1}, {2, 2}];
circle = Circle[{0, 0}, 2];

Then

intersection = RegionBoundary@RegionIntersection[rectangle, circle]
(*Circle[{0, 0}, 2, {\[Pi]/6, \[Pi]/3}]*)

is an arc segment, as expected. The intersection points are at its endpoints, which you can extract with (didn't find a built-in function for this one):

ArcEndpoints[Circle[{x_, y_}, r_, {a1_, a2_}]] := Point/@{{x + r Cos@a1, y + r Sin@a1}, {x + r Cos@a2, y + r Sin@a2}}

To visualize the result, use for example

Show@Graphics@{rectangle, circle, Red, ArcEndpoints@intersection}

to get

rectanglecircleintersection

Note: I'm probably overlooking multiple edge-cases (one-point intersection, no-intersection, etc), but you get the idea...

$\endgroup$

Your Answer

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

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