Programming Tasks 代考，85+高分通过
Simulations of fluids and solids are often conducted by discretising the domain of interest into a mesh of triangles. Each triangle has three vertices, and each vertex is usually a part of several triangles so that the mesh joins up. For example, the following is a mesh of the Great Lakes on the border between the US and Canada.
We can immediately see from this figure that this mesh is not connected: there are three distinct regions that are not linked. The objective in this question is to write a class which detects the disconnected parts of a mesh.
For the purposes of this exercise, a mesh is a set of v vertices and c triangles. The vertices are represented by an v x 2 array of float s containing the vertex locations: each row of this array is the 2-dimensional coordinates of a single vertex. The triangles are represented by a c x 3 array of int s representing the indices in the vertex list of the vertices making up each triangle. So if row 42 of the triangle array comprises 24, 0, 1004 then this means that triangle 42 is made up of vertices 24, 0, and 1004. One vertex may form part of several triangles, and this is how the mesh connects.
In the mesh_tools package, create a module mesh_tools.py containing a class Mesh . The constructor of this mesh should take two arguments points and triangles and should store them as attributes with the same names.
Create a class mesh_tools.mesh_tools.Adjacency . The constructor of this class should take a Mesh as input. The role of Adjacency is to record which vertices are adjacent. Two vertices are adjacent if they are both in the same triangle for some triangle in the mesh.
In the Adjacency constructor, store a sequence of v set s: one set per vertex. The i -th
set in this sequence should contain the indices (numbers) of the vertices adjacent to the i -th vertex.
Implement the __getitem__() special method on Adjacency , so that if a is an
Adjacency then a[i] returns the set of vertices adjacent to the i -th vertex.
Implement a Mesh.partition() method which takes no arguments other than the object itself, and returns a v -long sequence of integers. That is, one number for each vertex in the mesh. If vertices i and j are in the same connected region of the mesh, they should have the same number. If they are in different connected regions they should have different numbers.
The implementation of this exercise is largely an exercise in set operations. The algorithm is as follows:
At the start, all vertices are in a set of unvisited vertices. There is also a set of vertices to visit next, which is empty
A correct solution will have algorithmic complexity that is linear in c , the number of triangles in the mesh. In addition to the tests, a plotting script is provided for you to check your work. Running the following code:
$ python plot_partition.py
should produce a figure similar to the following. Note that the colours may be different depending on which number your algorithm gave to each lake. Note also that if you have your editor in full screen mode, the figure may appear on another workspace.