How do you create a random point generation with the values of 1 or 2?

2 次查看(过去 30 天)
Hello, So I have a 2 point question. The first question I have is how do you generate random points with the value of either 1 or 2? I am currently creating a code where I create random points inside a polygon and then use delaunay triangulation on the points. After creating the triangulation I then find the incenter point of each triangle which leads me to my next question. How do you average the three points of each triangulation and then average them into a single value which is the incenter? Thank you so much for your help!!
P.S. I have received much help on this code and could use a small explanation behind the steps used to get there. Thank you!
states = shaperead('usastatehi.shp');
st = states(47); %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 = 42;
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 , '.')
dt=delaunayTriangulation(x',y')
IC=incenter(dt)
dt1=delaunayTriangulation(IC)
hold on
triplot(dt, '--g')
triplot(dt1, '--r')
[XV, YV] = voronoi(IC(:,1),IC(:,2 ));
plot(XV,YV,'k')
axis([min(stX) max(stX) min(stY) max(stY)])

回答(1 个)

Roger Stafford
Roger Stafford 2017-12-4
As to your question of how to find the location of the incenter of a triangle, given its three vertices, here is how. Let (x1,y1), (x2,y2), and (x3,y3) be the 2D cartesian coordinates of the three vertices of a triangle. Let (xc,yc) be the desired coordinates of its incenter - that is, the point which is an equal orthogonal distance from each of its three sides.
% The three side lengths
a = sqrt((x2-x3)^2+(y2-y3)^2);
b = sqrt((x3-x1)^2+(y3-y1)^2);
c = sqrt((x1-x2)^2+(y1-y2)^2);
% The desired cartesian coordinates of the incenter
xc = (a*x1+b*x2+c*x3)/(a+b+c);
yc = (a*y1+b*y2+c*y3)/(a+b+c);
In other words, the incenter is the "average" of the three vertices, weighted respectively by the triangle's opposite side lengths.

类别

Help CenterFile Exchange 中查找有关 Delaunay Triangulation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by