Randomly generating points using probability from raster graph data?

1 view (last 30 days)
Hello,
I am trying to generate random points within a polygon based on values from a raster graph on that polygon - essentially the areas in the polygon with higher raster values will be the areas where points are generated more frequently than empty areas.
I am able to generate random points within a polygon; the code for this I have listed below: This generates two random points within a polygon to check if the line between them falls within the polygon.
I want to modify this script to generate random points based on a raster graph placed within the polygon. This script generates random points inside the bounding box of the shape and then throws them out if they are not within the polygon.
Does anyone know how I might go about this? I appreciate any feedback or ideas! I've been searching for many hours for a solution to this issue.
---------------------------------
M = shaperead('Shape.shp'd);
hold on
mapshow(M)
xrange=abs(M.BoundingBox(2,2)-M.BoundingBox(1,2));
yrange=abs(M.BoundingBox(2,1)-M.BoundingBox(1,1));
iterate = 100;
a=M.BoundingBox(2,1);
b=M.BoundingBox(1,1);
c=M.BoundingBox(2,2);
d=M.BoundingBox(1,2);
C = zeros(1,iterate);
count = 0;
for j=1:iterate
n=2;
PX = zeros(1,n);
PY = zeros(1,n);
for i=1:n
flagIsIn = 0;
while ~flagIsIn
PX(1,i) = (b-a).*rand(1,1) + a;
PY(1,i) = (d-c).*rand(1,1) + c;
flagIsIn = inpolygon(PX(1,i),PY(1,i),M.X,M.Y);
end
L = polyxpoly(PX,PY,M.X,M.Y);
if numel(L) == 0
count = count + 1;
end
end
% scatter(PX,PY,'r','*')
end
convex(1,d) = count/iterate
------------------

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!