フィルターのクリア

Creating Random Points inside a created polygon which has no definite boundaries

5 ビュー (過去 30 日間)
Hello I am currently working on a bit of a project where I need to be able to create random points inside of an already created polygon that does not have known set boundaries. For my code provided below I am using the state of Washington that does not have any set boundaries for me to generate my points from. If I could get some proper guidance on the steps to take that would be wonderful. Thank you!
states = shaperead('usastatehi.shp');
wa = p(45); %creates a polgon in the shape of Washington State
numPointsIn = 20;
for i = 1:numPointsIn
flagIsIn = 0;
while ~flagIsIn
x(i) = rand(1);
y(i) = rand(1);
flagIsIn = polyshape(wa);
end
end
plot(wa,'r+')

採用された回答

Walter Roberson
Walter Roberson 2017 年 11 月 29 日
Caution: the below code probably has problems for areas that cross 180E/W:
states = shaperead('usastatehi.shp');
st = states(45); %creates a polgon in the shape of Washington State
stBB = st.BoundingBox;
st_minlat = min(stBB(:,2));
st_maxlat = max(stBB(:,2));
st_latspan = st_maxlat - st_minlat;
st_minlong = min(stBB(:,1));
st_maxlong = max(stBB(:,1));
st_longspan = st_maxlong - st_minlong;
stX = st.X;
stY = st.Y;
numPointsIn = 20;
for i = 1:numPointsIn
flagIsIn = 0;
while ~flagIsIn
x(i) = st_minlong + rand(1) * st_longspan;
y(i) = st_minlat + rand(1) * st_latspan;
flagIsIn = inpolygon(x(i), y(i), stX, stY);
end
end
mapshow(st, 'edgecolor', 'r', 'facecolor', 'none')
hold on
scatter(x, y, '.')
hold off
  2 件のコメント
Ian Bouchard
Ian Bouchard 2017 年 11 月 29 日
When viewing the shape it has a bunch of random points in the bottom right of the graph that seem to have no correlation to the rest of the points since it is the same formation of points each time. What do you think could be causing this?
Walter Roberson
Walter Roberson 2017 年 11 月 29 日
I think it is just left-over on your axes. mapshow() happens to construct the patch object in a way that does not clear the axes. You can add in a cla() before the mapshow()

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeComputational Geometry についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by