# Generate a interpolation function from a mesh with assigned value?

Posted 5 months ago
937 Views
|
5 Replies
|
4 Total Likes
|
 Hi all, I was trying to use Mathematica for preprocessing to generate a Voronoi mesh with assigned values. Here is my code to generate the mesh: noPoly = 20; boxSize = 400; pts = RandomReal[{-boxSize, boxSize}, {noPoly, 2}]; mesh = VoronoiMesh[pts, {{-boxSize, boxSize}, {-boxSize, boxSize}}] You will get something like:Now this is what I want to do:1) For each "grain" in the diagram, I want to assign a constant value for the whole grain. i.e., define a function f(x,y) on the mesh such that it is constant in the same grain but maybe different for different grains.2) Then I want to generate a list of x,y,f(x,y) for x,y are uniform grid points, like (1,0), (2,0), (1,1), (1,2).... If I can do this, I know how to generate text file for another software to read and generate this function.2) is easy if I could do 1), i.e. get the function f(x,y). I have check the Mathematica documentation but have no idea how to do this. It seems PropertyValue might help but how? How do I even get those value defined on a mesh?Thanks in advance!
5 Replies
Sort By:
Posted 5 months ago
 It seems no one have a solution yet.If it is not possible to assign value to a mesh, is there other ways to generate a labelled Voronoi diagram? If I could label them, maybe I can substitute the labels with my values?
Posted 5 months ago
 Can be done with Nearest. Here is that example. SeedRandom[123456] noPoly = 20; boxSize = 400; pts = RandomReal[{-boxSize, boxSize}, {noPoly, 2}]; mesh = VoronoiMesh[pts, {{-boxSize, boxSize}, {-boxSize, boxSize}}] We create the function using a random value for each Voronoi point. vals = RandomReal[1, noPoly]; nf = Nearest[pts -> "Index"]; fun[x_] := vals[[nf[x][[1]]]] We'll check it by making a large array of these values and using ArrayPlot to illustrate in color. I do some rearrangement to account for the (in my opinion, counterintuitive) ordering used by ArrayPlot. tt = Table[fun[{x, y}], {x, -400, 400}, {y, -400, 400}]; ArrayPlot[Transpose[Map[Reverse, tt]], ColorFunction -> "Rainbow"] 
Posted 5 months ago
 Hi Daniel,Thanks for the workable solution. I noticed the mesh is not being used in your solution. Does that mean there is no simple ways to extend this question to some arbitrary mesh or diagrams?