Returning value if a point is in a region?

1

2

I'm writing a function so that if the user clicks on a certain region, a value is returned. The function takes in a condition list like the one below and a point.

condition = {(x + .5)^2 + y^2 < 1, (x - .5)^2 + y^2 < 1};

P1 = RegionPlot[condition[[1]] && condition[[2]], {x, -2, 2}, {y, -2, 2}, 
PlotStyle -> Red];  
P2 = RegionPlot[condition[[1]] && ! condition[[2]], {x, -2, 2}, {y, -2, 2}, 
PlotStyle -> Red];  
P3 = RegionPlot[condition[[2]] && ! condition[[1]], {x, -2, 2}, {y, -2, 2}, 
PlotStyle -> Red];

I can't figure this out at all. Can someone help me?

studentguest

Posted 2015-04-25T14:36:10.043

Reputation: 11

What and what ? Clarify the problem as right now you have so much info scattered across the post with no explicit connection to it ( the problem ) – Sektor – 2015-04-25T14:44:22.390

I edited the question. Does this make more sense? – studentguest – 2015-04-25T15:23:24.760

Answers

1

On one level, I'm not entirely sure how you want your function to work. It's not clear where the user is to click, when the function is called, and possibly (in the solution I propose below) where the return value of the function is to be stored.

The closest thing appears to me to be a ClickPane. ClickPane calls a function when it is clicked. The function can stored the calculated value in an external variable. Code inside a Dynamic can then be updated with the new value resulting from the click. Here is a simple example. The function returns a number associated with each region P1, P2, P3; the value 0 corresponds to a click outside all regions.

P1 = ImplicitRegion[condition[[1]] && condition[[2]], {{x, -2, 2}, {y, -2, 2}}];
P2 = ImplicitRegion[condition[[1]] && ! condition[[2]], {{x, -2, 2}, {y, -2, 2}}];
P3 = ImplicitRegion[condition[[2]] && ! condition[[1]], {{x, -2, 2}, {y, -2, 2}}];
ClickPane[
 Show[BoundaryDiscretizeRegion[#, Method -> "Continuation"] & /@ {P1, P2, P3}],
 (foo = Which[
     # ∈ P1, 1,
     # ∈ P2, 2,
     # ∈ P3, 3,
     True, 0]) &
 ]
Dynamic[foo]

Michael E2

Posted 2015-04-25T14:36:10.043

Reputation: 190 928

0

RegionPlot[{
  Mouseover[(x + .5)^2 + y^2 < 1 && (x - .5)^2 + y^2 < 1, 
   Text[Style["My first region", 16]]], 
  Mouseover[(x + .5)^2 + y^2 < 1 && ! (x - .5)^2 + y^2 < 1, 
   Text[Style["My second region", 16]]],
  Mouseover[! (x + .5)^2 + y^2 < 1 && (x - .5)^2 + y^2 < 1, 
   Text[Style["My third region", 16]]]
  },
 {x, -2, 2}, {y, -2, 2}]

or

RegionPlot[{
  Tooltip[(x + .5)^2 + y^2 < 1 && (x - .5)^2 + y^2 < 1, 
   Text[Style["My first region", 16]]], 
  Tooltip[(x + .5)^2 + y^2 < 1 && ! (x - .5)^2 + y^2 < 1, 
   Text[Style["My second region", 16]]],
  Tooltip[! (x + .5)^2 + y^2 < 1 && (x - .5)^2 + y^2 < 1, 
   Text[Style["My third region", 16]]]
  },
 {x, -2, 2}, {y, -2, 2},
 PlotStyle -> {Red, Green, Blue}]

David G. Stork

Posted 2015-04-25T14:36:10.043

Reputation: 31 784