本页内容

vector2d QML Value Type

vector2d 类型具有 x 和 y 属性。更多内容...

Import Statement: import QtQuick

详细说明

vector2d 类型具有x 和y 属性,除此之外,它与vector3d 类型相似。

vector2d 类型的属性默认值为零向量。此时,x 和y 均被设置为0 。

有关更多信息,请参阅关于vector3d 类型的文档。

要创建一个vector2d 值,可以将其指定为“x,y”字符串,也可以单独定义各分量,或者使用 Qt.vector2d() 函数进行组合。

vector2d 类型具有以下可在 QML 中调用的幂等函数:

函数签名描述示例
real dotProduct(vector2d other)返回向量this 与向量other 的点积结果,该结果为实数标量
var a = Qt.vector2d(1,2);
var b = Qt.vector2d(3,4);
var c = a.dotProduct(b);
console.log(c); // 11
vector2d times(vector2d other)返回将this 向量与other 向量相乘后得到的vector2d结果
var a = Qt.vector2d(1,2);
var b = Qt.vector2d(3,4);
var c = a.times(b);
console.log(c.toString()); // QVector2D(3, 8)
vector2d times(real factor)返回将this 向量与标量相乘所得的vector2d结果factor
var a = Qt.vector2d(1,2);
var b = 4.48;
var c = a.times(b);
console.log(c.toString()); // QVector2D(4.48, 8.96)
vector2d plus(vector2d other)返回将this 向量2d与other 向量2d相加后得到的向量2d结果
var a = Qt.vector2d(1,2);
var b = Qt.vector2d(3,4);
var c = a.plus(b);
console.log(c.toString()); // QVector2D(4, 6)
vector2d minus(vector2d other)返回将other 向量减去this 向量后的vector2d结果
var a = Qt.vector2d(1,2);
var b = Qt.vector2d(3,4);
var c = a.minus(b);
console.log(c.toString()); // QVector2D(-2, -2)
vector2d normalized()返回this 向量的归一化形式
var a = Qt.vector2d(1,2);
var b = a.normalized();
console.log(b.toString()); // QVector2D(0.447214, 0.894427)
real length()返回this 向量2D的长度的实数标量值
var a = Qt.vector2d(1,2);
var b = a.length();
console.log(b.toString()); // 2.23606797749979
vector3d toVector3d()返回将this 向量2d转换为vector3d后的结果
var a = Qt.vector2d(1,2);
var b = a.toVector3d();
console.log(b.toString()); // QVector3D(1, 2, 0)
vector4d toVector4d()返回将this vector2d 转换为 vector4d 后的 vector4d 结果
var a = Qt.vector2d(1,2);
var b = a.toVector4d();
console.log(b.toString()); // QVector4D(1, 2, 0, 0)
bool fuzzyEquals(vector2d other, real epsilon)如果this 的vector2d与other 的vector2d近似相等,则返回true。当this 的每个属性均在epsilon 范围内与other 相同时,该近似关系成立。请注意,epsilon 是一个可选参数,其默认值epsilon 为0.00001。
var a = Qt.vector2d(1,2);
var b = Qt.vector2d(1.0001, 1.9998);
var c = a.fuzzyEquals(b);        // default epsilon
var d = a.fuzzyEquals(b, 0.005); // supplied epsilon
console.log(c + " " + d); // false true

该值类型由QtQuick 导入提供。

另请参阅 QML 值类型。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.