Skip to content

[mxcad_2d API documentation] (../README. md)/[2d] (../modules/2d. md)/McGeVector3d

Class: McGeVector3d ​

2d.McGeVector3d

McGeVector3d represents directional vector objects in three-dimensional space.

Description

This class is used to describe and calculate vector relationships, and common scenarios include: Calculate the angle between two vectors, do dot product and cross product, normalize, rotate, and participate in matrix transformation. It is very commonly used in CAD geometric operations, orientation determination, and spatial transformations.

Example

ts
import { McGeVector3d } from "mxcad";

const vec1 = new McGeVector3d(10, 0, 0);
const vec2 = new McGeVector3d(0, 10, 0);

const angle = vec1.angleTo2(vec2, McGeVector3d.kZAxis);
const dot = vec1.dotProduct(vec2);
const cross = vec1.crossProduct(vec2);

Console.log ("angle:", angle);
Console.log (dot product: dot);
Console.log ("cross product:", cross);

Table of contents ​

Constructors ​

Properties ​

Accessors ​

Methods ​

Constructors ​

constructor ​

• new McGeVector3d(dX?, dY?, dZ?)

Constructor.

Parameters ​

NameTypeDescription
dX?Number \object
dY?NumberY coordinate
dZ?NumberZ coordinate

Properties ​

imp ​

• imp: any

Internal implementation object


kIdentity ​

▪ Static kIdentity: McGeVector3d

0 length vector

Example

ts

kNegateZAxis ​

▪ Static kNegateZAxis: McGeVector3d

Z-axis unit vector, pointing towards the negative Z-axis direction


kXAxis ​

▪ Static kXAxis: McGeVector3d

X-axis unit vector, pointing towards the positive X-axis direction


kYAxis ​

▪ Static kYAxis: McGeVector3d

Y-axis unit vector, pointing towards the positive Y-axis direction


kZAxis ​

▪ Static kZAxis: McGeVector3d

Z-axis unit vector, pointing towards the positive Z-axis direction

Accessors ​

vector3 ​

• get vector3(): Vector3

Returns ​

Vector3

• set vector3(val): void

Parameters ​

NameType
valVector3

Returns ​

void


x ​

• get x(): number

Get or set the X coordinate of the vector.

Returns ​

number

• set x(val): void

Parameters ​

NameType
valnumber

Returns ​

void


y ​

• get y(): number

Get or set the Y coordinate of the vector.

Returns ​

number

• set y(val): void

Parameters ​

NameType
valnumber

Returns ​

void


z ​

• get z(): number

Get or set the Z-coordinate of the vector.

Returns ​

number

• set z(val): void

Parameters ​

NameType
valnumber

Returns ​

void

Methods ​

angle ​

▸ angle(): number

Calculate the angle between the vector and the X-axis within the range of [0, 2 * Pi]

Returns ​

number


angleTo1 ​

▸ angleTo1(vec): number

Calculate the angle between two vectors within the range of [0, Pi]

Parameters ​

NameType
vecMcGeVector3d

Returns ​

number

Example

ts
import { McGeVector3d } from "mxcad";

const vec1 = new McGeVector3d(20,10,0);
const vec2 = new McGeVector3d(50,0,0);
const angle = vec1.angleTo1(vec2);

angleTo2 ​

▸ angleTo2(vec?, refVec?): number

Calculate the angle between two vectors within the range of [0,2 * Pi]

Parameters ​

NameType
vec?McGeVector3d
refVec?McGeVector3d

Returns ​

number

Example

ts
import { McGeVector3d } from "mxcad";

const vec1 = new McGeVector3d(20,10,0);
const angle = vec1.angleTo2(McGeVector3d.kXAxis, McGeVector3d.kNegateZAxis);

c ​

▸ c(): McGeVector3d

Kelon, a vector object

Returns ​

McGeVector3d

3D vector object


clone ​

▸ clone(): McGeVector3d

Kelon, a vector object

Returns ​

McGeVector3d

3D vector object

Example

ts
import { McGeVector3d } from "mxcad"

const vec1 = new McGeVector3d(20,10,0);
const vec2 = vec1.clone();

copy ​

▸ copy(val): McGeVector3d

Copy the value of the object

Parameters ​

NameTypeDescription
Val[McGeVector3d] (2d. McGeVector3d. md)3D Vector Object

