简体   繁体   中英

Matlab and color point specific areas

I have a grid with fixed points and random generated user's positions.

Distances for each point and user are measured from the beginning of the axis 0.0. I want to associate each user to the closest fixed point. I calculate both distance vectors and the min of them per user is pointing to the closest fixed point.

But i am stuck on finding a working way so each fixed point and associated user have something same in plot, pe same color and color area.

So my problem is two dimensional:

  1. First is to manage to associate each user to its closest fixed poind
  2. How to color the result.

Thank you.

For the point searching I would use dsearchn for this kind of thing. You can use it with or without delaunay triangulation depending on the ratio of users to fixed sites. I tend to use it the quick and easy way, which in your case would be:

indices_of_closest_fixed_points = dsearchn(fixed_points, user_points)

As for the colors I would suggest you define a color map using something like

mymap = lines(n) 

where n is the number of fixed points you have. You can then use scatter to plot the points with specific colors and sizes. Perhaps something like this to get you started:

x = user_points(1,:); 
y = user_points(2,:);
S = []; % point sizes, left empty for now
C = mymap(indices_of_closest_fixed_points,:); %colors

scatter(x,y,S,C);

To find the nearest point simply compute the euclidean distance between each user point and the complete set of fixed points. Then the index of the shortest distance will also be the index of the fixed point.

dist = calc_dist(fixedPts, aSingleUserPt)
[~, idx] = min(dist); 

To solve the color problem, you'll need to create a colormap from a fixed point index to a unique color. Then when you plot a user point you will set the color of the plot equal to the colormap evaluated at idx

Note the euclidean distance is very easy to calcuate:

euc_dist = sqrt( (x1 - x2)^2 + (y1 - y2)^2 );

There are functions on File Exchange that will let you compute this quickly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM