# How to draw a line on an existing region using RegionPlot with manipulate?

line1 = Table[{x, z + 1}, {x, 0, 2}]; Manipulate[ RegionPlot[x^2 + y^2 + z < 1, {x, 0, 2}, {y, 0, 2}, Epilog -> {Red, Line[line1]}], {{z, 0.5}, 0, 1}] when I add an new variable using manipulate, it will be wrong. what should I do? • hard to copy code from an image so it would be better to also paste the source code used. Jun 1 at 2:23
• Thank you. I have added it.line1 = Table[{x, z + 1}, {x, 0, 2}]; Manipulate[ RegionPlot[x^2 + y^2 + z < 1, {x, 0, 2}, {y, 0, 2}, Epilog -> {Red, Line[line1]}], {{z, 0.5}, 0, 1}] Jun 1 at 2:29

Next time please paste the code used. Since it is small, I retyped it now.

The problem is that you have global z and control variable also called z. These should not be mixed. Here is one workaround. Other workaround are possible. And always use TrackedSymbols line1=Table[{x,z+1},{x,0,2}]
Manipulate[
RegionPlot[x^2+y^2+z0<1,{x,0,2},{y,0,2},Epilog->{Red,Line[line1/.z->z0]}],
{{z0,0},0,1},
TrackedSymbols:>{z0}
]


I do not like mixing globals with Manipulate. Better is to use a Module inside Manipulate. Something like this and now you do not have to worry about different contexts

Manipulate[
Module[{x, y, line1},
line1 = Table[{x, z + 1}, {x, 0, 2}];
RegionPlot[x^2 + y^2 + z < 1, {x, 0, 2}, {y, 0, 2},
Epilog -> {Red, Line[line1]}]
],
{{z, 0, "z"}, 0, 1, .1, Appearance -> "Labeled"},
TrackedSymbols :> {z}
]


Update

When I change the x of line1 to start from 0.5 and end to 2, why only the line from 0.5 to 1.5 is displayed?

Because in your table the default increment is 1. Change it to the following: (ps. no need to use z0 anymore since we moved the code to inside the Manipulate.

Compare:

line1 = Table[{x, y}, {x, 0, 2}] with

 line1 = Table[{x, y}, {x, 1/2, 2}] So if you want to go to 2 in the second case need to tell it to use increment of 1/2 and not 1.

 line1 = Table[{x, y}, {x, 1/2, 2, 1/2}]  Manipulate[
Module[{x, y, line1},
line1 = Table[{x, z + 1}, {x, 0.5, 2, .1}];
RegionPlot[x^2 + y^2 + z < 1, {x, 0, 2}, {y, 0, 2},
Epilog -> {Red, Line[line1]}]
],
{{z, 0, "z"}, 0, 1, .1, Appearance -> "Labeled"},
TrackedSymbols :> {z}
]

• Got it. Thank you very much. Jun 1 at 2:44
• I have a new problem. When I change the x of line1 to start from 0.5 and end to 2, why only the line from 0.5 to 1.5 is displayed? Manipulate[ Module[{x, y, z, line1}, line1 = Table[{x, z + 1}, {x, 0.5, 2}]; RegionPlot[x^2 + y^2 + z0 < 1, {x, 0, 2}, {y, 0, 2}, Epilog -> {Red, Line[line1 /. z -> z0]}]], {{z0, 0, "z"}, 0, 1, .1, Appearance -> "Labeled"}, TrackedSymbols :> {z0}] Jun 1 at 3:25
• @hu_rrr see update. hopefully this does what you want. Jun 1 at 4:19
• Yes, it's really helpful! Jun 1 at 6:37