API documents for value types (#4587)

This commit is contained in:
Leslie Leigh
2019-06-12 11:12:27 +08:00
committed by Weiliang Wang
parent 3359c9a30f
commit 7cdc43da80
11 changed files with 1281 additions and 1703 deletions
+62 -62
View File
@@ -31,56 +31,47 @@ import Vec2 from './vec2';
****************************************************************************/
/**
* !#en
* AffineTransform class represent an affine transform matrix. It's composed basically by translation, rotation, scale transformations.<br/>
* !#zh
* AffineTransform 类代表一个仿射变换矩阵。它基本上是由平移旋转,缩放转变所组成。<br/>
* 二维仿射变换矩阵,描述了平移、缩放和缩放。
*/
export default class AffineTransform {
/**
* !#en Create a AffineTransform object with all contents in the matrix.
* !#zh 用在矩阵中的所有内容创建一个 AffineTransform 对象。
* 创建二维放射变换矩阵。
* @param a a 元素。
* @param b b 元素。
* @param c c 元素。
* @param d d 元素。
* @param tx tx 元素。
* @param ty ty 元素。
* @returns `new AffineTransform(a, b, c, d, tx, ty)`
*/
public static create (a: number, b: number, c: number, d: number, tx: number, ty: number) {
return new AffineTransform(a, b, c, d, tx, ty);
}
/**
* !#en
* Create a identity transformation matrix: <br/>
* [ 1, 0, 0, <br/>
* 0, 1, 0 ]
* !#zh
* 单位矩阵:<br/>
* [ 1, 0, 0, <br/>
* 0, 1, 0 ]
* 创建单位二维仿射变换矩阵,它不进行任何变换。
*/
public static identity () {
return new AffineTransform();
}
/**
* !#en Clone a AffineTransform object from the specified transform.
* !#zh 克隆指定的 AffineTransform 对象。
* 克隆指定的二维仿射变换矩阵。
* @param affineTransform 指定的二维仿射变换矩阵。
*/
public static clone (t: AffineTransform) {
return new AffineTransform(t.a, t.b, t.c, t.d, t.tx, t.ty);
public static clone (affineTransform: AffineTransform) {
return new AffineTransform(
affineTransform.a, affineTransform.b,
affineTransform.c, affineTransform.d,
affineTransform.tx, affineTransform.ty);
}
/**
* !#en
* Concatenate a transform matrix to another
* The results are reflected in the out affine transform
* out = t1 * t2
* This function is memory free, you should create the output affine transform by yourself and manage its memory.
* !#zh
* 拼接两个矩阵,将结果保存到 out 矩阵。这个函数不创建任何内存,你需要先创建 AffineTransform 对象用来存储结果,并作为第一个参数传入函数。
* out = t1 * t2
* @param out Out object to store the concat result
* @param t1 The first transform object.
* @param t2 The transform object to concatenate.
* @return Out object with the result of concatenation.
* 将两个矩阵相乘的结果赋值给出口矩阵。
* @param out 出口矩阵。
* @param t1 左矩阵。
* @param t2 右矩阵。
* @returns `out`
*/
public static concat (out: AffineTransform, t1: AffineTransform, t2: AffineTransform) {
const a = t1.a;
@@ -99,10 +90,10 @@ export default class AffineTransform {
}
/**
* !#en Get the invert transform of an AffineTransform object.
* This function is memory free, you should create the output affine transform by yourself and manage its memory.
* !#zh 求逆矩阵。这个函数不创建任何内存,你需要先创建 AffineTransform 对象用来存储结果,并作为第一个参数传入函数。
* @return Out object with inverted result.
* 将矩阵求逆的结果赋值给出口矩阵。
* @param out 出口矩阵。
* @param t 求逆的矩阵。
* @returns `out`
*/
public static invert (out: AffineTransform, t: AffineTransform) {
const { a, b, c, d } = t;
@@ -119,10 +110,10 @@ export default class AffineTransform {
}
/**
* !#en Get an AffineTransform object from a given matrix 4x4.
* This function is memory free, you should create the output affine transform by yourself and manage its memory.
* !#zh 从一个 4x4 Matrix 获取 AffineTransform 对象。这个函数不创建任何内存,你需要先创建 AffineTransform 对象用来存储结果,并作为第一个参数传入函数。
* @return Out object with inverted result.
* 将四维矩阵转换为二维仿射变换矩阵并赋值给出口矩阵。
* @param out 出口矩阵。
* @param mat 四维矩阵。
* @returns `out`
*/
public static fromMat4 (out: AffineTransform, mat: Mat4) {
out.a = mat.m00;
@@ -135,23 +126,21 @@ export default class AffineTransform {
}
/**
* !#en Apply the affine transformation on a point.
* This function is memory free, you should create the output Vec2 by yourself and manage its memory.
* !#zh 对一个点应用矩阵变换。这个函数不创建任何内存,你需要先创建一个 Vec2 对象用来存储结果,并作为第一个参数传入函数。
* @param out The output point to store the result
* @param point Point to apply transform.
* @param t Transform matrix.
* 应用二维仿射变换矩阵到二维向量上,并将结果赋值给出口向量。
* @param out 出口向量。
* @param point 应用变换的向量。
* @param t 二维仿射变换矩阵。
* @returns `out`
*/
public static transformVec2 (out: Vec2, point: Vec2, t: AffineTransform): Vec2;
/**
* !#en Apply the affine transformation on a point.
* This function is memory free, you should create the output Vec2 by yourself and manage its memory.
* !#zh 对一个点应用矩阵变换。这个函数不创建任何内存,你需要先创建一个 Vec2 对象用来存储结果,并作为第一个参数传入函数。
* @param out The output point to store the result
* @param x The x.
* @param y The y.
* @param t Transform matrix.
* 应用二维仿射变换矩阵到二维向量上,并将结果赋值给出口向量。
* @param out 出口向量。
* @param x 应用变换的向量的 x 分量。
* @param y 应用变换的向量的 y 分量。
* @param t 二维仿射变换矩阵。
* @returns `out`
*/
public static transformVec2 (out: Vec2, x: number, y: number, t: AffineTransform): Vec2;
@@ -172,10 +161,11 @@ export default class AffineTransform {
}
/**
* !#en Apply the affine transformation on a size.
* This function is memory free, you should create the output Size by yourself and manage its memory.
* !#zh 应用仿射变换矩阵到 Size 上。这个函数不创建任何内存,你需要先创建一个 Size 对象用来存储结果,并作为第一个参数传入函数。
* @param out The output point to store the result
* 应用二维仿射变换矩阵到二维尺寸上,并将结果赋值给出口尺寸。
* @param out 出口尺寸。
* @param size 应用变换的尺寸。
* @param t 二维仿射变换矩阵。
* @returns `out`
*/
public static transformSize (out: Size, size: Size, t: AffineTransform) {
out.width = t.a * size.width + t.c * size.height;
@@ -184,9 +174,11 @@ export default class AffineTransform {
}
/**
* !#en Apply the affine transformation on a rect.
* This function is memory free, you should create the output Rect by yourself and manage its memory.
* !#zh 应用仿射变换矩阵到 Rect 上。这个函数不创建任何内存,你需要先创建一个 Rect 对象用来存储结果,并作为第一个参数传入函数。
* 应用二维仿射变换矩阵到矩形上,并将结果赋值给出口矩形。
* @param out 出口矩形。
* @param rect 应用变换的矩形。
* @param t 二维仿射变换矩阵。
* @returns `out`
*/
public static transformRect (out: Rect, rect: Rect, t: AffineTransform) {
const ol = rect.x;
@@ -215,9 +207,8 @@ export default class AffineTransform {
}
/**
* !#en Apply the affine transformation on a rect, and truns to an Oriented Bounding Box.
* This function is memory free, you should create the output vectors by yourself and manage their memory.
* !#zh 应用仿射变换矩阵到 Rect 上, 并转换为有向包围盒。这个函数不创建任何内存,你需要先创建包围盒的四个 Vector 对象用来存储结果,并作为前四个参数传入函数。
* 应用二维仿射变换矩阵到矩形上, 并转换为有向包围盒。
* 这个函数不创建任何内存,你需要先创建包围盒的四个 Vector 对象用来存储结果,并作为前四个参数传入函数。
*/
public static transformObb (out_bl: Vec2, out_tl: Vec2, out_tr: Vec2, out_br: Vec2, rect: Rect, anAffineTransform: AffineTransform) {
const x = rect.x;
@@ -249,6 +240,15 @@ export default class AffineTransform {
public tx: number;
public ty: number;
/**
* 构造二维放射变换矩阵。
* @param a a 元素。
* @param b b 元素。
* @param c c 元素。
* @param d d 元素。
* @param tx tx 元素。
* @param ty ty 元素。
*/
constructor (a = 1, b = 0, c = 0, d = 1, tx = 0, ty = 0) {
this.a = a;
this.b = b;
+171 -195
View File
@@ -28,39 +28,27 @@ import CCClass from '../data/class';
import { color4 } from '../vmath';
import { ValueType } from './value-type';
const to_float = 1 / 255;
const toFloat = 1 / 255;
/**
* !#en
* Representation of RGBA colors.
*
* Each color component is a floating point value with a range from 0 to 255.
*
* You can also use the convenience method {{#crossLink "cc/color:method"}}cc.color{{/crossLink}} to create a new Color.
*
* !#zh
* cc.Color 用于表示颜色。
*
* 它包含 RGBA 四个以浮点数保存的颜色分量,每个的值都在 0 到 255 之间。
*
* 您也可以通过使用 {{#crossLink "cc/color:method"}}cc.color{{/crossLink}} 的便捷方法来创建一个新的 Color。
*
* @class Color
* @extends ValueType
* 通过 Red、Green、Blue 颜色通道表示颜色,并通过 Alpha 通道表示不透明度。
* 每个通道都为取值范围 [0, 255] 的整数。
*/
export default class Color extends ValueType {
private _val: number;
/**
* @param other
* 构造与指定颜色相等的颜色。
* @param other 相比较的颜色。
*/
constructor (other: Color);
/**
* @param [r=0] - red component of the color, default value is 0.
* @param [g=0] - green component of the color, defualt value is 0.
* @param [b=0] - blue component of the color, default value is 0.
* @param [a=255] - alpha component of the color, default value is 255.
* 构造具有指定通道的颜色。
* @param [r=0] 指定的 Red 通道。
* @param [g=0] 指定的 Green 通道。
* @param [b=0] 指定的 Blue 通道。
* @param [a=255] 指定的 Alpha 通道。
*/
constructor (r?: number, g?: number, b?: number, a?: number);
@@ -79,151 +67,133 @@ export default class Color extends ValueType {
this._val = ((a << 24) >>> 0) + (b << 16) + (g << 8) + r;
}
// color: [r, g, b, a]
/**
* !#en Solid white, RGBA is [255, 255, 255, 255].
* !#zh 纯白色,RGBA 是 [255, 255, 255, 255]。
* 创建并获取(不透明的)纯白色,各通道值依次为 (255, 255, 255, 255)。
*/
static get WHITE () {
return new Color(255, 255, 255, 255);
}
/**
* !#en Solid black, RGBA is [0, 0, 0, 255].
* !#zh 纯黑色,RGBA 是 [0, 0, 0, 255]。
* 创建并获取(不透明的)纯黑色,各通道值依次为 (0, 0, 0, 255)。
*/
static get BLACK () {
return new Color(0, 0, 0, 255);
}
/**
* !#en Transparent, RGBA is [0, 0, 0, 0].
* !#zh 透明,RGBA 是 [0, 0, 0, 0]。
* 创建并获取全透明的纯黑色,各通道值依次为 (0, 0, 0, 0)。
*/
static get TRANSPARENT () {
return new Color(0, 0, 0, 0);
}
/**
* !#en Grey, RGBA is [127.5, 127.5, 127.5].
* !#zh 灰色,RGBA 是 [127.5, 127.5, 127.5]。
* 创建并获取(不透明的)灰色,各通道值依次为 (127.5, 127.5, 127.5, 255)。
*/
static get GRAY () {
return new Color(127.5, 127.5, 127.5, 255);
}
/**
* !#en Solid red, RGBA is [255, 0, 0].
* !#zh 纯红色,RGBA 是 [255, 0, 0]。
* 创建并获取(不透明的)纯红色,各通道值依次为 (255, 0, 0, 255)。
*/
static get RED () {
return new Color(255, 0, 0, 255);
}
/**
* !#en Solid green, RGBA is [0, 255, 0].
* !#zh 纯绿色,RGBA 是 [0, 255, 0]。
* 创建并获取(不透明的)纯绿色,各通道值依次为 (0, 255, 0, 255)。
*/
static get GREEN () {
return new Color(0, 255, 0, 255);
}
/**
* !#en Solid blue, RGBA is [0, 0, 255].
* !#zh 纯蓝色,RGBA 是 [0, 0, 255]。
* 创建并获取(不透明的)纯蓝色,各通道值依次为 (0, 0, 255, 255)。
*/
static get BLUE () {
return new Color(0, 0, 255, 255);
}
/**
* !#en Yellow, RGBA is [255, 235, 4].
* !#zh 黄色,RGBA 是 [255, 235, 4]。
* 创建并获取(不透明的)黄色,各通道值依次为 (255, 235, 4, 255)。
*/
static get YELLOW () {
return new Color(255, 235, 4, 255);
}
/**
* !#en Orange, RGBA is [255, 127, 0].
* !#zh 橙色,RGBA 是 [255, 127, 0]。
* 创建并获取(不透明的)橙色,各通道值依次为 (255, 127, 0, 255)。
*/
static get ORANGE () {
return new Color(255, 127, 0, 255);
}
/**
* !#en Cyan, RGBA is [0, 255, 255].
* !#zh 青色,RGBA 是 [0, 255, 255]。
* 创建并获取(不透明的)青色,各通道值依次为 (0, 255, 255, 255)。
*/
static get CYAN () {
return new Color(0, 255, 255, 255);
}
/**
* !#en Magenta, RGBA is [255, 0, 255].
* !#zh 洋红色(品红色),RGBA 是 [255, 0, 255]。
* 创建并获取(不透明的)洋红色(品红色),各通道值依次为 (255, 0, 255, 255)。
*/
static get MAGENTA () {
return new Color(255, 0, 255, 255);
}
/**
* !#en red channel value
* !#zh 红色通道值
* 获取或设置当前颜色的 Red 通道。
*/
get r () {
return this._val & 0x000000ff;
}
set r (red) {
red = ~~cc.misc.clampf(red, 0, 255);
this._val = ((this._val & 0xffffff00) | red) >>> 0;
}
/**
* !#en green channel value
* !#zh 绿色通道值
* 获取或设置当前颜色的 Green 通道。
*/
get g () {
return (this._val & 0x0000ff00) >> 8;
}
set g (green) {
green = ~~cc.misc.clampf(green, 0, 255);
this._val = ((this._val & 0xffff00ff) | (green << 8)) >>> 0;
}
/**
* !#en blue channel value
* !#zh 蓝色通道值
* 获取或设置当前颜色的 Blue 通道。
*/
get b () {
return (this._val & 0x00ff0000) >> 16;
}
set b (blue) {
blue = ~~cc.misc.clampf(blue, 0, 255);
this._val = ((this._val & 0xff00ffff) | (blue << 16)) >>> 0;
}
/**
* !#en alpha channel value
* !#zh 透明度通道值
* 获取或设置当前颜色的 Alpha 通道。
*/
get a () {
return (this._val & 0xff000000) >>> 24;
}
set a (alpha) {
alpha = ~~cc.misc.clampf(alpha, 0, 255);
this._val = ((this._val & 0x00ffffff) | ((alpha << 24) >>> 0)) >>> 0;
}
/**
* !#en Clone a new color from the current color.
* !#zh 克隆当前颜色。
*
* @return Newly created color.
* @example
* var color = new cc.Color();
* var newColor = color.clone();// Color {r: 0, g: 0, b: 0, a: 255}
* 克隆当前颜色。
*/
public clone () {
const ret = new Color();
@@ -232,31 +202,20 @@ export default class Color extends ValueType {
}
/**
* !#en TODO
* !#zh 判断两个颜色是否相等。
*
* @param other
* @return
* @example
* var color1 = cc.Color.WHITE;
* var color2 = new cc.Color(255, 255, 255);
* cc.log(color1.equals(color2)); // true;
* color2 = cc.Color.RED;
* cc.log(color2.equals(color1)); // false;
* 判断当前颜色是否与指定颜色相等。
* @param other 相比较的颜色。
* @returns 两颜色的各通道都相等时返回 `true`;否则返回 `false`。
*/
public equals (other: Color) {
return other && this._val === other._val;
}
/**
* !#en TODO
* !#zh 线性插值
*
* @param to
* @param ratio - the interpolation coefficient.
* @param [out] - optional, the receiving vector.
* @return
* @example {@link utils/api/engine/docs/cocos2d/core/value-types/CCColor/lerp.js}
* 根据指定的插值比率,从当前颜色到目标颜色之间做插值。
* @param to 目标颜色。
* @param ratio 插值比率,范围为 [0,1]。
* @param out 当此参数定义时,本方法将插值结果赋值给此参数并返回此参数。
* @returns 当前颜色各个通道到目标颜色对应的各个通道之间按指定插值比率进行线性插值构成的颜色。
*/
public lerp (to: Color, ratio: number, out?: Color) {
out = out || new Color();
@@ -272,13 +231,8 @@ export default class Color extends ValueType {
}
/**
* !#en TODO
* !#zh 转换为方便阅读的字符串。
*
* @return
* @example
* var color = cc.Color.WHITE;
* color.toString(); // "rgba(255, 255, 255, 255)"
* 返回当前颜色的字符串表示。
* @returns 当前颜色的字符串表示。
*/
public toString () {
return 'rgba(' +
@@ -289,20 +243,17 @@ export default class Color extends ValueType {
}
/**
* !#en Convert color to css format.
* !#zh 转换为 CSS 格式。
*
* @param opt - "rgba", "rgb", "#rgb" or "#rrggbb".
* @return
* @example {@link utils/api/engine/docs/cocos2d/core/value-types/CCColor/toCSS.js}
* 将当前颜色转换为 CSS 格式。
* @param opt 格式选项。
* @returns 当前颜色的 CSS 格式。
*/
public toCSS (opt: 'rgba' | 'rgb' | '#rgb' | '#rrggbb') {
public toCSS (opt: 'rgba' | 'rgb' | '#rrggbb') {
if ( opt === 'rgba' ) {
return 'rgba(' +
(this.r | 0 ) + ',' +
(this.g | 0 ) + ',' +
(this.b | 0 ) + ',' +
(this.a * to_float).toFixed(2) + ')'
(this.a * toFloat).toFixed(2) + ')'
;
} else if ( opt === 'rgb' ) {
return 'rgb(' +
@@ -316,16 +267,12 @@ export default class Color extends ValueType {
}
/**
* !#en Read hex string and store color data into the current color object,
* the hex string must be formated as rgba or rgb.
* !#zh 读取 16 进制颜色。
*
* @param hexString
* @return
* @chainable
* @example
* var color = cc.Color.BLACK;
* color.fromHEX("#FFFF33"); // Color {r: 255, g: 255, b: 51, a: 255};
* 从十六进制颜色字符串中读入当前颜色。
* 十六进制颜色字符串应该以可选的 "#" 开头,紧跟最多 8 个代表十六进制数字的字符;
* 每两个连续字符代表的数值依次作为 Red、Green、Blue 和 Alpha 通道;
* 缺省的颜色通道将视为 0;缺省的透明通道将视为 255。
* @param hexString 十六进制颜色字符串。
* @returns `this`
*/
public fromHEX (hexString: string) {
hexString = (hexString.indexOf('#') === 0) ? hexString.substring(1) : hexString;
@@ -338,31 +285,24 @@ export default class Color extends ValueType {
}
/**
* !#en convert Color to HEX color string.
* e.g. cc.color(255,6,255) to : "#ff06ff"
* !#zh 转换为 16 进制。
*
* @param fmt - "#rgb", "#rrggbb" or "#rrggbbaa".
* @return
* 转换当前颜色为十六进制颜色字符串。
* @param fmt 格式选项。
* - `'#rrggbbaa'` 获取Red、Green、Blue、Alpha通道的十六进制值(**两位**,高位补 0)并依次连接;
* - `'#rrggbb` 与 `'#rrggbbaa'` 类似但不包括 Alpha 通道。
* @returns 十六进制颜色字符串。
* @example
* var color = cc.Color.BLACK;
* color.toHEX("#rgb"); // "000";
* color.toHEX("#rrggbb"); // "000000";
* const color = new Color(255, 14, 0, 255);
* color.toHex("rrggbbaa"); // "FF0E00FF"
* color.toHex("rrggbb"); // "FF0E00"
*/
public toHEX (fmt: '#rgb' | '#rrggbb' | '#rrggbbaa') {
public toHEX (fmt: '#rrggbb' | '#rrggbbaa') {
const hex = [
(this.r | 0 ).toString(16),
(this.g | 0 ).toString(16),
(this.b | 0 ).toString(16),
];
let i = -1;
if ( fmt === '#rgb' ) {
for ( i = 0; i < hex.length; ++i ) {
if ( hex[i].length > 1 ) {
hex[i] = hex[i][0];
}
}
} else if ( fmt === '#rrggbb' ) {
if ( fmt === '#rrggbb' ) {
for ( i = 0; i < hex.length; ++i ) {
if ( hex[i].length === 1 ) {
hex[i] = '0' + hex[i];
@@ -380,29 +320,24 @@ export default class Color extends ValueType {
}
/**
* !#en Convert to 24bit rgb value.
* !#zh 转换为 24bit 的 RGB 值。
*
* @return
* 将当前颜色转换为 RGB 整数值。
* @returns RGB 整数值。从最低有效位开始,每8位分别是 Red、Green、Blue 通道的值。
* @example
* var color = cc.Color.YELLOW;
* color.toRGBValue(); // 16771844;
* const color = Color.YELLOW;
* color.toRGBValue();
*/
public toRGBValue () {
return this._val & 0x00ffffff;
}
/**
* !#en Read HSV model color and convert to RGB color
* !#zh 读取 HSV(色彩模型)格式。
*
* @param h
* @param s
* @param v
* @return
* @chainable
* 从 HSV 颜色中读入当前颜色。
* @param h H 通道。
* @param s S 通道。
* @param v V 通道。
* @returns `this`
* @example
* var color = cc.Color.YELLOW;
* const color = Color.YELLOW;
* color.fromHSV(0, 0, 1); // Color {r: 255, g: 255, b: 255, a: 255};
*/
public fromHSV (h: number, s: number, v: number) {
@@ -471,18 +406,16 @@ export default class Color extends ValueType {
}
/**
* !#en Transform to HSV model color
* !#zh 转换为 HSV(色彩模型)格式。
*
* @return - {h: number, s: number, v: number}.
* 转换当前颜色为 HSV 颜色。
* @returns HSV 颜色。成员 `h`、`s`、`v` 分别代表 HSV 颜色的 H、S、V 通道。
* @example
* var color = cc.Color.YELLOW;
* color.toHSV(); // Object {h: 0.1533864541832669, s: 0.9843137254901961, v: 1};
* const color = cc.Color.YELLOW;
* color.toHSV(); // {h: 0.1533864541832669, s: 0.9843137254901961, v: 1}
*/
public toHSV () {
const r = this.r * to_float;
const g = this.g * to_float;
const b = this.b * to_float;
const r = this.r * toFloat;
const g = this.g * toFloat;
const b = this.b * toFloat;
const hsv = { h: 0, s: 0, v: 0 };
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
@@ -504,77 +437,120 @@ export default class Color extends ValueType {
return hsv;
}
public set (c: Color) {
if (c._val) {
this._val = c._val;
/**
* 设置当前颜色使其与指定颜色相等。
* @param other 相比较的颜色。
* @returns 当前颜色。
*/
public set (other: Color) {
if (other._val) {
this._val = other._val;
} else {
this._val = ((c.a << 24) >>> 0) + (c.b << 16) + (c.g << 8) + c.r;
this._val = ((other.a << 24) >>> 0) + (other.b << 16) + (other.g << 8) + other.r;
}
}
public mulSelf (c: Color) {
const r = ((this._val & 0x000000ff) * c.r) >> 8;
const g = ((this._val & 0x0000ff00) * c.g) >> 8;
const b = ((this._val & 0x00ff0000) * c.b) >> 8;
const a = ((this._val & 0xff000000) >>> 8) * c.a;
/**
* 将当前颜色乘以与指定颜色:当前颜色的每个通道都乘以指定颜色对应的通道。
* @param other 指定的颜色。
* @returns `this`
*/
public mulSelf (other: Color) {
const r = ((this._val & 0x000000ff) * other.r) >> 8;
const g = ((this._val & 0x0000ff00) * other.g) >> 8;
const b = ((this._val & 0x00ff0000) * other.b) >> 8;
const a = ((this._val & 0xff000000) >>> 8) * other.a;
this._val = (a & 0xff000000) | (b & 0x00ff0000) | (g & 0x0000ff00) | (r & 0x000000ff);
return this;
}
public mul (c: Color, out?: Color) {
/**
* 将当前颜色乘以与指定颜色的结果赋值给出口颜色。
* @param other 指定的颜色。
* @param [out] 出口颜色,当未指定时将创建为新的颜色。
*/
public mul (other: Color, out?: Color) {
out = out || new Color();
const r = ((this._val & 0x000000ff) * c.r) >> 8;
const g = ((this._val & 0x0000ff00) * c.g) >> 8;
const b = ((this._val & 0x00ff0000) * c.b) >> 8;
const a = ((this._val & 0xff000000) >>> 8) * c.a;
const r = ((this._val & 0x000000ff) * other.r) >> 8;
const g = ((this._val & 0x0000ff00) * other.g) >> 8;
const b = ((this._val & 0x00ff0000) * other.b) >> 8;
const a = ((this._val & 0xff000000) >>> 8) * other.a;
out._val = (a & 0xff000000) | (b & 0x00ff0000) | (g & 0x0000ff00) | (r & 0x000000ff);
return out;
}
get x () { return this.r * to_float; }
set x (val) { this.r = val * 255; }
get y () { return this.g * to_float; }
set y (val) { this.g = val * 255; }
get z () { return this.b * to_float; }
set z (val) { this.b = val * 255; }
get w () { return this.a * to_float; }
set w (val) { this.a = val * 255; }
/**
* 通过除以 255,将当前颜色的各个通道都视为范围 [0, 1] 内,设置 Red 通道的值。
*/
get x () {
return this.r * toFloat;
}
set x (value) {
this.r = value * 255;
}
/**
* 通过除以 255,将当前颜色的各个通道都视为范围 [0, 1] 内,设置 Green 通道的值。
*/
get y () {
return this.g * toFloat;
}
set y (value) {
this.g = value * 255;
}
/**
* 通过除以 255,将当前颜色的各个通道都视为范围 [0, 1] 内,设置 Blue 通道的值。
*/
get z () {
return this.b * toFloat;
}
set z (value) {
this.b = value * 255;
}
/**
* 通过除以 255,将当前颜色的各个通道都视为范围 [0, 1] 内,设置 Alpha 通道的值。
*/
get w () {
return this.a * toFloat;
}
set w (value) {
this.a = value * 255;
}
}
CCClass.fastDefine('cc.Color', Color, {r: 0, g: 0, b: 0, a: 255});
/**
* !#en
* The convenience method to create a new {{#crossLink "Color/Color:method"}}cc.Color{{/crossLink}}
* Alpha channel is optional. Default value is 255.
*
* !#zh
* 通过该方法来创建一个新的 {{#crossLink "Color/Color:method"}}cc.Color{{/crossLink}} 对象。
* Alpha 通道是可选的。默认值是 255。
*
*
* @param other
* @return
* @example {@link utils/api/engine/docs/cocos2d/core/value-types/CCColor/color.js}
* 构造与指定颜色相等的颜色。相当于 `new Color(other)`。
* @param other 相比较的颜色。
* @returns `new Color(other)`
*/
export function color (other: Color | string): Color;
export function color (other: Color): Color;
// tslint:disable:unified-signatures
/**
* !#en
* The convenience method to create a new {{#crossLink "Color/Color:method"}}cc.Color{{/crossLink}}
* Alpha channel is optional. Default value is 255.
*
* !#zh
* 通过该方法来创建一个新的 {{#crossLink "Color/Color:method"}}cc.Color{{/crossLink}} 对象。
* Alpha 通道是可选的。默认值是 255。
*
*
* @param [r=0]
* @param [g=0]
* @param [b=0]
* @param [a=255]
* @return
* @example {@link utils/api/engine/docs/cocos2d/core/value-types/CCColor/color.js}
* 从十六进制字符串中构造颜色。相当于 `(new Color()).fromHex(hexString)`。
* @param other 相比较的颜色。
* @returns `(new Color()).fromHex(hexString)`
*/
export function color (hexString: string): Color;
// tslint:enable:unified-signatures
/**
* 构造具有指定通道的颜色。相当于 `new Color(r, g, b, a)`。
* @param [r=0] 指定的 Red 通道。
* @param [g=0] 指定的 Green 通道。
* @param [b=0] 指定的 Blue 通道。
* @param [a=255] 指定的 Alpha 通道。
* @returns `new Color(r, g, b, a)`
*/
export function color (r?: number, g?: number, b?: number, a?: number): Color;
+129 -129
View File
@@ -1,129 +1,129 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
import * as js from '../utils/js';
/**
* @en
* Define an enum type. <br/>
* If a enum item has a value of -1, it will be given an Integer number according to it's order in the list.<br/>
* Otherwise it will use the value specified by user who writes the enum definition.
*
* @zh
* 定义一个枚举类型。<br/>
* 用户可以把枚举值设为任意的整数,如果设为 -1,系统将会分配为上一个枚举值 + 1。
*
* @param obj - a JavaScript literal object containing enum names and values, or a TypeScript enum type
* @return the defined enum type
* @example {@link cocos2d/core/platform/CCEnum/Enum.js}
* @typescript Enum<T>(obj: T): T
*/
export default function Enum<T> (obj: T): T {
if ('__enums__' in obj) {
return obj;
}
js.value(obj, '__enums__', null, true);
let lastIndex: number = -1;
const keys: string[] = Object.keys(obj);
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
let val = obj[key];
if (val === -1) {
val = ++lastIndex;
obj[key] = val;
}
else {
if (typeof val === 'number') {
lastIndex = val;
}
else if (typeof val === 'string' && Number.isInteger(parseFloat(key))) {
continue;
}
}
const reverseKey: string = '' + val;
if (key !== reverseKey) {
if ((CC_EDITOR || CC_TEST) && reverseKey in obj && obj[reverseKey] !== key) {
cc.errorID(7100, reverseKey);
continue;
}
js.value(obj, reverseKey, key);
}
}
return obj;
}
Enum.isEnum = (enumType) => {
return enumType && enumType.hasOwnProperty('__enums__');
};
/**
* @param enumDef - the enum type defined from cc.Enum
* @return {Object[]}
*/
Enum.getList = (enumDef) => {
if (enumDef.__enums__) {
return enumDef.__enums__;
}
const enums: any[] = enumDef.__enums__ = [];
// tslint:disable-next-line: forin
for (const name in enumDef) {
const value = enumDef[name];
if (Number.isInteger(value)) {
enums.push({ name, value });
}
}
enums.sort((a, b) => a.value - b.value);
return enums;
};
if (CC_DEV) {
// check key order in object literal
const _TestEnum: Enum = Enum({
ZERO: -1,
ONE: -1,
TWO: -1,
THREE: -1,
});
if (_TestEnum.ZERO !== 0 || _TestEnum.ONE !== 1 || _TestEnum.THREE !== 3) {
cc.errorID(7101);
}
}
/**
* @param enumx
*/
export function ccenum (enumx) {
if ('__enums__' in enumx) {
return;
}
js.value(enumx, '__enums__', null, true);
}
cc.Enum = Enum;
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
import * as js from '../utils/js';
/**
* @en
* Define an enum type. <br/>
* If a enum item has a value of -1, it will be given an Integer number according to it's order in the list.<br/>
* Otherwise it will use the value specified by user who writes the enum definition.
*
* @zh
* 定义一个枚举类型。<br/>
* 用户可以把枚举值设为任意的整数,如果设为 -1,系统将会分配为上一个枚举值 + 1。
*
* @param obj - a JavaScript literal object containing enum names and values, or a TypeScript enum type
* @return the defined enum type
* @example {@link cocos2d/core/platform/CCEnum/Enum.js}
* @typescript Enum<T>(obj: T): T
*/
export default function Enum<T> (obj: T): T {
if ('__enums__' in obj) {
return obj;
}
js.value(obj, '__enums__', null, true);
let lastIndex: number = -1;
const keys: string[] = Object.keys(obj);
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
let val = obj[key];
if (val === -1) {
val = ++lastIndex;
obj[key] = val;
}
else {
if (typeof val === 'number') {
lastIndex = val;
}
else if (typeof val === 'string' && Number.isInteger(parseFloat(key))) {
continue;
}
}
const reverseKey: string = '' + val;
if (key !== reverseKey) {
if ((CC_EDITOR || CC_TEST) && reverseKey in obj && obj[reverseKey] !== key) {
cc.errorID(7100, reverseKey);
continue;
}
js.value(obj, reverseKey, key);
}
}
return obj;
}
Enum.isEnum = (enumType) => {
return enumType && enumType.hasOwnProperty('__enums__');
};
/**
* @param enumDef - the enum type defined from cc.Enum
* @return {Object[]}
*/
Enum.getList = (enumDef) => {
if (enumDef.__enums__) {
return enumDef.__enums__;
}
const enums: any[] = enumDef.__enums__ = [];
// tslint:disable-next-line: forin
for (const name in enumDef) {
const value = enumDef[name];
if (Number.isInteger(value)) {
enums.push({ name, value });
}
}
enums.sort((a, b) => a.value - b.value);
return enums;
};
if (CC_DEV) {
// check key order in object literal
const _TestEnum: Enum = Enum({
ZERO: -1,
ONE: -1,
TWO: -1,
THREE: -1,
});
if (_TestEnum.ZERO !== 0 || _TestEnum.ONE !== 1 || _TestEnum.THREE !== 3) {
cc.errorID(7101);
}
}
/**
* @param enumx
*/
export function ccenum (enumx) {
if ('__enums__' in enumx) {
return;
}
js.value(enumx, '__enums__', null, true);
}
cc.Enum = Enum;
+176 -163
View File
@@ -30,61 +30,113 @@ import { ValueType } from './value-type';
import Vec3 from './vec3';
/**
* !#en Representation of 4*4 matrix.
* !#zh 表示 4*4 矩阵
* 表示四维(4x4)矩阵。
*/
export default class Mat4 extends ValueType {
/**
* 矩阵第 0 列第 0 行的元素。
*/
public m00: number;
/**
* 矩阵第 0 列第 1 行的元素。
*/
public m01: number;
/**
* 矩阵第 0 列第 2 行的元素。
*/
public m02: number;
/**
* 矩阵第 0 列第 3 行的元素。
*/
public m03: number;
/**
* 矩阵第 1 列第 0 行的元素。
*/
public m04: number;
/**
* 矩阵第 1 列第 1 行的元素。
*/
public m05: number;
/**
* 矩阵第 1 列第 2 行的元素。
*/
public m06: number;
/**
* 矩阵第 1 列第 3 行的元素。
*/
public m07: number;
/**
* 矩阵第 2 列第 0 行的元素。
*/
public m08: number;
/**
* 矩阵第 2 列第 1 行的元素。
*/
public m09: number;
/**
* 矩阵第 2 列第 2 行的元素。
*/
public m10: number;
/**
* 矩阵第 2 列第 3 行的元素。
*/
public m11: number;
/**
* 矩阵第 3 列第 0 行的元素。
*/
public m12: number;
/**
* 矩阵第 3 列第 1 行的元素。
*/
public m13: number;
/**
* 矩阵第 3 列第 2 行的元素。
*/
public m14: number;
/**
* 矩阵第 3 列第 3 行的元素。
*/
public m15: number;
/**
* !#en
* Constructor
* see {{#crossLink "cc/mat4:method"}}cc.mat4{{/crossLink}}
* !#zh
* 构造函数,可查看 {{#crossLink "cc/mat4:method"}}cc.mat4{{/crossLink}}
*
* @param other
* 构造与指定矩阵相等的矩阵。
* @param other 相比较的矩阵。
*/
constructor (other: Mat4);
/**
* !#en
* Constructor
* see {{#crossLink "cc/mat4:method"}}cc.mat4{{/crossLink}}
* !#zh
* 构造函数,可查看 {{#crossLink "cc/mat4:method"}}cc.mat4{{/crossLink}}
*
* @param m00 Component in column 0, row 0 position (index 0)
* @param m01 Component in column 0, row 1 position (index 1)
* @param m02 Component in column 0, row 2 position (index 2)
* @param m03 Component in column 0, row 3 position (index 3)
* @param m10 Component in column 1, row 0 position (index 4)
* @param m11 Component in column 1, row 1 position (index 5)
* @param m12 Component in column 1, row 2 position (index 6)
* @param m13 Component in column 1, row 3 position (index 7)
* @param m20 Component in column 2, row 0 position (index 8)
* @param m21 Component in column 2, row 1 position (index 9)
* @param m22 Component in column 2, row 2 position (index 10)
* @param m23 Component in column 2, row 3 position (index 11)
* @param m30 Component in column 3, row 0 position (index 12)
* @param m31 Component in column 3, row 1 position (index 13)
* @param m32 Component in column 3, row 2 position (index 14)
* @param m33 Component in column 3, row 3 position (index 15)
* 构造具有指定元素的矩阵。
* @param m00 矩阵第 0 列第 0 行的元素。
* @param m01 矩阵第 0 列第 1 行的元素。
* @param m02 矩阵第 0 列第 2 行的元素。
* @param m03 矩阵第 0 列第 3 行的元素。
* @param m04 矩阵第 1 列第 0 行的元素。
* @param m05 矩阵第 1 列第 1 行的元素。
* @param m06 矩阵第 1 列第 2 行的元素。
* @param m07 矩阵第 1 列第 3 行的元素。
* @param m08 矩阵第 2 列第 0 行的元素。
* @param m09 矩阵第 2 列第 1 行的元素。
* @param m10 矩阵第 2 列第 2 行的元素。
* @param m11 矩阵第 2 列第 3 行的元素。
* @param m12 矩阵第 3 列第 0 行的元素。
* @param m13 矩阵第 3 列第 1 行的元素。
* @param m14 矩阵第 3 列第 2 行的元素。
* @param m15 矩阵第 3 列第 3 行的元素。
*/
constructor (
m00?: number, m01?: number, m02?: number, m03?: number,
@@ -112,10 +164,7 @@ export default class Mat4 extends ValueType {
}
/**
* !#en clone a Mat4 object
* !#zh 克隆一个 Mat4 对象
*
* @return
* 克隆当前矩阵。
*/
public clone () {
const t = this;
@@ -127,12 +176,9 @@ export default class Mat4 extends ValueType {
}
/**
* !#en Sets the matrix with another one's value
* !#zh 用另一个矩阵设置这个矩阵的值。
*
* @param srcObj
* @return returns this
* @chainable
* 设置当前矩阵使其与指定矩阵相等。
* @param other 相比较的矩阵。
* @returns `this`
*/
public set (other: Mat4) {
const t = this;
@@ -156,34 +202,26 @@ export default class Mat4 extends ValueType {
}
/**
* !#en Check whether two matrix equal
* !#zh 当前的矩阵是否与指定的矩阵相等。
*
* @param other
* @return
* 判断当前矩阵是否与指定矩阵相等。
* @param other 相比较的矩阵。
* @returns 两矩阵的各元素都分别相等时返回 `true`;否则返回 `false`。
*/
public equals (other: Mat4) {
return xmat4.exactEquals(this, other);
}
/**
* !#en Check whether two matrix equal with default degree of variance.
* !#zh
* 近似判断两个矩阵是否相等。<br/>
* 判断 2 个矩阵是否在默认误差范围之内,如果在则返回 true,反之则返回 false。
*
* @param other
* @return
* 判断当前矩阵是否与指定矩阵相等。
* @param other 相比较的矩阵。
* @returns 两矩阵的各元素都分别相等时返回 `true`;否则返回 `false`。
*/
public fuzzyEquals (other: Mat4) {
return xmat4.equals(this, other);
}
/**
* !#en Transform to string with matrix informations
* !#zh 转换为方便阅读的字符串。
*
* @return
* 返回当前矩阵的字符串表示。
* @returns 当前矩阵的字符串表示。
*/
public toString () {
const t = this;
@@ -197,20 +235,17 @@ export default class Mat4 extends ValueType {
}
/**
* Set the matrix to the identity matrix
*
* @return self
* @chainable
* 将当前矩阵设为单位矩阵。
* @returns `this`
*/
public identity () {
return xmat4.identity(this);
}
/**
* Transpose the values of a mat4
*
* @param [out] The receiving matrix, can be `this`; if absent, a new matrix would be created.
* @return out
* 将当前矩阵的转置矩阵赋值给出口矩阵。
* @param [out] 出口矩阵,当未指定时将创建为新的矩阵。
* @returns `out`
*/
public transpose (out?: Mat4) {
out = out || new cc.Mat4();
@@ -218,10 +253,9 @@ export default class Mat4 extends ValueType {
}
/**
* Inverts a mat4
*
* @param [out] The receiving matrix, can be `this`; if absent, a new matrix would be created.
* @return out
* 将当前矩阵的逆矩阵赋值给出口矩阵。
* @param [out] 出口矩阵,当未指定时将创建为新的矩阵。
* @returns `out`
*/
public invert (out?: Mat4) {
out = out || new cc.Mat4();
@@ -229,10 +263,9 @@ export default class Mat4 extends ValueType {
}
/**
* Calculates the adjugate of a mat4
*
* @param [out] The receiving matrix, can be `this`; if absent, a new matrix would be created.
* @return out
* 将当前矩阵的伴随矩阵赋值给出口矩阵。
* @param [out] 出口矩阵,当未指定时将创建为新的矩阵。
* @returns `out`
*/
public adjoint (out?: Mat4) {
out = out || new cc.Mat4();
@@ -240,20 +273,18 @@ export default class Mat4 extends ValueType {
}
/**
* Calculates the determinant of a mat4
*
* @return determinant of a
* 计算当前矩阵的行列式。
* @returns 当前矩阵的行列式。
*/
public determinant () {
return xmat4.determinant(this);
}
/**
* Adds two Mat4
*
* @param other the second operand
* @param [out] The receiving matrix, can be `this`; if absent, a new matrix would be created.
* @return out
* 矩阵加法。将当前矩阵与指定矩阵的相加结果赋值给出口矩阵。
* @param other 指定的矩阵。
* @param [out] 出口矩阵,当未指定时将创建为新的矩阵。
* @returns `out`
*/
public add (other: Mat4, out?: Mat4) {
out = out || new cc.Mat4();
@@ -261,11 +292,10 @@ export default class Mat4 extends ValueType {
}
/**
* Subtracts the current matrix with another one
*
* @param other the second operand
* @param [out] The receiving matrix, can be `this`; if absent, a new matrix would be created.
* @return out
* 矩阵减法。将当前矩阵减去指定矩阵的结果赋值给出口矩阵。
* @param other 减数矩阵。
* @param [out] 出口矩阵,当未指定时将创建为新的矩阵。
* @returns `out`
*/
public sub (other: Mat4, out?: Mat4) {
out = out || new cc.Mat4();
@@ -273,11 +303,10 @@ export default class Mat4 extends ValueType {
}
/**
* Subtracts the current matrix with another one
*
* @param other the second operand
* @param [out] The receiving matrix, can be `this`; if absent, a new matrix would be created.
* @return out
* 矩阵乘法。将当前矩阵左乘指定矩阵的结果赋值给出口矩阵。
* @param other 指定的矩阵。
* @param [out] 出口矩阵,当未指定时将创建为新的矩阵。
* @returns `out`
*/
public mul (other: Mat4, out?: Mat4) {
out = out || new cc.Mat4();
@@ -285,23 +314,21 @@ export default class Mat4 extends ValueType {
}
/**
* Multiply each element of the matrix by a scalar.
*
* @param number amount to scale the matrix's elements by
* @param [out] The receiving matrix, can be `this`; if absent, a new matrix would be created.
* @return out
* 矩阵数乘。将当前矩阵与指定标量的数乘结果赋值给出口矩阵。
* @param scalar 指定的标量。
* @param [out] 出口矩阵,当未指定时将创建为新的矩阵。
* @returns `out`
*/
public mulScalar (num: number, out?: Mat4) {
public mulScalar (scalar: number, out?: Mat4) {
out = out || new cc.Mat4();
return xmat4.scale(out, this, num);
return xmat4.scale(out, this, scalar);
}
/**
* Translate a mat4 by the given vector
*
* @param v vector to translate by
* @param [out] The receiving matrix, can be `this`; if absent, a new matrix would be created.
* @return out
* 将当前矩阵左乘位移矩阵的结果赋值给出口矩阵,位移矩阵由各个轴的位移给出。
* @param v 各个轴的位移。
* @param [out] 出口矩阵,当未指定时将创建为新的矩阵。
* @returns `out`
*/
public translate (v: Vec3, out?: Mat4) {
out = out || new cc.Mat4();
@@ -309,11 +336,10 @@ export default class Mat4 extends ValueType {
}
/**
* Scales the mat4 by the dimensions in the given vec3
*
* @param v vector to scale by
* @param [out] The receiving matrix, can be `this`; if absent, a new matrix would be created.
* @return out
* 将当前矩阵左乘缩放矩阵的结果赋值给出口矩阵,缩放矩阵由各个轴的缩放给出。
* @param v 各个轴的缩放。
* @param [out] 出口矩阵,当未指定时将创建为新的矩阵。
* @returns `out`
*/
public scale (v: Vec3, out?: Mat4) {
out = out || new cc.Mat4();
@@ -321,12 +347,11 @@ export default class Mat4 extends ValueType {
}
/**
* Rotates a mat4 by the given angle around the given axis
*
* @param rad the angle to rotate the matrix by
* @param axis the axis to rotate around
* @param [out] The receiving matrix, can be `this`; if absent, a new matrix would be created.
* @return out
* 将当前矩阵左乘旋转矩阵的结果赋值给出口矩阵,旋转矩阵由旋转轴和旋转角度给出。
* @param 旋转角度(弧度制)。
* @param 旋转轴。
* @param [out] 出口矩阵,当未指定时将创建为新的矩阵。
* @returns `out`
*/
public rotate (rad: number, axis: Vec3, out?: Mat4) {
out = out || new cc.Mat4();
@@ -334,10 +359,8 @@ export default class Mat4 extends ValueType {
}
/**
* Returns the translation vector component of a transformation matrix.
*
* @param {Vec3} out Vector to receive translation component, if not provided, a new vec3 will be created
* @return out
* 从当前矩阵中计算出位移变换的部分,并以各个轴上位移的形式赋值给出口向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
*/
public getTranslation (out?: Mat4) {
out = out || new cc.Vec3();
@@ -345,10 +368,8 @@ export default class Mat4 extends ValueType {
}
/**
* Returns the scale factor component of a transformation matrix
*
* @param out Vector to receive scale component, if not provided, a new vec3 will be created
* @return out
* 从当前矩阵中计算出缩放变换的部分,并以各个轴上缩放的形式赋值给出口向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
*/
public getScale (out: Vec3) {
out = out || new cc.Vec3();
@@ -356,10 +377,8 @@ export default class Mat4 extends ValueType {
}
/**
* Returns the rotation factor component of a transformation matrix
*
* @param out Vector to receive rotation component, if not provided, a new quaternion object will be created
* @return out
* 从当前矩阵中计算出旋转变换的部分,并以四元数的形式赋值给出口四元数。
* @param [out] 出口四元数,当未指定时将创建为新的四元数。
*/
public getRotation (out: Quat) {
out = out || new Quat();
@@ -367,24 +386,22 @@ export default class Mat4 extends ValueType {
}
/**
* Restore the matrix values from a quaternion rotation, vector translation and vector scale
*
* @param q Rotation quaternion
* @param v Translation vector
* @param s Scaling vector
* @return the current mat4 object
* @chainable
* 重置当前矩阵的值,使其表示指定的旋转、缩放、位移依次组合的变换。
* @param q 四元数表示的旋转变换。
* @param v 位移变换,表示为各个轴的位移。
* @param s 缩放变换,表示为各个轴的缩放。
* @returns `this`
*/
public fromRTS (q: Quat, v: Vec3, s: Vec3) {
return xmat4.fromRTS(this, q, v, s);
}
/**
* Restore the matrix values from a quaternion rotation
*
* @param q Rotation quaternion
* @return the current mat4 object
* @chainable
* 重置当前矩阵的值,使其表示指定四元数表示的旋转变换。
* @param q 四元数表示的旋转变换。
* @param v 位移变换,表示为各个轴的位移。
* @param s 缩放变换,表示为各个轴的缩放。
* @returns `this`
*/
public fromQuat (quat: Quat) {
return xmat4.fromQuat(this, quat);
@@ -400,35 +417,31 @@ CCClass.fastDefine('cc.Mat4', Mat4, {
cc.Mat4 = Mat4;
/**
* !#en The convenience method to create a new {{#crossLink "Mat4"}}cc.Mat4{{/crossLink}}.
* !#zh 通过该简便的函数进行创建 {{#crossLink "Mat4"}}cc.Mat4{{/crossLink}} 对象。
*
* @param other
* @return
* 构造与指定矩阵相等的矩阵。等价于 `new Mat4(other)`。
* @param other 相比较的矩阵。
* @returns `new Mat4(other)`
*/
export function mat4 (other: Mat4): Mat4;
/**
* !#en The convenience method to create a new {{#crossLink "Mat4"}}cc.Mat4{{/crossLink}}.
* !#zh 通过该简便的函数进行创建 {{#crossLink "Mat4"}}cc.Mat4{{/crossLink}} 对象。
*
* @param m00 Component in column 0, row 0 position (index 0)
* @param m01 Component in column 0, row 1 position (index 1)
* @param m02 Component in column 0, row 2 position (index 2)
* @param m03 Component in column 0, row 3 position (index 3)
* @param m10 Component in column 1, row 0 position (index 4)
* @param m11 Component in column 1, row 1 position (index 5)
* @param m12 Component in column 1, row 2 position (index 6)
* @param m13 Component in column 1, row 3 position (index 7)
* @param m20 Component in column 2, row 0 position (index 8)
* @param m21 Component in column 2, row 1 position (index 9)
* @param m22 Component in column 2, row 2 position (index 10)
* @param m23 Component in column 2, row 3 position (index 11)
* @param m30 Component in column 3, row 0 position (index 12)
* @param m31 Component in column 3, row 1 position (index 13)
* @param m32 Component in column 3, row 2 position (index 14)
* @param m33 Component in column 3, row 3 position (index 15)
* @return
* 构造具有指定元素的矩阵。等价于 `new Mat4(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33)`
* @param m00 矩阵第 0 列第 0 行的元素。
* @param m01 矩阵第 0 列第 1 行的元素。
* @param m02 矩阵第 0 列第 2 行的元素。
* @param m03 矩阵第 0 列第 3 行的元素。
* @param m04 矩阵第 1 列第 0 行的元素。
* @param m05 矩阵第 1 列第 1 行的元素。
* @param m06 矩阵第 1 列第 2 行的元素。
* @param m07 矩阵第 1 列第 3 行的元素。
* @param m08 矩阵第 2 列第 0 行的元素。
* @param m09 矩阵第 2 列第 1 行的元素。
* @param m10 矩阵第 2 列第 2 行的元素。
* @param m11 矩阵第 2 列第 3 行的元素。
* @param m12 矩阵第 3 列第 0 行的元素。
* @param m13 矩阵第 3 列第 1 行的元素。
* @param m14 矩阵第 3 列第 2 行的元素。
* @param m15 矩阵第 3 列第 3 行的元素。
* @returns `new Mat4(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33)`
*/
export function mat4 (
m00?: number, m01?: number, m02?: number, m03?: number,
+53 -52
View File
@@ -29,40 +29,41 @@ import { ValueType } from './value-type';
import Vec3 from './vec3';
/**
* !#en Representation of 2D vectors and points.
* !#zh 表示 2D 向量和坐标
* 四元数。
*/
export default class Quat extends ValueType {
/**
* x 分量。
*/
public x: number;
/**
* y 分量。
*/
public y: number;
/**
* z 分量。
*/
public z: number;
/**
* w 分量。
*/
public w: number;
/**
* !#en
* Constructor
* see {{#crossLink "cc/quat:method"}}cc.quat{{/crossLink}}
* !#zh
* 构造函数,可查看 {{#crossLink "cc/quat:method"}}cc.quat{{/crossLink}}
*
* @param other
* 构造与指定四元数相等的四元数。
* @param other 相比较的四元数。
*/
constructor (other: Quat);
/**
* !#en
* Constructor
* see {{#crossLink "cc/quat:method"}}cc.quat{{/crossLink}}
* !#zh
* 构造函数,可查看 {{#crossLink "cc/quat:method"}}cc.quat{{/crossLink}}
*
* @param [x=0]
* @param [y=0]
* @param [z=0]
* @param [w=1]
* 构造具有指定分量的四元数。
* @param [x=0] 指定的 x 分量。
* @param [y=0] 指定的 y 分量。
* @param [z=0] 指定的 z 分量。
* @param [w=1] 指定的 w 分量。
*/
constructor (x?: number, y?: number, z?: number, w?: number);
@@ -82,47 +83,51 @@ export default class Quat extends ValueType {
}
/**
* !#en clone a Quat object and return the new object
* !#zh 克隆一个四元数并返回
*
* @return
* 克隆当前四元数。
*/
public clone () {
return new Quat(this.x, this.y, this.z, this.w);
}
/**
* !#en Set values with another quaternion
* !#zh 用另一个四元数的值设置到当前对象上。
*
* @param newValue - !#en new value to set. !#zh 要设置的新值
* @return returns this
* @chainable
* 设置当前四元数使其与指定四元数相等。
* @param other 相比较的四元数。
* @returns `this`
*/
public set (newValue: Quat) {
this.x = newValue.x;
this.y = newValue.y;
this.z = newValue.z;
this.w = newValue.w;
public set (other: Quat) {
this.x = other.x;
this.y = other.y;
this.z = other.z;
this.w = other.w;
return this;
}
/**
* !#en Check whether current quaternion equals another
* !#zh 当前的四元数是否与指定的四元数相等。
*
* @param other
* @return
* 判断当前四元数是否与指定四元数相等。
* @param other 相比较的四元数。
* @returns 两四元数的各分量都相等时返回 `true`;否则返回 `false`。
*/
public equals (other: Quat) {
return other && this.x === other.x && this.y === other.y && this.z === other.z && this.w === other.w;
}
/**
* 将当前四元数转化为欧拉角(x-y-z)并赋值给出口向量。
* @param out [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public getEulerAngles (out?: Vec3) {
out = out || new Vec3();
return xquat.toEuler(out, this);
}
/**
* 根据指定的插值比率,从当前四元数到目标四元数之间做插值。
* @param to 目标四元数。
* @param ratio 插值比率,范围为 [0,1]。
* @param out 当此参数定义时,本方法将插值结果赋值给此参数并返回此参数。
* @returns 当前四元数到目标四元数之间的**球形插值**结果。
*/
public lerp (to: Quat, ratio: number, out?: Quat) {
out = out || new Quat();
xquat.slerp(out, this, to, ratio);
@@ -133,23 +138,19 @@ export default class Quat extends ValueType {
CCClass.fastDefine('cc.Quat', Quat, { x: 0, y: 0, z: 0, w: 1 });
/**
* !#en The convenience method to create a new {{#crossLink "Quat"}}cc.Quat{{/crossLink}}.
* !#zh 通过该简便的函数进行创建 {{#crossLink "Quat"}}cc.Quat{{/crossLink}} 对象。
*
* @param other
* @return
* 构造与指定四元数相等的四元数。等价于 `new Quat(other)`。
* @param other 相比较的四元数。
* @returns `new Quat(other)`
*/
export function quat (other: Quat): Quat;
/**
* !#en The convenience method to create a new {{#crossLink "Quat"}}cc.Quat{{/crossLink}}.
* !#zh 通过该简便的函数进行创建 {{#crossLink "Quat"}}cc.Quat{{/crossLink}} 对象。
*
* @param [x=0]
* @param [y=0]
* @param [z=0]
* @param [w=1]
* @return
* 构造具有指定分量的四元数。等价于 `new Quat(x, y, z, w)`。
* @param [x=0] 指定的 x 分量。
* @param [y=0] 指定的 y 分量。
* @param [z=0] 指定的 z 分量。
* @param [w=0] 指定的 w 分量。
* @returns `new Quat(x, y, z)`
*/
export function quat (x?: number, y?: number, z?: number, w?: number): Quat;
+136 -172
View File
@@ -31,124 +31,140 @@ import { ValueType } from './value-type';
import Vec2 from './vec2';
/**
* !#en A 2D rectangle defined by x, y position and width, height.
* !#zh 通过位置和宽高定义的 2D 矩形。
* @class Rect
* @extends ValueType
* 轴对齐矩形。
* 矩形内的所有点都大于等于矩形的最小点 (xMin, yMin) 并且小于等于矩形的最大点 (xMax, yMax)。
* 矩形的宽度定义为 xMax - xMin;高度定义为 yMax - yMin。
*/
export default class Rect extends ValueType {
/**
* !#en The minimum x value, equals to rect.x
* !#zh 矩形 x 轴上的最小值,等价于 rect.x。
* 获取或设置矩形在 x 轴上的最小值。
*/
get xMin () { return this.x; }
get xMin () {
return this.x;
}
set xMin (value) {
this.width += this.x - value;
this.x = value;
}
/**
* !#en The minimum y value, equals to rect.y
* !#zh 矩形 y 轴上的最小值。
* 获取或设置矩形在 y 轴上的最小值。
*/
get yMin () { return this.y; }
get yMin () {
return this.y;
}
set yMin (value) {
this.height += this.y - value;
this.y = value;
}
/**
* !#en The maximum x value.
* !#zh 矩形 x 轴上的最大值。
* 获取或设置矩形在 x 轴上的最大值。
*/
get xMax () { return this.x + this.width; }
set xMax (value) { this.width = value - this.x; }
get xMax () {
return this.x + this.width;
}
set xMax (value) {
this.width = value - this.x;
}
/**
* !#en The maximum y value.
* !#zh 矩形 y 轴上的最大值。
* 获取或设置矩形在 y 轴上的最大值。
*/
get yMax () { return this.y + this.height; }
set yMax (value) { this.height = value - this.y; }
get yMax () {
return this.y + this.height;
}
set yMax (value) {
this.height = value - this.y;
}
/**
* !#en The position of the center of the rectangle.
* !#zh 矩形的中心点。
* 获取或设置矩形中心点的坐标。
*/
get center () {
return new cc.Vec2(this.x + this.width * 0.5,
return new Vec2(this.x + this.width * 0.5,
this.y + this.height * 0.5);
}
set center (value) {
this.x = value.x - this.width * 0.5;
this.y = value.y - this.height * 0.5;
}
/**
* !#en The X and Y position of the rectangle.
* !#zh 矩形的 x 和 y 坐标。
* 获取或设置矩形最小点的坐标。
*/
get origin () {
return new cc.Vec2(this.x, this.y);
}
set origin (value) {
this.x = value.x;
this.y = value.y;
}
/**
* !#en Width and height of the rectangle.
* !#zh 矩形的大小。
* 获取或设置矩形的尺寸。
*/
get size () {
return new Size(this.width, this.height);
}
set size (value) {
this.width = value.width;
this.height = value.height;
}
/**
* !#en Creates a rectangle from two coordinate values.
* !#zh 根据指定 2 个坐标创建出一个矩形区域。
* @param v1
* @param v2
* @return
* @example
* cc.Rect.fromMinMax(cc.v2(10, 10), cc.v2(20, 20)); // Rect {x: 10, y: 10, width: 10, height: 10};
* 由任意两个点创建一个矩形,目标矩形即是这两个点各向 x、y 轴作线所得到的矩形。
* @param v1 指定的点。
* @param v2 指定的点。
* @returns 目标矩形。
*/
public static fromMinMax (v1: Vec2, v2: Vec2) {
const min_x = Math.min(v1.x, v2.x);
const min_y = Math.min(v1.y, v2.y);
const max_x = Math.max(v1.x, v2.x);
const max_y = Math.max(v1.y, v2.y);
return new Rect(min_x, min_y, max_x - min_x, max_y - min_y);
const minX = Math.min(v1.x, v2.x);
const minY = Math.min(v1.y, v2.y);
const maxX = Math.max(v1.x, v2.x);
const maxY = Math.max(v1.y, v2.y);
return new Rect(minX, minY, maxX - minX, maxY - minY);
}
/**
* 获取或设置矩形最小点的 x 坐标。
*/
public x: number;
/**
* 获取或设置矩形最小点的 y 坐标。
*/
public y: number;
/**
* 获取或设置矩形的宽度。
*/
public width: number;
/**
* 获取或设置矩形的高度。
*/
public height: number;
/**
* !#en
* Constructor of Rect class.
* see {{#crossLink "cc/rect:method"}} cc.rect {{/crossLink}} for convenience method.
* !#zh
* Rect类的构造函数。可以通过 {{#crossLink "cc/rect:method"}} cc.rect {{/crossLink}} 简便方法进行创建。
* 构造与指定矩形相等的矩形。
* @param other 相比较的矩形。
*/
constructor (rect: Rect);
constructor (other: Rect);
/**
* !#en
* Constructor of Rect class.
* see {{#crossLink "cc/rect:method"}} cc.rect {{/crossLink}} for convenience method.
* !#zh
* Rect类的构造函数。可以通过 {{#crossLink "cc/rect:method"}} cc.rect {{/crossLink}} 简便方法进行创建。
* 构造具有指定的最小值和尺寸的矩形。
* @param x 矩形在 x 轴上的最小值。
* @param y 矩形在 y 轴上的最小值。
* @param width 矩形的宽度。
* @param height 矩形的高度。
*/
constructor (x?: number, y?: number, width?: number, height?: number);
@@ -168,24 +184,28 @@ export default class Rect extends ValueType {
}
/**
* !#en TODO
* !#zh 克隆一个新的 Rect。
* @example
* var a = new cc.Rect(0, 0, 10, 10);
* a.clone();// Rect {x: 0, y: 0, width: 10, height: 10}
* 克隆当前矩形。
*/
public clone () {
return new Rect(this.x, this.y, this.width, this.height);
}
/**
* !#en TODO
* !#zh 是否等于指定的矩形。
* @param other
* @example
* var a = new cc.Rect(0, 0, 10, 10);
* var b = new cc.Rect(0, 0, 10, 10);
* a.equals(b);// true;
* 设置当前矩形使其与指定矩形相等。
* @param other 相比较的矩形。
* @returns `this`
*/
public set (other: Rect) {
this.x = other.x;
this.y = other.y;
this.width = other.width;
this.height = other.height;
}
/**
* 判断当前矩形是否与指定矩形相等。
* @param other 相比较的矩形。
* @returns 两矩阵的最小值和最大值都分别相等时返回 `true`;否则返回 `false`。
*/
public equals (other: Rect) {
return this.x === other.x &&
@@ -195,21 +215,11 @@ export default class Rect extends ValueType {
}
/**
* !#en TODO
* !#zh 线性插值
*
* @param to
* @param ratio - the interpolation coefficient.
* @param out - optional, the receiving vector.
* @example
* var a = new cc.Rect(0, 0, 10, 10);
* var b = new cc.Rect(50, 50, 100, 100);
* update (dt) {
* // method 1;
* var c = a.lerp(b, dt * 0.1);
* // method 2;
* a.lerp(b, dt * 0.1, c);
* }
* 根据指定的插值比率,从当前矩形到目标矩形之间做插值。
* @param to 目标矩形。
* @param ratio 插值比率,范围为 [0,1]。
* @param out 当此参数定义时,本方法将插值结果赋值给此参数并返回此参数。
* @returns 当前矩形最小值到目标矩形最小值之间,以及当前矩阵尺寸到目标矩形尺寸之间,按指定插值比率进行线性插值构成的矩形。
*/
public lerp (to: Rect, ratio: number, out?: Rect) {
out = out || new Rect();
@@ -224,23 +234,18 @@ export default class Rect extends ValueType {
return out;
}
public set (source: Rect) {
this.x = source.x;
this.y = source.y;
this.width = source.width;
this.height = source.height;
/**
* 返回当前矩形的字符串表示。
* @returns 当前矩形的字符串表示。
*/
public toString () {
return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)}, ${this.width.toFixed(2)}, ${this.height.toFixed(2)})`;
}
/**
* !#en Check whether the current rectangle intersects with the given one
* !#zh 当前矩形与指定矩形是否相交。
*
* @param other
* @return
* @example
* var a = new cc.Rect(0, 0, 10, 10);
* var b = new cc.Rect(0, 0, 20, 20);
* a.intersects(b);// true
* 判断当前矩形是否与指定矩形相交。
* @param other 相比较的矩形。
* @returns 相交则返回 `true`,否则返回 `false`。
*/
public intersects (other: Rect) {
const maxax = this.x + this.width;
@@ -251,27 +256,20 @@ export default class Rect extends ValueType {
}
/**
* !#en Returns the overlapping portion of 2 rectangles.
* !#zh 返回 2 个矩形重叠的部分。
*
* @param out Stores the result
* @param rectB
* @return Returns the out parameter
* @example
* var a = new cc.Rect(0, 10, 20, 20);
* var b = new cc.Rect(0, 10, 10, 10);
* var intersection = new cc.Rect();
* a.intersection(intersection, b); // intersection {x: 0, y: 10, width: 10, height: 10};
* 计算当前矩形与指定矩形重叠部分的矩形,将其赋值给出口矩形。
* @param out 出口矩形。
* @param other 指定的矩形。
* @returns `out`
*/
public intersection (out: Rect, rectB: Rect) {
public intersection (out: Rect, other: Rect) {
const axMin = this.x;
const ayMin = this.y;
const axMax = this.x + this.width;
const ayMax = this.y + this.height;
const bxMin = rectB.x;
const byMin = rectB.y;
const bxMax = rectB.x + rectB.width;
const byMax = rectB.y + rectB.height;
const bxMin = other.x;
const byMin = other.y;
const bxMax = other.x + other.width;
const byMax = other.y + other.height;
out.x = Math.max(axMin, bxMin);
out.y = Math.max(ayMin, byMin);
out.width = Math.min(axMax, bxMax) - out.x;
@@ -280,14 +278,9 @@ export default class Rect extends ValueType {
}
/**
* !#en Check whether the current rect contains the given point
* !#zh 当前矩形是否包含指定坐标点。
* Returns true if the point inside this rectangle.
* @param point
* @example
* var a = new cc.Rect(0, 0, 10, 10);
* var b = new cc.Vec2(0, 5);
* a.contains(b);// true
* 判断当前矩形是否包含指定的点。
* @param point 指定的点。
* @returns 指定的点包含在矩形内则返回 `true`,否则返回 `false`。
*/
public contains (point: Vec2) {
return (this.x <= point.x &&
@@ -297,13 +290,9 @@ export default class Rect extends ValueType {
}
/**
* !#en Returns true if the other rect totally inside this rectangle.
* !#zh 当前矩形是否包含指定矩形。
* @param other
* @example
* var a = new cc.Rect(0, 0, 20, 20);
* var b = new cc.Rect(0, 0, 10, 10);
* a.containsRect(b);// true
* 判断当前矩形是否包含指定矩形。
* @param other 指定的矩形。
* @returns 指定矩形所有的点都包含在当前矩形内则返回 `true`,否则返回 `false`。
*/
public containsRect (other: Rect) {
return (this.x <= other.x &&
@@ -313,26 +302,20 @@ export default class Rect extends ValueType {
}
/**
* !#en Returns the smallest rectangle that contains the current rect and the given rect.
* !#zh 返回一个包含当前矩形和指定矩形的最小矩形。
* @param out Stores the result
* @param rectB
* @return Returns the out parameter
* @example
* var a = new cc.Rect(0, 10, 20, 20);
* var b = new cc.Rect(0, 10, 10, 10);
* var union = new cc.Rect();
* a.union(union, b); // union {x: 0, y: 10, width: 20, height: 20};
* 创建同时包含当前矩形和指定矩形的最小矩形,将其赋值给出口矩形。
* @param out 出口矩形。
* @param other 指定的矩形。
* @returns `out`
*/
public union (out: Rect, rectB: Rect) {
public union (out: Rect, other: Rect) {
const ax = this.x;
const ay = this.y;
const aw = this.width;
const ah = this.height;
const bx = rectB.x;
const by = rectB.y;
const bw = rectB.width;
const bh = rectB.height;
const bx = other.x;
const by = other.y;
const bw = other.width;
const bh = other.height;
out.x = Math.min(ax, bx);
out.y = Math.min(ay, by);
out.width = Math.max(ax + aw, bx + bw) - out.x;
@@ -341,10 +324,13 @@ export default class Rect extends ValueType {
}
/**
* !#en Apply matrix4 to the rect.
* !#zh 使用 mat4 对矩形进行矩阵转换。
* @param out The output rect
* @param mat The matrix4
* 应用矩阵变换到当前矩形:
* 应用矩阵变换到当前矩形的最小点得到新的最小点,
* 将当前矩形的尺寸视为二维向量应用矩阵变换得到新的尺寸;
* 并将如此构成的新矩形赋值给出口矩形。
* @param out 出口矩形。
* @param matrix 变换矩阵。
* @returns `out`
*/
public transformMat4 (out: Rect, mat: Mat4) {
const ol = this.x;
@@ -371,17 +357,6 @@ export default class Rect extends ValueType {
out.height = maxY - minY;
return out;
}
/**
* !#en Output rect informations to string
* !#zh 转换为方便阅读的字符串
* @example
* var a = new cc.Rect(0, 0, 10, 10);
* a.toString();// "(0.00, 0.00, 10.00, 10.00)";
*/
public toString () {
return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)}, ${this.width.toFixed(2)}, ${this.height.toFixed(2)})`;
}
}
CCClass.fastDefine('cc.Rect', Rect, { x: 0, y: 0, width: 0, height: 0 });
@@ -389,30 +364,19 @@ CCClass.fastDefine('cc.Rect', Rect, { x: 0, y: 0, width: 0, height: 0 });
cc.Rect = Rect;
/**
* !#en
* The convenience method to create a new Rect.
* see {{#crossLink "Rect/Rect:method"}}cc.Rect{{/crossLink}}
* !#zh
* 该方法用来快速创建一个新的矩形。{{#crossLink "Rect/Rect:method"}}cc.Rect{{/crossLink}}
* @param rect
* @example
* var a = cc.rect(new Rect(0, 0, 10, 0));
* 构造与指定矩形相等的矩形。等价于 `new Rect(rect)`。
* @param rect 相比较的矩形。
* @returns `new Rect(rect)`
*/
export function rect (rect: Rect): Rect;
/**
* !#en
* The convenience method to create a new Rect.
* see {{#crossLink "Rect/Rect:method"}}cc.Rect{{/crossLink}}
* !#zh
* 该方法用来快速创建一个新的矩形。{{#crossLink "Rect/Rect:method"}}cc.Rect{{/crossLink}}
* @param rect
* @param x
* @param y
* @param width
* @param height
* @example
* var a = cc.rect(0, 0, 10, 0);
* 构造具有指定的最小值和尺寸的矩形,等价于`new Rect(x, y, width, height)`。
* @param x 矩形在 x 轴上的最小值。
* @param y 矩形在 y 轴上的最小值。
* @param width 矩形的宽度。
* @param height 矩形的高度。
* @returns `new Rect(x, y, width, height)`
*/
export function rect (x?: number, y?: number, width?: number, height?: number): Rect;
+53 -78
View File
@@ -28,27 +28,30 @@ import CCClass from '../data/class';
import { ValueType } from './value-type';
/**
* !#en
* cc.Size is the class for size object,<br/>
* please do not use its constructor to create sizes,<br/>
* use {{#crossLink "cc/size:method"}}{{/crossLink}} alias function instead.<br/>
* It will be deprecated soon, please use cc.Vec2 instead.
*
* !#zh
* cc.Size 是 size 对象的类。<br/>
* 请不要使用它的构造函数创建的 size,<br/>
* 使用 {{#crossLink "cc/size:method"}}{{/crossLink}} 别名函数。<br/>
* 它不久将被取消,请使用cc.Vec2代替。
*
* @class Size
* 二维尺寸。
*/
export default class Size extends ValueType {
/**
* 宽度。
*/
public width: number;
/**
* 高度。
*/
public height: number;
constructor (size: Size);
/**
* 构造与指定尺寸相等的尺寸。
* @param other 相比较的尺寸。
*/
constructor (other: Size);
/**
* 构造具有指定宽度和高度的尺寸。
* @param [width=0] 指定的宽度。
* @param [height=0] 指定的高度。
*/
constructor (width?: number, height?: number);
constructor (width?: Size | number, height?: number) {
@@ -63,30 +66,26 @@ export default class Size extends ValueType {
}
/**
* !#en return a Size object with width = 0 and height = 0.
* !#zh 返回一个宽度为 0 和高度为 0 的 Size 对象。
*/
static get ZERO () {
return new Size(0.0, 0.0);
}
/**
* !#en Clone a new size object from this one.
* !#zh 克隆 size 对象。
* @example
* var a = new cc.size(10, 10);
* a.clone();// return Size {width: 0, height: 0};
* 克隆当前尺寸。
*/
public clone () {
return new Size(this.width, this.height);
}
/**
* !#en TODO
* !#zh 当前 Size 对象是否等于指定 Size 对象。
* @example
* var a = new cc.size(10, 10);
* a.equals(new cc.size(10, 10));// return true;
* 设置当前尺寸使其与指定的尺寸相等。
* @param other 相比较的尺寸。
* @returns `this`
*/
public set (other: Size) {
this.width = other.width;
this.height = other.height;
}
/**
* 判断当前尺寸是否与指定尺寸的相等。
* @param other 相比较的尺寸。
* @returns 两尺寸的宽和高都分别相等时返回 `true`;否则返回 `false`。
*/
public equals (other: Size) {
return this.width === other.width &&
@@ -94,20 +93,11 @@ export default class Size extends ValueType {
}
/**
* !#en TODO
* !#zh 线性插值。
* @param to
* @param ratio - the interpolation coefficient.
* @param out - optional, the receiving vector.
* @example
* var a = new cc.size(10, 10);
* var b = new cc.rect(50, 50, 100, 100);
* update (dt) {
* // method 1;
* var c = a.lerp(b, dt * 0.1);
* // method 2;
* a.lerp(b, dt * 0.1, c);
* }
* 根据指定的插值比率,从当前尺寸到目标尺寸之间做插值。
* @param to 目标尺寸。
* @param ratio 插值比率,范围为 [0,1]。
* @param out 当此参数定义时,本方法将插值结果赋值给此参数并返回此参数。
* @returns 当前尺寸的宽和高到目标尺寸的宽和高分别按指定插值比率进行线性插值构成的向量。
*/
public lerp (to: Size, ratio: number, out?: Size) {
out = out || new Size();
@@ -118,51 +108,36 @@ export default class Size extends ValueType {
return out;
}
public set (source: Size) {
this.width = source.width;
this.height = source.height;
}
/**
* !#en TODO
* !#zh 转换为方便阅读的字符串。
* @example
* var a = new cc.size(10, 10);
* a.toString();// return "(10.00, 10.00)";
* 返回当前尺寸的字符串表示。
* @returns 当前尺寸的字符串表示。
*/
public toString () {
return `(${this.width.toFixed(2)}, ${this.height.toFixed(2)})`;
}
/**
* 创建宽和高都为 0 的尺寸并返回。
*/
static get ZERO () {
return new Size(0.0, 0.0);
}
}
CCClass.fastDefine('cc.Size', Size, { width: 0, height: 0 });
/**
* !#en
* Helper function that creates a cc.Size.<br/>
* Please use cc.p or cc.v2 instead, it will soon replace cc.Size.
* !#zh
* 创建一个 cc.Size 对象的帮助函数。<br/>
* 注意:可以使用 cc.p 或者是 cc.v2 代替,它们将很快取代 cc.Size。
*
* @param size
* @return
* @example {@link utils/api/engine/docs/cocos2d/core/value-types/CCSize/size.js}
* 等价于 `new Size(other)`。
* @param other 相比较的尺寸。
* @returns `new Size(other)`
*/
export function size (size: Size): Size;
export function size (other: Size): Size;
/**
* !#en
* Helper function that creates a cc.Size.<br/>
* Please use cc.p or cc.v2 instead, it will soon replace cc.Size.
* !#zh
* 创建一个 cc.Size 对象的帮助函数。<br/>
* 注意:可以使用 cc.p 或者是 cc.v2 代替,它们将很快取代 cc.Size。
*
* @param [w] - width
* @param [h] - height
* @return
* @example {@link utils/api/engine/docs/cocos2d/core/value-types/CCSize/size.js}
* 等价于 `new Size(x, y)`。
* @param [x=0] 指定的宽度。
* @param [y=0] 指定的高度。
* @returns `new Size(x, y)`
*/
export function size (width?: number, height?: number): Size;
+30 -31
View File
@@ -24,65 +24,64 @@
THE SOFTWARE.
****************************************************************************/
import { errorID } from '../platform/CCDebug';
import * as js from '../utils/js';
/**
* !#en The base class of all value types.
* !#zh 所有值类型的基类。
* @class ValueType
*
* 所有值类型的基类。
*/
export class ValueType {
/**
* !#en This method returns an exact copy of current value.
* !#zh 克隆当前值,该方法返回一个新对象,新对象的值和原对象相等。
* 克隆当前值。克隆的结果值应与当前值相等,即满足 `this.equals(this, value.clone())`。
*
* 本方法的基类版本简单地返回 `this`;
* 派生类**必须**重写本方法,并且返回的对象不应当为 `this`,即满足 `this !== this.clone()`。
* @returns 克隆结果值。
*/
public clone (): ValueType {
cc.errorID('0100', js.getClassName(this) + '.clone');
errorID(100, js.getClassName(this) + '.clone');
return this;
}
/**
* !#en Compares this object with the other one.
* !#zh 当前对象是否等于指定对象。
* @param other
* 判断当前值是否与指定值相等。此判断应当具有交换性,即满足 `this.equals(other) === other.equals(this)`。
* 本方法的基类版本简单地返回 `false`。
* @param other 相比较的值。
* @returns 相等则返回 `true`,否则返回 `false`。
*/
public equals (other: this) {
cc.errorID('0100', js.getClassName(this) + '.equals');
// errorID(100, js.getClassName(this) + '.equals');
return false;
}
/**
* !#en
* Linearly interpolates between this value to to value by ratio which is in the range [0, 1].
* When ratio = 0 returns this. When ratio = 1 return to. When ratio = 0.5 returns the average of this and to.
* !#zh
* 线性插值。<br/>
* 当 ratio = 0 时返回自身,ratio = 1 时返回目标,ratio = 0.5 返回自身和目标的平均值。。
* @param to - the to value
* @param ratio - the interpolation coefficient
* @returns
* 根据指定的插值比率,从当前值到目标值之间做插值。
* 当插值比率为 `0` 时,此方法的返回值应和当前值相等,即满足 `this.lerp(other, 0, out).equals(this)`;
* 当插值比率为 `1` 时,此方法的返回值应和目标值相等,即满足 `this.lerp(other, 1, out).equals(other)`。
* 本方法的基类版本在插值比率为 `1` 时将目标值的克隆作为插值结果,在其它情况下将当前值的克隆作为插值结果。
* @param to 目标值。
* @param ratio 插值比率,范围为 [0,1]。
* @param out 当此参数定义时,本方法允许将插值结果赋值给此参数并返回此参数。
* @returns 插值结果。
*/
public lerp (to: this, ratio: number, out?: this) {
cc.errorID('0100', js.getClassName(this) + '.lerp');
return this.clone();
// errorID(100, js.getClassName(this) + '.lerp');
return ratio === 1 ? to.clone() : this.clone();
}
/**
* !#en
* Copys all the properties from another given object to this value.
* !#zh
* 从其它对象把所有属性复制到当前对象。
* @param source - the source to copy
* 赋值当前值使其与指定值相等,即在 `this.set(other)` 之后应有 `this.equals(other)`。
* 本方法的基类版本简单地返回 `this`,派生类**必须**重写本方法。
* @param other 相比较的值。
*/
public set (source: this) {
cc.errorID('0100', js.getClassName(this) + '.set');
public set (other: this) {
errorID(100, js.getClassName(this) + '.set');
}
/**
* !#en TODO
* !#zh 转换为方便阅读的字符串。
* 返回当前值的字符串表示。
* 本方法的基类版本返回空字符串。
* @returns 当前值的字符串表示。
*/
public toString () {
return '' + {};
+184 -355
View File
@@ -30,44 +30,32 @@ import Mat4 from './mat4';
import { ValueType } from './value-type';
/**
* !#en Representation of 2D vectors and points.
* !#zh 表示 2D 向量和坐标
* 二维向量。
*/
export default class Vec2 extends ValueType {
/**
* x 分量。
*/
public x: number;
/**
* y 分量。
*/
public y: number;
/**
* !#en
* Constructor
* see {{#crossLink "cc/vec2:method"}}cc.v2{{/crossLink}} or {{#crossLink "cc/p:method"}}cc.p{{/crossLink}}
* !#zh
* 构造函数,可查看 {{#crossLink "cc/vec2:method"}}cc.v2{{/crossLink}} 或者 {{#crossLink "cc/p:method"}}cc.p{{/crossLink}}
* @param v
* 构造与指定向量相等的向量。
* @param other 相比较的向量。
*/
constructor (v: Vec2);
constructor (other: Vec2);
/**
* !#en
* Constructor
* see {{#crossLink "cc/vec2:method"}}cc.v2{{/crossLink}} or {{#crossLink "cc/p:method"}}cc.p{{/crossLink}}
* !#zh
* 构造函数,可查看 {{#crossLink "cc/vec2:method"}}cc.v2{{/crossLink}} 或者 {{#crossLink "cc/p:method"}}cc.p{{/crossLink}}
* @param [x=0]
* @param [y=0]
* 构造具有指定分量的向量。
* @param [x=0] 指定的 x 分量。
* @param [y=0] 指定的 y 分量。
*/
constructor (x?: number, y?: number);
/**
* !#en
* Constructor
* see {{#crossLink "cc/vec2:method"}}cc.v2{{/crossLink}} or {{#crossLink "cc/p:method"}}cc.p{{/crossLink}}
* !#zh
* 构造函数,可查看 {{#crossLink "cc/vec2:method"}}cc.v2{{/crossLink}} 或者 {{#crossLink "cc/p:method"}}cc.p{{/crossLink}}
* @param [x=0]
* @param [y=0]
*/
constructor (x?: number | Vec2, y?: number) {
super();
if (x && typeof x === 'object') {
@@ -80,44 +68,37 @@ export default class Vec2 extends ValueType {
}
/**
* !#en clone a Vec2 object
* !#zh 克隆一个 Vec2 对象
* 克隆当前向量。
*/
public clone () {
return new Vec2(this.x, this.y);
}
/**
* !#en Sets vector with another's value
* !#zh 设置向量值。
* @param newValue - !#en new value to set. !#zh 要设置的新值
* @return returns this
* @chainable
* 设置当前向量使其与指定向量相等。
* @param other 相比较的向量。
* @returns `this`
*/
public set (newValue: Vec2) {
this.x = newValue.x;
this.y = newValue.y;
public set (other: Vec2) {
this.x = other.x;
this.y = other.y;
return this;
}
/**
* !#en Check whether two vector equal
* !#zh 当前的向量是否与指定的向量相等。
* @param other
* @return
* 判断当前向量是否与指定向量相等。
* @param other 相比较的向量。
* @returns 两向量的各分量都分别相等时返回 `true`;否则返回 `false`。
*/
public equals (other: Vec2) {
return other && this.x === other.x && this.y === other.y;
}
/**
* !#en Check whether two vector equal with some degree of variance.
* !#zh
* 近似判断两个点是否相等。<br/>
* 判断 2 个向量是否在指定数值的范围之内,如果在则返回 true,反之则返回 false。
* @param other
* @param variance
* @return
* 判断当前向量是否在误差范围 [-variance, variance] 内与指定向量相等。
* @param other 相比较的向量。
* @param variance 允许的误差,应为非负数。
* @returns 当两向量的各分量都在指定的误差范围内分别相等时,返回 `true`;否则返回 `false`。
*/
public fuzzyEquals (other: Vec2, variance: number) {
if (this.x - variance <= other.x && other.x <= this.x + variance) {
@@ -129,21 +110,19 @@ export default class Vec2 extends ValueType {
}
/**
* !#en Transform to string with vector informations
* !#zh 转换为方便阅读的字符串。
* 返回当前向量的字符串表示。
* @returns 当前向量的字符串表示。
*/
public toString () {
return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)})`;
}
/**
* !#en Calculate linear interpolation result between this vector and another one with given ratio
* !#zh 线性插值。
* @param to
* @param ratio - the interpolation coefficient
* @param [out] - optional, the receiving vector, you can pass the same vec2
* to save result to itself, if not provided, a new vec2 will be created
* @return
* 根据指定的插值比率,从当前向量到目标向量之间做插值。
* @param to 目标向量。
* @param ratio 插值比率,范围为 [0,1]。
* @param out 当此参数定义时,本方法将插值结果赋值给此参数并返回此参数。
* @returns 当前向量各个分量到目标向量对应的各个分量之间按指定插值比率进行线性插值构成的向量。
*/
public lerp (to: Vec2, ratio: number, out?: Vec2) {
out = out || new Vec2();
@@ -155,218 +134,140 @@ export default class Vec2 extends ValueType {
}
/**
* !#en Clamp the vector between from float and to float.
* !#zh
* 返回指定限制区域后的向量。<br/>
* 向量大于 max_inclusive 则返回 max_inclusive。<br/>
* 向量小于 min_inclusive 则返回 min_inclusive。<br/>
* 否则返回自身。
* @param min_inclusive
* @param max_inclusive
* @return
* @example
* var min_inclusive = cc.v2(0, 0);
* var max_inclusive = cc.v2(20, 20);
* var v1 = cc.v2(20, 20).clamp(min_inclusive, max_inclusive); // Vec2 {x: 20, y: 20};
* var v2 = cc.v2(0, 0).clamp(min_inclusive, max_inclusive); // Vec2 {x: 0, y: 0};
* var v3 = cc.v2(10, 10).clamp(min_inclusive, max_inclusive); // Vec2 {x: 10, y: 10};
* 设置当前向量的值,使其各个分量都处于指定的范围内。
* @param minInclusive 每个分量都代表了对应分量允许的最小值。
* @param maxInclusive 每个分量都代表了对应分量允许的最大值。
* @returns `this`
*/
public clampf (min_inclusive: Vec2, max_inclusive: Vec2) {
this.x = clamp(this.x, min_inclusive.x, max_inclusive.x);
this.y = clamp(this.y, min_inclusive.y, max_inclusive.y);
public clampf (minInclusive: Vec2, maxInclusive: Vec2) {
this.x = clamp(this.x, minInclusive.x, maxInclusive.x);
this.y = clamp(this.y, minInclusive.y, maxInclusive.y);
return this;
}
/**
* !#en Adds this vector. If you want to save result to another vector, use add() instead.
* !#zh 向量加法。如果你想保存结果到另一个向量,使用 add() 代替。
* @param vector
* @return returns this
* @chainable
* @example
* var v = cc.v2(10, 10);
* v.addSelf(cc.v2(5, 5));// return Vec2 {x: 15, y: 15};
* 向量加法。将当前向量加上指定向量。
* @param other 指定的向量。
* @returns `this`
*/
public addSelf (vector: Vec2) {
this.x += vector.x;
this.y += vector.y;
public addSelf (other: Vec2) {
this.x += other.x;
this.y += other.y;
return this;
}
/**
* !#en Adds two vectors, and returns the new result.
* !#zh 向量加法,并返回新结果。
* @param vector
* @param [out] - optional, the receiving vector, you can pass the same vec2
* to save result to itself, if not provided, a new vec2 will be created
* @return the result
* @example
* var v = cc.v2(10, 10);
* v.add(cc.v2(5, 5)); // return Vec2 {x: 15, y: 15};
* var v1;
* v.add(cc.v2(5, 5), v1); // return Vec2 {x: 15, y: 15};
* 向量加法。将当前向量与指定向量的相加结果赋值给出口向量。
* @param other 指定的向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public add (vector: Vec2, out?: Vec2) {
public add (other: Vec2, out?: Vec2) {
out = out || new Vec2();
out.x = this.x + vector.x;
out.y = this.y + vector.y;
out.x = this.x + other.x;
out.y = this.y + other.y;
return out;
}
/**
* !#en Subtracts one vector from this. If you want to save result to another vector, use sub() instead.
* !#zh 向量减法。如果你想保存结果到另一个向量,可使用 sub() 代替。
* @param vector
* @return returns this
* @chainable
* @example
* var v = cc.v2(10, 10);
* v.subSelf(cc.v2(5, 5));// return Vec2 {x: 5, y: 5};
* 向量减法。将当前向量减去指定向量。
* @param other 减数向量。
* @returns `this`
*/
public subSelf (vector: Vec2) {
this.x -= vector.x;
this.y -= vector.y;
public subSelf (other: Vec2) {
this.x -= other.x;
this.y -= other.y;
return this;
}
/**
* !#en Subtracts one vector from this, and returns the new result.
* !#zh 向量减法,并返回新结果。
* @param vector
* @param [out] - optional, the receiving vector, you can pass the same vec2
* to save result to itself, if not provided, a new vec2 will be created
* @return the result
* @example
* var v = cc.v2(10, 10);
* v.sub(cc.v2(5, 5)); // return Vec2 {x: 5, y: 5};
* var v1;
* v.sub(cc.v2(5, 5), v1); // return Vec2 {x: 5, y: 5};
* 向量减法。将当前向量减去指定向量的结果赋值给出口向量。
* @param other 减数向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public sub (vector: Vec2, out?: Vec2) {
public sub (other: Vec2, out?: Vec2) {
out = out || new Vec2();
out.x = this.x - vector.x;
out.y = this.y - vector.y;
out.x = this.x - other.x;
out.y = this.y - other.y;
return out;
}
/**
* !#en Multiplies this by a number. If you want to save result to another vector, use mul() instead.
* !#zh 缩放当前向量。如果你想结果保存到另一个向量,可使用 mul() 代替。
* @param num
* @return returns this
* @chainable
* @example
* var v = cc.v2(10, 10);
* v.mulSelf(5);// return Vec2 {x: 50, y: 50};
* 向量数乘。将当前向量数乘指定标量。
* @param scalar 标量乘数。
* @returns `this`
*/
public mulSelf (num: number) {
this.x *= num;
this.y *= num;
public mulSelf (scalar: number) {
this.x *= scalar;
this.y *= scalar;
return this;
}
/**
* !#en Multiplies by a number, and returns the new result.
* !#zh 缩放向量,并返回新结果。
* @param num
* @param [out] - optional, the receiving vector, you can pass the same vec2
* to save result to itself, if not provided, a new vec2 will be created
* @return the result
* @example
* var v = cc.v2(10, 10);
* v.mul(5); // return Vec2 {x: 50, y: 50};
* var v1;
* v.mul(5, v1); // return Vec2 {x: 50, y: 50};
* 向量数乘。将当前向量数乘指定标量的结果赋值给出口向量。
* @param scalar 标量乘数。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public mul (num: number, out?: Vec2) {
public mul (scalar: number, out?: Vec2) {
out = out || new Vec2();
out.x = this.x * num;
out.y = this.y * num;
out.x = this.x * scalar;
out.y = this.y * scalar;
return out;
}
/**
* !#en Multiplies two vectors.
* !#zh 分量相乘。
* @param vector
* @return returns this
* @chainable
* @example
* var v = cc.v2(10, 10);
* v.scaleSelf(cc.v2(5, 5));// return Vec2 {x: 50, y: 50};
* 向量乘法。将当前向量乘以与指定向量。
* @param other 指定的向量。
* @returns `this`
*/
public scaleSelf (vector: Vec2) {
this.x *= vector.x;
this.y *= vector.y;
public scaleSelf (other: Vec2) {
this.x *= other.x;
this.y *= other.y;
return this;
}
/**
* !#en Multiplies two vectors, and returns the new result.
* !#zh 分量相乘,并返回新的结果。
*
* @param vector
* @param [out] - optional, the receiving vector, you can pass the same vec2
* to save result to itself, if not provided, a new vec2 will be created
* @return the result
* @example
* var v = cc.v2(10, 10);
* v.scale(cc.v2(5, 5)); // return Vec2 {x: 50, y: 50};
* var v1;
* v.scale(cc.v2(5, 5), v1); // return Vec2 {x: 50, y: 50};
* 向量乘法。将当前向量乘以与指定向量的结果赋值给当前向量。
* @param other 指定的向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public scale (vector: Vec2, out?: Vec2) {
public scale (other: Vec2, out?: Vec2) {
out = out || new Vec2();
out.x = this.x * vector.x;
out.y = this.y * vector.y;
out.x = this.x * other.x;
out.y = this.y * other.y;
return out;
}
/**
* !#en Divides by a number. If you want to save result to another vector, use div() instead.
* !#zh 向量除法。如果你想结果保存到另一个向量,可使用 div() 代替。
* @param divisor
* @return returns this
* @chainable
* @example
* var v = cc.v2(10, 10);
* v.divSelf(5); // return Vec2 {x: 2, y: 2};
* 将当前向量的各个分量除以指定标量。相当于 `this.mulSelf(1 / scalar)`。
* @param scalar 标量除数。
* @returns `this`
*/
public divSelf (num: number) {
this.x /= num;
this.y /= num;
public divSelf (scalar: number) {
this.x /= scalar;
this.y /= scalar;
return this;
}
/**
* !#en Divides by a number, and returns the new result.
* !#zh 向量除法,并返回新的结果。
*
* @param divisor
* @param [out] - optional, the receiving vector, you can pass the same vec2
* to save result to itself, if not provided, a new vec2 will be created
* @return the result
* @example
* var v = cc.v2(10, 10);
* v.div(5); // return Vec2 {x: 2, y: 2};
* var v1;
* v.div(5, v1); // return Vec2 {x: 2, y: 2};
* 将当前向量的各个分量除以指定标量的结果赋值给出口向量。相当于 `this.mul(1 / scalar, out)`。
* @param scalar 标量除数。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public div (num: number, out?: Vec2) {
public div (scalar: number, out?: Vec2) {
out = out || new Vec2();
out.x = this.x / num;
out.y = this.y / num;
out.x = this.x / scalar;
out.y = this.y / scalar;
return out;
}
/**
* !#en Negates the components. If you want to save result to another vector, use neg() instead.
* !#zh 向量取反。如果你想结果保存到另一个向量,可使用 neg() 代替。
* @return returns this
* @chainable
* @example
* var v = cc.v2(10, 10);
* v.negSelf(); // return Vec2 {x: -10, y: -10};
* 将当前向量的各个分量取反。
* @returns `this`
*/
public negSelf () {
this.x = -this.x;
@@ -375,15 +276,9 @@ export default class Vec2 extends ValueType {
}
/**
* !#en Negates the components, and returns the new result.
* !#zh 返回取反后的新向量。
* @param [out] - optional, the receiving vector, you can pass the same vec2 to
* save result to itself, if not provided, a new vec2 will be created
* @return the result
* @example
* var v = cc.v2(10, 10);
* var v1;
* v.neg(v1); // return Vec2 {x: -10, y: -10};
* 将当前向量的各个分量取反的结果赋值给出口向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public neg (out?: Vec2) {
out = out || new Vec2();
@@ -393,63 +288,41 @@ export default class Vec2 extends ValueType {
}
/**
* !#en Dot product
* !#zh 当前向量与指定向量进行点乘。
* @param [vector]
* @return the result
* @example
* var v = cc.v2(10, 10);
* v.dot(cc.v2(5, 5)); // return 100;
* 向量点乘。
* @param other 指定的向量。
* @returns 当前向量与指定向量点乘的结果。
*/
public dot (vector: Vec2) {
return this.x * vector.x + this.y * vector.y;
public dot (other: Vec2) {
return this.x * other.x + this.y * other.y;
}
/**
* !#en Cross product
* !#zh 当前向量与指定向量进行叉乘。
* @param [vector]
* @return the result
* @example
* var v = cc.v2(10, 10);
* v.cross(cc.v2(5, 5)); // return 0;
* 向量叉乘。
* @param other 指定的向量。
* @returns `out`
*/
public cross (vector: Vec2) {
return this.x * vector.y - this.y * vector.x;
public cross (other: Vec2) {
return this.x * other.y - this.y * other.x;
}
/**
* !#en Returns the length of this vector.
* !#zh 返回该向量的长度。
* @return the result
* @example
* var v = cc.v2(10, 10);
* v.mag(); // return 14.142135623730951;
* 计算向量的长度(模)。
* @returns 向量的长度(模)。
*/
public mag () {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
/**
* !#en Returns the squared length of this vector.
* !#zh 返回该向量的长度平方。
* @return the result
* @example
* var v = cc.v2(10, 10);
* v.magSqr(); // return 200;
* 计算向量长度(模)的平方。
* @returns 向量长度(模)的平方。
*/
public magSqr () {
return this.x * this.x + this.y * this.y;
}
/**
* !#en Make the length of this vector to 1.
* !#zh 向量归一化,让这个向量的长度为 1。
* @return returns this
* @chainable
* @example
* var v = cc.v2(10, 10);
* v.normalizeSelf(); // return Vec2 {x: 0.7071067811865475, y: 0.7071067811865475};
* 归一化当前向量,以使其长度(模)为 1。
*/
public normalizeSelf () {
const magSqr = this.x * this.x + this.y * this.y;
@@ -469,20 +342,9 @@ export default class Vec2 extends ValueType {
}
/**
* !#en
* Returns this vector with a magnitude of 1.<br/>
* <br/>
* Note that the current vector is unchanged and a new normalized vector is returned.
* If you want to normalize the current vector, use normalizeSelf function.
* !#zh
* 返回归一化后的向量。<br/>
* <br/>
* 注意,当前向量不变,并返回一个新的归一化向量。如果你想来归一化当前向量,可使用 normalizeSelf 函数。
* @param [out] - optional, the receiving vector, you can pass the same vec2
* to save result to itself, if not provided, a new vec2 will be created
* @return result
* var v = cc.v2(10, 10);
* v.normalize(); // return Vec2 {x: 0.7071067811865475, y: 0.7071067811865475};
* 将当前向量归一化的结果赋值给出口向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public normalize (out?: Vec2) {
out = out || new Vec2();
@@ -493,58 +355,40 @@ export default class Vec2 extends ValueType {
}
/**
* !#en Get angle in radian between this and vector.
* !#zh 夹角的弧度。
* @param vector
* @return from 0 to Math.PI
* 获取当前向量和指定向量之间的角度。
* @param other 指定的向量。
* @returns 当前向量和指定向量之间的角度(弧度制);若当前向量和指定向量中存在零向量,将返回 0。
*/
public angle (vector: Vec2) {
public angle (other: Vec2) {
const magSqr1 = this.magSqr();
const magSqr2 = vector.magSqr();
const magSqr2 = other.magSqr();
if (magSqr1 === 0 || magSqr2 === 0) {
console.warn('Can\'t get angle between zero vector');
return 0.0;
}
const dot = this.dot(vector);
const dot = this.dot(other);
let theta = dot / (Math.sqrt(magSqr1 * magSqr2));
theta = clamp(theta, -1.0, 1.0);
return Math.acos(theta);
}
/**
* !#en Get angle in radian between this and vector with direction.
* !#zh 带方向的夹角的弧度。
* @param vector
* @return from -MathPI to Math.PI
* 获取当前向量和指定向量之间的有符号角度。
* 有符号角度的取值范围为 (-180, 180],当前向量可以通过逆时针旋转有符号角度与指定向量同向。
* @param other 指定的向量。
* @returns 当前向量和指定向量之间的有符号角度(弧度制);若当前向量和指定向量中存在零向量,将返回 0。
*/
public signAngle (vector: Vec2) {
const angle = this.angle(vector);
return this.cross(vector) < 0 ? -angle : angle;
public signAngle (other: Vec2) {
const angle = this.angle(other);
return this.cross(other) < 0 ? -angle : angle;
}
/**
* !#en rotate
* !#zh 返回旋转给定弧度后的新向量。
* @param radians
* @param [out] - optional, the receiving vector, you can pass the same vec2 to
* save result to itself, if not provided, a new vec2 will be created
* @return the result
*/
public rotate (radians: number, out?: Vec2) {
out = out || new Vec2();
out.x = this.x;
out.y = this.y;
return out.rotateSelf(radians);
}
/**
* !#en rotate self
* !#zh 按指定弧度旋转向量。
* @param radians
* @return returns this
* @chainable
* 旋转当前向量。
* @param radians 旋转角度(弧度制)。
* @returns `this`
*/
public rotateSelf (radians: number) {
const sin = Math.sin(radians);
@@ -556,68 +400,61 @@ export default class Vec2 extends ValueType {
}
/**
* !#en Calculates the projection of the current vector over the given vector.
* !#zh 返回当前向量在指定 vector 向量上的投影向量。
* @param vector
* @return
* @example
* var v1 = cc.v2(20, 20);
* var v2 = cc.v2(5, 5);
* v1.project(v2); // Vec2 {x: 20, y: 20};
* 将当前向量的旋转结果赋值给出口向量。
* @param radians 旋转角度(弧度制)。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public project (vector: Vec2) {
return vector.mul(this.dot(vector) / vector.dot(vector));
}
/**
* Transforms the vec2 with a mat4. 3rd vector component is implicitly '0', 4th vector component is implicitly '1'
* @param m matrix to transform with
* @param [out] the receiving vector, you can pass the same vec2
* to save result to itself, if not provided, a new vec2 will be created
* @returns out
*/
public transformMat4 (m: Mat4, out?: Vec2) {
public rotate (radians: number, out?: Vec2) {
out = out || new Vec2();
vec2.transformMat4(out, this, m);
out.x = this.x;
out.y = this.y;
return out.rotateSelf(radians);
}
// _serialize: function () {
// return [this.x, this.y];
// },
// _deserialize: function (data) {
// this.x = data[0];
// this.y = data[1];
// }
// static
/**
* 计算当前向量在指定向量上的投影向量。
* @param other 指定的向量。
* @returns 计算出的投影向量。
*/
public project (other: Vec2) {
return other.mul(this.dot(other) / other.dot(other));
}
/**
* !#en return a Vec2 object with x = 1 and y = 1.
* !#zh 新 Vec2 对象。
* 将当前向量视为 z 分量为 0、w 分量为 1 的四维向量,
* 应用四维矩阵变换到当前矩阵,结果的 x、y 分量赋值给出口向量。
* @param matrix 变换矩阵。
* @param [out] 出口向量,当未指定时将创建为新的向量。
*/
public transformMat4 (matrix: Mat4, out?: Vec2) {
out = out || new Vec2();
vec2.transformMat4(out, this, matrix);
}
/**
* 创建分量都为 1 的向量并返回。
*/
static get ONE () {
return new Vec2(1.0, 1.0);
}
/**
* !#en return a Vec2 object with x = 0 and y = 0.
* !#zh 返回 x = 0 和 y = 0 的 Vec2 对象。
* 创建零向量并返回。
*/
static get ZERO () {
return new Vec2(0.0, 0.0);
}
/**
* !#en return a Vec2 object with x = 0 and y = 1.
* !#zh 返回 x = 0 和 y = 1 的 Vec2 对象。
* 创建与 y 轴同向的单位向量并返回。
*/
static get UP () {
return new Vec2(0.0, 1.0);
}
/**
* !#en return a Vec2 object with x = 1 and y = 0.
* !#zh 返回 x = 1 和 y = 0 的 Vec2 对象。
* 创建与 x 轴同向的单位向量并返回。
*/
static get RIGHT () {
return new Vec2(1.0, 0.0);
@@ -629,25 +466,17 @@ CCClass.fastDefine('cc.Vec2', Vec2, { x: 0, y: 0 });
cc.Vec2 = Vec2;
/**
* !#en The convenience method to create a new {{#crossLink "Vec2"}}cc.Vec2{{/crossLink}}.
* !#zh 通过该简便的函数进行创建 {{#crossLink "Vec2"}}cc.Vec2{{/crossLink}} 对象。
* @param v
* @return
* @example
* var v1 = cc.v2(0, 0);
* var v2 = cc.v2(v1);
* 等价于 `new Vec2(other)`。
* @param other 相比较的向量。
* @returns `new Vec2(other)`
*/
export function v2 (v: Vec2): Vec2;
export function v2 (other: Vec2): Vec2;
/**
* !#en The convenience method to create a new {{#crossLink "Vec2"}}cc.Vec2{{/crossLink}}.
* !#zh 通过该简便的函数进行创建 {{#crossLink "Vec2"}}cc.Vec2{{/crossLink}} 对象。
* @param [x=0]
* @param [y=0]
* @return
* @example
* var v1 = cc.v2();
* var v2 = cc.v2(0, 0);
* 等价于 `new Vec2(x, y)`。
* @param [x=0] 指定的 x 分量。
* @param [y=0] 指定的 y 分量。
* @returns `new Vec2(x, y)`
*/
export function v2 (x?: number, y?: number): Vec2;
+160 -247
View File
@@ -30,35 +30,35 @@ import Mat4 from './mat4';
import { ValueType } from './value-type';
/**
* !#en Representation of 3D vectors and points.
* !#zh 表示 3D 向量和坐标
* 三维向量。
*/
export default class Vec3 extends ValueType {
/**
* x 分量。
*/
public x: number;
/**
* y 分量。
*/
public y: number;
/**
* z 分量。
*/
public z: number;
/**
* !#en
* Constructor
* see {{#crossLink "cc/vec3:method"}}cc.v3{{/crossLink}}
* !#zh
* 构造函数,可查看 {{#crossLink "cc/vec3:method"}}cc.v3{{/crossLink}}
* @param v
* 构造与指定向量相等的向量。
* @param v 相比较的向量。
*/
constructor (v: Vec3);
/**
* !#en
* Constructor
* see {{#crossLink "cc/vec3:method"}}cc.v3{{/crossLink}}
* !#zh
* 构造函数,可查看 {{#crossLink "cc/vec3:method"}}cc.v3{{/crossLink}}
* @param [x=0]
* @param [y=0]
* @param [z=0]
* 构造具有指定分量的向量。
* @param [x=0] 指定的 x 分量。
* @param [y=0] 指定的 y 分量。
* @param [z=0] 指定的 z 分量。
*/
constructor (x?: number, y?: number, z?: number);
@@ -76,48 +76,38 @@ export default class Vec3 extends ValueType {
}
/**
* !#en clone a Vec3 value
* !#zh 克隆一个 Vec3 值
* 克隆当前向量。
*/
public clone () {
return new Vec3(this.x, this.y, this.z);
}
/**
* !#en Set the current vector value with the given vector.
* !#zh 用另一个向量设置当前的向量对象值。
*
* @param newValue - !#en new value to set. !#zh 要设置的新值
* @return returns this
* @chainable
* 设置当前向量使其与指定向量相等。
* @param other 相比较的向量。
* @returns `this`
*/
public set (newValue: Vec3) {
this.x = newValue.x;
this.y = newValue.y;
this.z = newValue.z;
public set (other: Vec3) {
this.x = other.x;
this.y = other.y;
this.z = other.z;
return this;
}
/**
* !#en Check whether the vector equals another one
* !#zh 当前的向量是否与指定的向量相等。
*
* @param other
* @return
* 判断当前向量是否与指定向量相等。
* @param other 相比较的向量。
* @returns 两向量的各分量都分别相等时返回 `true`;否则返回 `false`。
*/
public equals (other: Vec3) {
return this.x === other.x && this.y === other.y && this.z === other.z;
}
/**
* !#en Check whether two vector equal with some degree of variance.
* !#zh
* 近似判断两个点是否相等。<br/>
* 判断 2 个向量是否在指定数值的范围之内,如果在则返回 true,反之则返回 false。
*
* @param other
* @param variance
* @return
* 判断当前向量是否在误差范围 [-variance, variance] 内与指定向量相等。
* @param other 相比较的向量。
* @param variance 允许的误差,应为非负数。
* @returns 当两向量的各分量都在指定的误差范围内分别相等时,返回 `true`;否则返回 `false`。
*/
public fuzzyEquals (other: Vec3, variance: number) {
if (this.x - variance <= other.x && other.x <= this.x + variance) {
@@ -131,23 +121,19 @@ export default class Vec3 extends ValueType {
}
/**
* !#en Transform to string with vector informations
* !#zh 转换为方便阅读的字符串。
*
* @return
* 返回当前向量的字符串表示。
* @returns 当前向量的字符串表示。
*/
public toString () {
return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)}, ${this.z.toFixed(2)})`;
}
/**
* !#en Calculate linear interpolation result between this vector and another one with given ratio
* !#zh 线性插值。
*
* @param to
* @param ratio - the interpolation coefficient
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return
* 根据指定的插值比率,从当前向量到目标向量之间做插值。
* @param to 目标向量。
* @param ratio 插值比率,范围为 [0,1]。
* @param out 当此参数定义时,本方法将插值结果赋值给此参数并返回此参数。
* @returns 当前向量各个分量到目标向量对应的各个分量之间按指定插值比率进行线性插值构成的向量。
*/
public lerp (to: Vec3, ratio: number, out?: Vec3) {
out = out || new Vec3();
@@ -156,185 +142,151 @@ export default class Vec3 extends ValueType {
}
/**
* !#en Clamp the vector between from float and to float.
* !#zh
* 返回指定限制区域后的向量。<br/>
* 向量大于 max_inclusive 则返回 max_inclusive。<br/>
* 向量小于 min_inclusive 则返回 min_inclusive。<br/>
* 否则返回自身。
*
* @param min_inclusive
* @param max_inclusive
* @return
* 设置当前向量的值,使其各个分量都处于指定的范围内。
* @param minInclusive 每个分量都代表了对应分量允许的最小值。
* @param maxInclusive 每个分量都代表了对应分量允许的最大值。
* @returns `this`
*/
public clampf (min_inclusive: Vec3, max_inclusive: Vec3) {
this.x = clamp(this.x, min_inclusive.x, max_inclusive.x);
this.y = clamp(this.y, min_inclusive.y, max_inclusive.y);
this.z = clamp(this.z, min_inclusive.z, max_inclusive.z);
public clampf (minInclusive: Vec3, maxInclusive: Vec3) {
this.x = clamp(this.x, minInclusive.x, maxInclusive.x);
this.y = clamp(this.y, minInclusive.y, maxInclusive.y);
this.z = clamp(this.z, minInclusive.z, maxInclusive.z);
return this;
}
/**
* !#en Adds this vector. If you want to save result to another vector, use add() instead.
* !#zh 向量加法。如果你想保存结果到另一个向量,使用 add() 代替。
*
* @param vector
* @return returns this
* @chainable
* 向量加法。将当前向量加上指定向量。
* @param other 指定的向量。
* @returns `this`
*/
public addSelf (vector: Vec3) {
this.x += vector.x;
this.y += vector.y;
this.z += vector.z;
public addSelf (other: Vec3) {
this.x += other.x;
this.y += other.y;
this.z += other.z;
return this;
}
/**
* !#en Adds two vectors, and returns the new result.
* !#zh 向量加法,并返回新结果。
*
* @param vector
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 向量加法。将当前向量与指定向量的相加结果赋值给出口向量。
* @param other 指定的向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public add (vector: Vec3, out?: Vec3) {
public add (other: Vec3, out?: Vec3) {
out = out || new Vec3();
out.x = this.x + vector.x;
out.y = this.y + vector.y;
out.z = this.z + vector.z;
out.x = this.x + other.x;
out.y = this.y + other.y;
out.z = this.z + other.z;
return out;
}
/**
* !#en Subtracts one vector from this. If you want to save result to another vector, use sub() instead.
* !#zh 向量减法。如果你想保存结果到另一个向量,可使用 sub() 代替。
*
* @param vector
* @return returns this
* @chainable
* 向量减法。将当前向量减去指定向量。
* @param other 减数向量。
* @returns `this`
*/
public subSelf (vector: Vec3) {
this.x -= vector.x;
this.y -= vector.y;
this.z -= vector.z;
public subSelf (other: Vec3) {
this.x -= other.x;
this.y -= other.y;
this.z -= other.z;
return this;
}
/**
* !#en Subtracts one vector from this, and returns the new result.
* !#zh 向量减法,并返回新结果。
*
* @param vector
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 向量减法。将当前向量减去指定向量的结果赋值给出口向量。
* @param other 减数向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public sub (vector: Vec3, out?: Vec3) {
public sub (other: Vec3, out?: Vec3) {
out = out || new Vec3();
out.x = this.x - vector.x;
out.y = this.y - vector.y;
out.z = this.z - vector.z;
out.x = this.x - other.x;
out.y = this.y - other.y;
out.z = this.z - other.z;
return out;
}
/**
* !#en Multiplies this by a number. If you want to save result to another vector, use mul() instead.
* !#zh 缩放当前向量。如果你想结果保存到另一个向量,可使用 mul() 代替。
*
* @param num
* @return returns this
* @chainable
* 向量数乘。将当前向量数乘指定标量。
* @param scalar 标量乘数。
* @returns `this`
*/
public mulSelf (num: number) {
this.x *= num;
this.y *= num;
this.z *= num;
public mulSelf (scalar: number) {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
return this;
}
/**
* !#en Multiplies by a number, and returns the new result.
* !#zh 缩放向量,并返回新结果。
*
* @param num
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 向量数乘。将当前向量数乘指定标量的结果赋值给出口向量。
* @param scalar 标量乘数。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public mul (num: number, out?: Vec3) {
public mul (scalar: number, out?: Vec3) {
out = out || new Vec3();
out.x = this.x * num;
out.y = this.y * num;
out.z = this.z * num;
out.x = this.x * scalar;
out.y = this.y * scalar;
out.z = this.z * scalar;
return out;
}
/**
* !#en Multiplies two vectors.
* !#zh 分量相乘。
*
* @param vector
* @return returns this
* @chainable
* 向量乘法。将当前向量乘以与指定向量。
* @param other 指定的向量。
* @returns `this`
*/
public scaleSelf (vector: Vec3) {
this.x *= vector.x;
this.y *= vector.y;
this.z *= vector.z;
public scaleSelf (other: Vec3) {
this.x *= other.x;
this.y *= other.y;
this.z *= other.z;
return this;
}
/**
* !#en Multiplies two vectors, and returns the new result.
* !#zh 分量相乘,并返回新的结果。
*
* @param vector
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 向量乘法。将当前向量乘以与指定向量的结果赋值给当前向量。
* @param other 指定的向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public scale (vector: Vec3, out?: Vec3) {
public scale (other: Vec3, out?: Vec3) {
out = out || new Vec3();
out.x = this.x * vector.x;
out.y = this.y * vector.y;
out.z = this.z * vector.z;
out.x = this.x * other.x;
out.y = this.y * other.y;
out.z = this.z * other.z;
return out;
}
/**
* !#en Divides by a number. If you want to save result to another vector, use div() instead.
* !#zh 向量除法。如果你想结果保存到另一个向量,可使用 div() 代替。
*
* @param vector
* @return returns this
* @chainable
* 将当前向量的各个分量除以指定标量。相当于 `this.mulSelf(1 / scalar)`。
* @param scalar 标量除数。
* @returns `this`
*/
public divSelf (num: number) {
this.x /= num;
this.y /= num;
this.z /= num;
public divSelf (scalar: number) {
this.x /= scalar;
this.y /= scalar;
this.z /= scalar;
return this;
}
/**
* !#en Divides by a number, and returns the new result.
* !#zh 向量除法,并返回新的结果。
*
* @param vector
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 将当前向量的各个分量除以指定标量的结果赋值给出口向量。相当于 `this.mul(1 / scalar, out)`。
* @param scalar 标量除数。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public div (num: number, out?: Vec3) {
public div (scalar: number, out?: Vec3) {
out = out || new Vec3();
out.x = this.x / num;
out.y = this.y / num;
out.z = this.z / num;
out.x = this.x / scalar;
out.y = this.y / scalar;
out.z = this.z / scalar;
return out;
}
/**
* !#en Negates the components. If you want to save result to another vector, use neg() instead.
* !#zh 向量取反。如果你想结果保存到另一个向量,可使用 neg() 代替。
*
* @return returns this
* @chainable
* 将当前向量的各个分量取反。
* @returns `this`
*/
public negSelf () {
this.x = -this.x;
@@ -344,11 +296,9 @@ export default class Vec3 extends ValueType {
}
/**
* !#en Negates the components, and returns the new result.
* !#zh 返回取反后的新向量。
*
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 将当前向量的各个分量取反的结果赋值给出口向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public neg (out?: Vec3) {
out = out || new Vec3();
@@ -359,59 +309,44 @@ export default class Vec3 extends ValueType {
}
/**
* !#en Dot product
* !#zh 当前向量与指定向量进行点乘。
*
* @param [vector]
* @return the result
* 向量点乘。
* @param other 指定的向量。
* @returns 当前向量与指定向量点乘的结果。
*/
public dot (vector: Vec3) {
return this.x * vector.x + this.y * vector.y + this.z * vector.z;
public dot (other: Vec3) {
return this.x * other.x + this.y * other.y + this.z * other.z;
}
/**
* !#en Cross product
* !#zh 当前向量与指定向量进行叉乘。
*
* @param vector
* @param [out]
* @return the result
* 向量叉乘。将当前向量左叉乘指定向量的结果赋值给出口向量。
* @param other 指定的向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns 当前向量左叉乘指定向量的结果。
*/
public cross (vector: Vec3, out?: Vec3) {
public cross (other: Vec3, out?: Vec3) {
out = out || new Vec3();
vec3.cross(out, this, vector);
vec3.cross(out, this, other);
return out;
}
/**
* !#en Returns the length of this vector.
* !#zh 返回该向量的长度。
*
* @return the result
* @example
* var v = cc.v2(10, 10);
* v.mag(); // return 14.142135623730951;
* 计算向量的长度(模)。
* @returns 向量的长度(模)。
*/
public mag () {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
/**
* !#en Returns the squared length of this vector.
* !#zh 返回该向量的长度平方。
*
* @return the result
* 计算向量长度(模)的平方。
* @returns 向量长度(模)的平方。
*/
public magSqr () {
return this.x * this.x + this.y * this.y + this.z * this.z;
}
/**
* !#en Make the length of this vector to 1.
* !#zh 向量归一化,让这个向量的长度为 1。
*
* @return returns this
* @chainable
* 归一化当前向量,以使其长度(模)为 1。
*/
public normalizeSelf () {
vec3.normalize(this, this);
@@ -419,18 +354,9 @@ export default class Vec3 extends ValueType {
}
/**
* !#en
* Returns this vector with a magnitude of 1.<br/>
* <br/>
* Note that the current vector is unchanged and a new normalized vector is returned.
* If you want to normalize the current vector, use normalizeSelf function.
* !#zh
* 返回归一化后的向量。<br/>
* <br/>
* 注意,当前向量不变,并返回一个新的归一化向量。如果你想来归一化当前向量,可使用 normalizeSelf 函数。
*
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return result
* 将当前向量归一化的结果赋值给出口向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public normalize (out?: Vec3) {
out = out || new Vec3();
@@ -439,15 +365,14 @@ export default class Vec3 extends ValueType {
}
/**
* Transforms the vec3 with a mat4. 4th vector component is implicitly '1'
*
* @param m matrix to transform with
* @param [out] The receiving vector, can be `this`; if absent, a new vector would be created.
* @returns out
* 将当前向量视为 w 分量为 1 的四维向量,
* 应用四维矩阵变换到当前矩阵,结果的 x、y、z 分量赋值给出口向量。
* @param matrix 变换矩阵。
* @param [out] 出口向量,当未指定时将创建为新的向量。
*/
public transformMat4 (m: Mat4, out?: Vec3) {
public transformMat4 (matrix: Mat4, out?: Vec3) {
out = out || new Vec3();
vec3.transformMat4(out, this, m);
vec3.transformMat4(out, this, matrix);
return out;
}
}
@@ -456,30 +381,18 @@ CCClass.fastDefine('cc.Vec3', Vec3, { x: 0, y: 0, z: 0 });
cc.Vec3 = Vec3;
/**
* !#en The convenience method to create a new {{#crossLink "Vec3"}}cc.Vec3{{/crossLink}}.
* !#zh 通过该简便的函数进行创建 {{#crossLink "Vec3"}}cc.Vec3{{/crossLink}} 对象。
* @param v
* @return
* @example
* var v1 = cc.v3();
* var v2 = cc.v3(0, 0, 0);
* var v3 = cc.v3(v2);
* var v4 = cc.v3({x: 100, y: 100, z: 0});
* 等价于 `new Vec3(other)`。
* @param other 相比较的向量。
* @returns `new Vec3(other)`
*/
export function v3 (v: Vec3): Vec3;
export function v3 (other: Vec3): Vec3;
/**
* !#en The convenience method to create a new {{#crossLink "Vec3"}}cc.Vec3{{/crossLink}}.
* !#zh 通过该简便的函数进行创建 {{#crossLink "Vec3"}}cc.Vec3{{/crossLink}} 对象。
* @param [x=0]
* @param [y=0]
* @param [z=0]
* @return
* @example
* var v1 = cc.v3();
* var v2 = cc.v3(0, 0, 0);
* var v3 = cc.v3(v2);
* var v4 = cc.v3({x: 100, y: 100, z: 0});
* 等价于 `new Vec3(x, y, z)`。
* @param [x=0] 指定的 x 分量。
* @param [y=0] 指定的 y 分量。
* @param [z=0] 指定的 z 分量。
* @returns `new Vec3(x, y, z)`
*/
export function v3 (x?: number, y?: number, z?: number): Vec3;
+127 -219
View File
@@ -30,43 +30,41 @@ import Mat4 from './mat4';
import { ValueType } from './value-type';
/**
* !#en Representation of 3D vectors and points.
* !#zh 表示 3D 向量和坐标
*
* @class Vec4
* @extends ValueType
* 四维向量。
*/
export default class Vec4 extends ValueType {
/**
* x 分量。
*/
public x: number;
/**
* y 分量。
*/
public y: number;
/**
* z 分量。
*/
public z: number;
/**
* w 分量。
*/
public w: number;
/**
* !#en
* Constructor
* see {{#crossLink "cc/vec4:method"}}cc.v4{{/crossLink}}
* !#zh
* 构造函数,可查看 {{#crossLink "cc/vec4:method"}}cc.v4{{/crossLink}}
*
* @param v
* 构造与指定向量相等的向量。
* @param other 相比较的向量。
*/
constructor (v: Vec4);
constructor (other: Vec4);
/**
* !#en
* Constructor
* see {{#crossLink "cc/vec4:method"}}cc.v4{{/crossLink}}
* !#zh
* 构造函数,可查看 {{#crossLink "cc/vec4:method"}}cc.v4{{/crossLink}}
*
* @param [x=0]
* @param [y=0]
* @param [z=0]
* @param [w=0]
* 构造具有指定分量的向量。
* @param [x=0] 指定的 x 分量。
* @param [y=0] 指定的 y 分量。
* @param [z=0] 指定的 z 分量。
* @param [w=0] 指定的 w 分量。
*/
constructor (x?: number, y?: number, z?: number, w?: number);
@@ -86,51 +84,39 @@ export default class Vec4 extends ValueType {
}
/**
* !#en clone a Vec4 value
* !#zh 克隆一个 Vec4 值
*
* @return
* 克隆当前向量。
*/
public clone () {
return new Vec4(this.x, this.y, this.z, this.w);
}
/**
* !#en Set the current vector value with the given vector.
* !#zh 用另一个向量设置当前的向量对象值。
*
* @param newValue - !#en new value to set. !#zh 要设置的新值
* @return returns this
* @chainable
* 设置当前向量使其与指定向量相等。
* @param other 相比较的向量。
* @returns `this`
*/
public set (newValue: Vec4) {
this.x = newValue.x;
this.y = newValue.y;
this.z = newValue.z;
this.w = newValue.w;
public set (other: Vec4) {
this.x = other.x;
this.y = other.y;
this.z = other.z;
this.w = other.w;
return this;
}
/**
* !#en Check whether the vector equals another one
* !#zh 当前的向量是否与指定的向量相等。
*
* @param other
* @return
* 判断当前向量是否与指定向量相等。
* @param other 相比较的向量。
* @returns 两向量的各分量都分别相等时返回 `true`;否则返回 `false`。
*/
public equals (other: Vec4) {
return this.x === other.x && this.y === other.y && this.z === other.z && this.w === other.w;
}
/**
* !#en Check whether two vector equal with some degree of variance.
* !#zh
* 近似判断两个点是否相等。<br/>
* 判断 2 个向量是否在指定数值的范围之内,如果在则返回 true,反之则返回 false。
*
* @param other
* @param variance
* @return
* 判断当前向量是否在误差范围 [-variance, variance] 内与指定向量相等。
* @param other 相比较的向量。
* @param variance 允许的误差,应为非负数。
* @returns 当两向量的各分量都在指定的误差范围内分别相等时,返回 `true`;否则返回 `false`。
*/
public fuzzyEquals (other: Vec4, variance: number) {
if (this.x - variance <= other.x && other.x <= this.x + variance) {
@@ -146,23 +132,11 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Transform to string with vector informations
* !#zh 转换为方便阅读的字符串。
*
* @return
*/
public toString () {
return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)}, ${this.z.toFixed(2)}, ${this.w.toFixed(2)})`;
}
/**
* !#en Calculate linear interpolation result between this vector and another one with given ratio
* !#zh 线性插值。
*
* @param to
* @param ratio - the interpolation coefficient
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return
* 根据指定的插值比率,从当前向量到目标向量之间做插值。
* @param to 目标向量。
* @param ratio 插值比率,范围为 [0,1]。
* @param out 当此参数定义时,本方法将插值结果赋值给此参数并返回此参数。
* @returns 当前向量各个分量到目标向量对应的各个分量之间按指定插值比率进行线性插值构成的向量。
*/
public lerp (to: Vec4, ratio: number, out?: Vec4) {
out = out || new Vec4();
@@ -171,32 +145,31 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Clamp the vector between from float and to float.
* !#zh
* 返回指定限制区域后的向量。<br/>
* 向量大于 max_inclusive 则返回 max_inclusive。<br/>
* 向量小于 min_inclusive 则返回 min_inclusive。<br/>
* 否则返回自身。
*
* @param min_inclusive
* @param max_inclusive
* @return
* 返回当前向量的字符串表示。
* @returns 当前向量的字符串表示。
*/
public clampf (min_inclusive: Vec4, max_inclusive: Vec4) {
this.x = clamp(this.x, min_inclusive.x, max_inclusive.x);
this.y = clamp(this.y, min_inclusive.y, max_inclusive.y);
this.z = clamp(this.z, min_inclusive.z, max_inclusive.z);
this.w = clamp(this.w, min_inclusive.w, max_inclusive.w);
public toString () {
return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)}, ${this.z.toFixed(2)}, ${this.w.toFixed(2)})`;
}
/**
* 设置当前向量的值,使其各个分量都处于指定的范围内。
* @param minInclusive 每个分量都代表了对应分量允许的最小值。
* @param maxInclusive 每个分量都代表了对应分量允许的最大值。
* @returns `this`
*/
public clampf (minInclusive: Vec4, maxInclusive: Vec4) {
this.x = clamp(this.x, minInclusive.x, maxInclusive.x);
this.y = clamp(this.y, minInclusive.y, maxInclusive.y);
this.z = clamp(this.z, minInclusive.z, maxInclusive.z);
this.w = clamp(this.w, minInclusive.w, maxInclusive.w);
return this;
}
/**
* !#en Adds this vector. If you want to save result to another vector, use add() instead.
* !#zh 向量加法。如果你想保存结果到另一个向量,使用 add() 代替。
*
* @param vector
* @return returns this
* @chainable
* 向量加法。将当前向量加上指定向量。
* @param other 指定的向量。
* @returns `this`
*/
public addSelf (vector: Vec4) {
this.x += vector.x;
@@ -207,12 +180,10 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Adds two vectors, and returns the new result.
* !#zh 向量加法,并返回新结果。
*
* @param vector
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 向量加法。将当前向量与指定向量的相加结果赋值给出口向量。
* @param other 指定的向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public add (vector: Vec4, out?: Vec4) {
out = out || new Vec4();
@@ -224,12 +195,9 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Subtracts one vector from this. If you want to save result to another vector, use sub() instead.
* !#zh 向量减法。如果你想保存结果到另一个向量,可使用 sub() 代替。
*
* @param vector
* @return returns this
* @chainable
* 向量减法。将当前向量减去指定向量。
* @param other 减数向量。
* @returns `this`
*/
public subSelf (vector: Vec4) {
this.x -= vector.x;
@@ -240,12 +208,10 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Subtracts one vector from this, and returns the new result.
* !#zh 向量减法,并返回新结果。
*
* @param vector
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 向量减法。将当前向量减去指定向量的结果赋值给出口向量。
* @param other 减数向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public sub (vector: Vec4, out?: Vec4) {
out = out || new Vec4();
@@ -257,12 +223,9 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Multiplies this by a number. If you want to save result to another vector, use mul() instead.
* !#zh 缩放当前向量。如果你想结果保存到另一个向量,可使用 mul() 代替。
*
* @param num
* @return returns this
* @chainable
* 向量数乘。将当前向量数乘指定标量。
* @param scalar 标量乘数。
* @returns `this`
*/
public mulSelf (num: number) {
this.x *= num;
@@ -273,12 +236,10 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Multiplies by a number, and returns the new result.
* !#zh 缩放向量,并返回新结果。
*
* @param num
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 向量数乘。将当前向量数乘指定标量的结果赋值给出口向量。
* @param scalar 标量乘数。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public mul (num: number, out?: Vec4) {
out = out || new Vec4();
@@ -290,12 +251,9 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Multiplies two vectors.
* !#zh 分量相乘。
*
* @param vector
* @return returns this
* @chainable
* 向量乘法。将当前向量乘以与指定向量。
* @param other 指定的向量。
* @returns `this`
*/
public scaleSelf (vector: Vec4) {
this.x *= vector.x;
@@ -306,12 +264,10 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Multiplies two vectors, and returns the new result.
* !#zh 分量相乘,并返回新的结果。
*
* @param vector
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 向量乘法。将当前向量乘以与指定向量的结果赋值给当前向量。
* @param other 指定的向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public scale (vector: Vec4, out?: Vec4) {
out = out || new Vec4();
@@ -323,12 +279,9 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Divides by a number. If you want to save result to another vector, use div() instead.
* !#zh 向量除法。如果你想结果保存到另一个向量,可使用 div() 代替。
*
* @param vector
* @return returns this
* @chainable
* 将当前向量的各个分量除以指定标量。相当于 `this.mulSelf(1 / scalar)`。
* @param scalar 标量除数。
* @returns `this`
*/
public divSelf (num: number) {
this.x /= num;
@@ -339,12 +292,10 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Divides by a number, and returns the new result.
* !#zh 向量除法,并返回新的结果。
*
* @param vector
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 将当前向量的各个分量除以指定标量的结果赋值给出口向量。相当于 `this.mul(1 / scalar, out)`。
* @param scalar 标量除数。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public div (num: number, out?: Vec4) {
out = out || new Vec4();
@@ -356,11 +307,8 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Negates the components. If you want to save result to another vector, use neg() instead.
* !#zh 向量取反。如果你想结果保存到另一个向量,可使用 neg() 代替。
*
* @return returns this
* @chainable
* 将当前向量的各个分量取反。
* @returns `this`
*/
public negSelf () {
this.x = -this.x;
@@ -371,11 +319,9 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Negates the components, and returns the new result.
* !#zh 返回取反后的新向量。
*
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return the result
* 将当前向量的各个分量取反的结果赋值给出口向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public neg (out?: Vec4) {
out = out || new Vec4();
@@ -387,23 +333,19 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Dot product
* !#zh 当前向量与指定向量进行点乘。
*
* @param [vector]
* @return the result
* 向量点乘。
* @param other 指定的向量。
* @returns 当前向量与指定向量点乘的结果。
*/
public dot (vector: Vec4) {
return this.x * vector.x + this.y * vector.y + this.z * vector.z + this.w * vector.w;
}
/**
* !#en Cross product
* !#zh 当前向量与指定向量进行叉乘。
*
* @param vector
* @param [out]
* @return the result
* 向量叉乘。视当前向量和指定向量为三维向量(舍弃 w 分量),将当前向量左叉乘指定向量的结果赋值给出口向量。
* @param other 指定的向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public cross (vector: Vec4, out?: Vec4) {
out = out || new Vec4();
@@ -412,13 +354,8 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Returns the length of this vector.
* !#zh 返回该向量的长度。
*
* @return the result
* @example
* var v = cc.v4(10, 10);
* v.mag(); // return 14.142135623730951;
* 计算向量的长度(模)。
* @returns 向量的长度(模)。
*/
public mag () {
const { x, y, z, w } = this;
@@ -426,10 +363,8 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Returns the squared length of this vector.
* !#zh 返回该向量的长度平方。
*
* @return the result
* 计算向量长度(模)的平方。
* @returns 向量长度(模)的平方。
*/
public magSqr () {
const { x, y, z, w } = this;
@@ -437,11 +372,7 @@ export default class Vec4 extends ValueType {
}
/**
* !#en Make the length of this vector to 1.
* !#zh 向量归一化,让这个向量的长度为 1。
*
* @return returns this
* @chainable
* 归一化当前向量,以使其长度(模)为 1。
*/
public normalizeSelf () {
vec4.normalize(this, this);
@@ -449,18 +380,9 @@ export default class Vec4 extends ValueType {
}
/**
* !#en
* Returns this vector with a magnitude of 1.<br/>
* <br/>
* Note that the current vector is unchanged and a new normalized vector is returned.
* If you want to normalize the current vector, use normalizeSelf function.
* !#zh
* 返回归一化后的向量。<br/>
* <br/>
* 注意,当前向量不变,并返回一个新的归一化向量。如果你想来归一化当前向量,可使用 normalizeSelf 函数。
*
* @param [out] - The receiving vector, can be `this`; if absent, a new vector would be created.
* @return result
* 将当前向量归一化的结果赋值给出口向量。
* @param [out] 出口向量,当未指定时将创建为新的向量。
* @returns `out`
*/
public normalize (out?: Vec4) {
out = out || new Vec4();
@@ -469,11 +391,9 @@ export default class Vec4 extends ValueType {
}
/**
* Transforms the vec4 with a mat4. 4th vector component is implicitly '1'
*
* @param m matrix to transform with
* @param [out] The receiving vector, can be `this`; if absent, a new vector would be created.
* @return out
* 应用四维矩阵变换到当前矩阵,结果将赋值给出口向量。
* @param matrix 变换矩阵。
* @param [out] 出口向量,当未指定时将创建为新的向量。
*/
public transformMat4 (m: Mat4, out?: Vec4) {
out = out || new Vec4();
@@ -486,31 +406,19 @@ CCClass.fastDefine('cc.Vec4', Vec4, { x: 0, y: 0, z: 0, w: 0 });
cc.Vec4 = Vec4;
/**
* !#en The convenience method to create a new {{#crossLink "Vec4"}}cc.Vec4{{/crossLink}}.
* !#zh 通过该简便的函数进行创建 {{#crossLink "Vec4"}}cc.Vec4{{/crossLink}} 对象。
* @param v
* @return
* @example
* var v1 = cc.v4();
* var v2 = cc.v4(0, 0, 0);
* var v3 = cc.v4(v2);
* var v4 = cc.v4({x: 100, y: 100, z: 0});
* 构造与指定向量相等的向量。等价于 `new Vec4(other)`。
* @param other 相比较的向量。
* @returns `new Vec4(other)`
*/
export function v4 (v: Vec4): Vec4;
export function v4 (other: Vec4): Vec4;
/**
* !#en The convenience method to create a new {{#crossLink "Vec4"}}cc.Vec4{{/crossLink}}.
* !#zh 通过该简便的函数进行创建 {{#crossLink "Vec4"}}cc.Vec4{{/crossLink}} 对象。
* @param [x=0]
* @param [y=0]
* @param [z=0]
* @param [w=0]
* @return
* @example
* var v1 = cc.v4();
* var v2 = cc.v4(0, 0, 0);
* var v3 = cc.v4(v2);
* var v4 = cc.v4({x: 100, y: 100, z: 0});
* 构造具有指定分量的向量。等价于 `new Vec4(x, y, z, w)`。
* @param [x=0] 指定的 x 分量。
* @param [y=0] 指定的 y 分量。
* @param [z=0] 指定的 z 分量。
* @param [w=0] 指定的 w 分量。
* @returns `new Vec4(x, y, z)`
*/
export function v4 (x?: number, y?: number, z?: number, w?: number): Vec4;