在 matter.js 中考虑这个函数:
/**
* Creates a new set of axes from the given vertices.
* @method fromVertices
* @param {vertices} vertices
* @return {axes} A new axes from the given vertices
*/
Axes.fromVertices = function(vertices) {
var axes = {};
// find the unique axes, using edge normal gradients
for (var i = 0; i < vertices.length; i++) {
var j = (i + 1) % vertices.length,
normal = Vector.normalise({
x: vertices[j].y - vertices[i].y,
y: vertices[i].x - vertices[j].x
}),
gradient = (normal.y === 0) ? Infinity : (normal.x / normal.y);
// limit precision
gradient = gradient.toFixed(3).toString();
axes[gradient] = normal;
}
return Common.values(axes);
};
为了完成,这里是 Common.values() 函数:
/**
* Returns the list of values for the given object.
* @method values
* @param {} obj
* @return {array} Array of the objects property values
*/
Common.values = function(obj) {
var values = [];
if (Object.keys) {
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
values.push(obj[keys[i]]);
}
return values;
}
// avoid hasOwnProperty for performance
for (var key in obj)
values.push(obj[key]);
return values;
};
我不太清楚轴对象的结构。我没有看到axes[gradient] = normal
代码的意义,因为Common.values() function
它只返回值,因此永远不会返回渐变?