0

Suppose I have a scatter plot showing points on a 2D plane. What I want to do is to create a regular polygon and color the points inside and at the boundary of the polygon differently. I would also like to return the coordinates of the points inside the polygon in an array.

How do I do this using Julia Plots?

Thanks

dbrane
  • 129
  • 5

1 Answers1

0

There are many algorithms to compute whether a point is contained within a polygon. They are fairly involved, and so I'll leave that to you if you'd like to recreate them. The package PolygonOps.jl include Point-in-poly testing, so I'd recommend starting there. You could sort the plotted points based on whether or not they are inside the polygon, and plot them with according colors. Then you could return the ones found to be inside. Below is an implementation.

using PolygonOps
using Plots

function do_poly_check(points,poly)
    p = plot();
    points_inside = []
    for i in points
        if PolygonOps.inpolygon(i,poly)!=0
            scatter!(i,color = :blue)
            push!(points_inside,i)
        else
            scatter!(i,color = :red)
        end
    end    

    display(p)
    return points_inside
end

An example in action would then look like this:

points = [
    (0,0),
    (0,1),
    (1,1),
    (2,1),
    (3,1),
    (4,2),
]

poly = [
    (0,0),
    (1,0),
    (1,1),
    (0,1),
    (0,0),
]

do_poly_check(points,poly)

Note that you did not specify to plot the polygon itself, although it could be done on the same scatterplot.

Jonathan F.
  • 215
  • 1
  • 11