필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
Ian Bouchard
Ian Bouchard 2017년 12월 4일
답변: Roger Stafford 2017년 12월 4일
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