Returns ​

McGeVector3d

Copy the 3D vector object

Example

ts
import { McGeVector3d } from "mxcad"

const vec1 = new McGeVector3d(20,10,0);
const vec2 = new McGeVector3d();
vec2.copy(vec1);

crossProduct ​

▸ crossProduct(vec): McGeVector3d

The cross product of two vectors

Parameters ​

NameType
vecMcGeVector3d

Returns ​

McGeVector3d

3D vector object


dotProduct ​

▸ dotProduct(vec): number

The dot product of two vectors.

The dot product can determine the degree of similarity between two directions: -The result is 0: two vectors are perpendicular; -Result greater than 0: The directions of the two vectors are basically the same; -Result less than 0: The two vectors have opposite directions.

It is commonly used to determine whether a vector is perpendicular, to determine the projection direction, and to calculate the cosine of angles in geometric scenes.

Parameters ​

NameTypeDescription
Vec[McGeVector3d] (2d. McGeVector3d. md)3D vector object

Returns ​

number

Dot product result

Example

ts
import { McGeVector3d } from "mxcad";

//Direction vector: along the positive X-axis direction
const v1 = new McGeVector3d(10, 0, 0);

//Direction vector: along the positive Y-axis direction
const v2 = new McGeVector3d(0, 10, 0);

//The dot product is 0, indicating that the two directions are perpendicular
const dot = v1.dotProduct(v2);
console.log("dot:", dot); // 0

//Continue to determine if a vector is in the same direction as the X-axis
const v3 = new McGeVector3d(5, 0, 0);
const sameDir = v1.dotProduct(v3) > 0;
Console.log ("same direction:", sameDir);//true

isEqualTo ​

▸ isEqualTo(vec): boolean

Determine whether comparing two vectors is equal

Parameters ​

NameTypeDescription
Vec[McGeVector3d] (2d. McGeVector3d. md)3D vector object

Returns ​

boolean

Boolean value


isUnitLength ​

▸ isUnitLength(): boolean

Check if the current vector is of unit length

Returns ​

boolean

Boolean value


isZeroLength ​

▸ isZeroLength(): boolean

Is it a zero vector

Returns ​

boolean

Boolean value


length ​

▸ length(): number

Obtain vector length

Returns ​

number

Vector length


mult ​

▸ mult(val): McGeVector3d

Multiply a vector with a certain value, modify the length of the vector

Parameters ​

NameTypeDescription
ValnumberNumber

Returns ​

McGeVector3d

3D vector object

Example

ts
import { McGeVector3d } from "mxcad";

const vec1 = new McGeVector3d(20,10,0);
const vec = vec1.clone().normalize().mult(20)

negate ​

▸ negate(): McGeVector3d

Vector inversion

Returns ​

McGeVector3d

Example

ts
import { McGeVector3d } from "mxcad";

const vec = new McGeVector3d(20,10,0);
vec_neg = vec.clone().negate()

normalize ​

▸ normalize(): McGeVector3d

Vector normalization operation

Returns ​

McGeVector3d

3D vector object


perpVector ​

▸ perpVector(): McGeVector3d

Vertical vector

Returns ​

McGeVector3d

Example

ts
import { McGeVector3d } from "mxcad";

const vec = new McGeVector3d(20,10,0);
vec_perp = vec.clone().perpVector()

rotateBy ​

▸ rotateBy(ang, axis?): McGeVector3d

rotate

Parameters ​

NameTypeDescription
'ang''number'Rotation angle
axis?[McGeVector3d] (2d. McGeVector3d. md)Rotation axis vector

Returns ​

McGeVector3d

Example

ts
import { McGeVector3d } from "mxcad";

const vec = new McGeVector3d(20,10,0);
vec.rotateBy(Math.PI * 0.5);

setFromVector3 ​

▸ setFromVector3(val): McGeVector3d

Parameters ​

NameType
valVector3

Returns ​

McGeVector3d


toVector3 ​

▸ toVector3(): Vector3

Convert to THREE Vector3

Returns ​

Vector3


transformBy ​

▸ transformBy(leftSide): McGeVector3d

Transform the vector using a matrix

Parameters ​

NameTypeDescription
LeftSideMcGeMatrix3d (2d. McGeMatrix3d. md)Transformation Matrix

Returns ​

McGeVector3d

The transformed vector object

Example

ts