필터 지우기
필터 지우기

selecting a triangle in an array of delaunay points

조회 수: 2 (최근 30일)
luc
luc 2014년 12월 16일
답변: luc 2014년 12월 18일
The code explains it all.
Try to run it, it might give you a good result, but that would be random.
This code is supposed to display a point, and color the nearest triangle green. It does not do this, and gives "nan" errors on seemingly random iterations of the loop.
I would appreciate some help :)
clc;
clear all;
close all;
P = [ 2.5 8.0
6.5 8.0
2.5 5.0
6.5 5.0
1.0 6.5
8.0 6.5];
dt=delaunayTriangulation(P);
triplot(dt);
hold on
%%example functions
dt.Points
dt.ConnectivityList
%abs vals x
a=min(P(:,1))
b=max(P(:,1))
%abs vals y
c=min(P(:,2))
d=max(P(:,2))
for n=1:5
random_x=a + (b-a).*rand(1);
random_y=c + (d-c).*rand(1);
triangleId=pointLocation(dt, random_x,random_y) %select the triangle ID of the triangle closest to the point generated by the random generator
scatter(random_x,random_y) %display the points
%%now... the part that does not work
%%selecting the right triangle and turning it a different color
tri = dt(triangleId, [1:end 1]);
patch(P(tri,1), P(tri,2), 'r', 'LineWidth',1, 'FaceColor','g')
end
  댓글 수: 6
John D'Errico
John D'Errico 2014년 12월 17일
My point is, pointLocation will return NaN in that case. SURPRISE!
How often will that event happen? As it turns out, that should happen roughly 21.429% of the time that a point falls outside of your triangulated region, but inside the rectangle that you use to generate the random points.
(1 - (8-2.5)*3/(3*(8-1)))*100
ans =
21.429
luc
luc 2014년 12월 17일
Thanks! This solved my problem. I think an addition to the source code of this function would be to still pick the nearest triangle to the point, accompanied by an error message saying that the point itself is not inside a triangle.

댓글을 달려면 로그인하십시오.

채택된 답변

luc
luc 2014년 12월 18일
Answered, thanks!

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by