selecting a triangle in an array of delaunay points

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 月 16 日
I think part of your problem is one of definition.
1. How do you define the nearest triangle? If a point falls inside the triangulation, then the nearest triangle might logically be that triangle which contains the point. What if the point falls outside?
2. Once you have clearly defined what "nearest" means in your context, then how will you implement that?
I don't see the answers to either of these questions in your post, and without those steps you must be stuck.
luc
luc 2014 年 12 月 16 日
編集済み: luc 2014 年 12 月 16 日
The nearest triangle is defined in:
triangleId=pointLocation(dt, random_x,random_y)
This should get the closest triangleID from subset "dt" o the point randomx randomy.
The next part, selecting the triangle to be painted green is in the "patch" function, grab the triangle ID, get the triangle coordinates (3points) and fill in the space between these points.
John D'Errico
John D'Errico 2014 年 12 月 16 日
Again, that only works if the point is inside the convex hull of your point set.
Note that your set does not fill a rectangle, yet your random point generator choose points from the rectangle that contains your data. So a reasonable fraction of the time, pointLocation will fail. What do you think it returns when it cannot find an enclosing triangle? Read the help, but I don't think you should be surprised.
luc
luc 2014 年 12 月 16 日
Ill check tommorrow if that is the problem, if it is, thanks :). If it aint, I'll post here.
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 日

0 投票

Answered, thanks!

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDelaunay Triangulation についてさらに検索

質問済み:

luc
2014 年 12 月 16 日

回答済み:

luc
2014 年 12 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by