# How to create a streamplot only inside a polygon?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-streamplot-only-inside-a-polygon/110273>\
**Category:** New to Julia\
**Tags:** makie\
**Created:** [February 15, 2024, 10:47pm UTC](https://discourse.julialang.org/t/how-to-create-a-streamplot-only-inside-a-polygon/110273 "2024-02-15T22:47:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Midez](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Midez](https://discourse.julialang.org/u/Midez)\
**Post date:** [February 15, 2024, 10:47pm UTC](https://discourse.julialang.org/t/how-to-create-a-streamplot-only-inside-a-polygon/110273/1 "2024-02-15T22:47:06Z")

</div>

Say I got a triangle defined by

```julia
using CairoMakie, GeometryBasics, DelaunayTriangulation

P = [(-1,0),(1,0),(0,1),(-1,0)]

```

Now let’s triangulate the triangle to get equal points **(x,y)** indside

```julia
boundary_nodes, points = convert_boundary_points_to_indices(P)

tri = DelaunayTriangulation.triangulate(points)

refine!(tri; max_area=1e-2get_total_area(tri))

pts = [Point(val[1], val[2]) for val in tri.points]
fcs = [TriangleFace(val[1], val[2], val[3]) for val in tri.triangles]

msh = GeometryBasics.Mesh(pts, fcs)

x = [val[1] for val in tri.points]

y = [val[2] for val in tri.points]

```

Finally let’s just define a test-function f that assigns a value based on the point **(x,y)**

```julia
f(x,y) = Point2f(x,y)

```

After all If I run

```julia
streamplot(f, x, y)

```

I get streamlines also outside of the triangle even if **x** and **y** are points only from the inside.

 ![streamovertri](https://global.discourse-cdn.com/julialang/original/3X/f/5/f5c633afecb449f797b4c17155dd2e23289c5bec.png)

Is there a way to restrict the lines so that they don’t reach outside? This would help me tremendously with my project. Cheers!

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [February 15, 2024, 11:15pm UTC](https://discourse.julialang.org/t/how-to-create-a-streamplot-only-inside-a-polygon/110273/2 "2024-02-15T23:15:13Z")

</div>

Well…you can’t really restrict the lines easily, but what you _can_ do is plot a white polygon with a triangular hole over the plot.

Consider something like:

```julia
fx(x,y) = Point2f(x,y)
f, a, p = streamplot(fx, -1..1, 0..1)
tri = Point2f[(-1,0),(1,0),(0,1),(-1,0)]
out = Rect(-5, -5, 10, 10)
mask_poly = Makie.GeometryBasics.Polygon(
           Makie.convert_arguments(Makie.PointBased(), out)[1], # convert the rectangle to a vector of points
           [tri]
 )
poly!(a, mask_poly; color = :white, xautolimits = false, yautolimits = false) # so that the mask does not interfere with the limits

translate!(a.scene, 0, 0, -100) # move the plots below the grid so the grid is seen!
f

```

Unfortunately Discourse isn’t letting me upload an image, but this worked sufficiently on my machine!

---

<div class="post-metadata">

**Author:** ![Midez](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Midez](https://discourse.julialang.org/u/Midez)\
**Post date:** [February 15, 2024, 11:20pm UTC](https://discourse.julialang.org/t/how-to-create-a-streamplot-only-inside-a-polygon/110273/3 "2024-02-15T23:20:13Z")

</div>

Alright thank you 😄 This may be the easiest solution. Modifying the streamplot-code so that it adapt to boundaries might take way too much effort.
