Multifaceted Patches
Example — Defining a Cube
A cube is defined by eight vertices that form six sides. This illustration shows thex-,y-, andz-coordinates of the vertices defining a cube in which the sides are one unit in length.
If you specify thex-,y-, andz-coordinate arguments as vectors, they render as a single polygon with points connected in sequence. If the arguments are matrices, MATLAB®draws one polygon per column, producing a single patch with multiple faces. These faces need not be connected and can be self-intersecting.
Alternatively, you can specify the coordinates of each unique vertex and the order in which to connect them to form the faces. The examples in this section illustrate both techniques.
Specifying X, Y, and Z Coordinates
Each of the six faces has four vertices. Because you do not need to close each polygon (i.e., the first and last vertices do not need to be the same), you can define this cube using a 4-by-6 matrix for each of thex-,y-, andz-coordinates.
Each column of the matrices specifies a different face. While there are only eight vertices, you must specify 24 vertices to define all six faces. Since each face shares vertices with four other faces, you can define the patch more efficiently by defining each vertex only once and then specifying the order in which to connect these vertices to form each face. The patchVertices
andFaces
properties define patches in just this way.
Specifying Faces and Vertices
These matrices specify the cube usingVertices
andFaces
.
Using the vertices/faces technique can save a considerable amount of computer memory when patches contain a large number of faces. This technique requires the formalpatch
function syntax, which entails assigning values to theVertices
andFaces
properties explicitly. For example,
patch('Vertices',vertex_matrix,'Faces',faces_matrix)
因为高级语法并不自动ly assign face or edge colors, you must set the appropriate properties to produce patches with colors other than the default white face color and black edge color.
Flat Face Color
Flat face color is the result of specifying one color per face. For example, using the vertices/faces technique and theFaceVertexCData
property to define color, this statement specifies one color per face and sets theFaceColor
property toflat
.
vert = [0 0 0;1 0 0;1 1 0;0 1 0;0 0 1;1 0 1;1 1 1;0 1 1]; fac = [1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8;1 2 3 4;5 6 7 8]; patch('Vertices',vert,'Faces',fac,... 'FaceVertexCData',hsv(6),'FaceColor','flat')
Adjust the axes:
view(3) axis vis3d
Because truecolor specified with theFaceVertexCData
property has the same format as a MATLAB colormap (i.e., an n-by-3 array of RGB values), this example uses thehsv
colormap to generate the six colors required for flat shading.
To map face colors to the current colormap, assign an n-by-1 array to theFaceVertexCData
property:
patch('Vertices',vert,'Faces',fac,... 'FaceVertexCData',(1:6)','FaceColor','flat')
Adjust the axes:
view(3) axis vis3d
Interpolated Face Color
Interpolated face color means the vertex colors of each face define a transition of color from one vertex to the next. To interpolate the colors between vertices, you must specify a color for each vertex and set theFaceColor
property tointerp
.
patch('Vertices',vert,'Faces',fac,... 'FaceVertexCData',hsv(8),'FaceColor','interp')
Adjust the axes:
view(3) axis vis3d
produces a cube with each face colored by interpolating the vertex colors.
To specify the same coloring using the x, y, z, c technique, c must be anm-by-n-by-3 array, where the dimensions of x, y, and z arem-by-n.
This diagram shows the correspondence between theFaceVertexCData
andCData
properties.
How Patch Data Relates to a Colormapdiscusses coloring techniques in more detail.