idlize
1/*
2* Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16import { float32 } from "@koalaui/compat"17
18export class Point3 {19x: float3220y: float3221z: float3222constructor(x: float32, y: float32, z: float32) {23this.x = x24this.y = y25this.z = z26}27
28subtract(value: Point3): Point3 {29return new Point3(30this.x - value.x,31this.y - value.y,32this.z - value.z33)34}35
36cross(value: Point3): Point3 {37return new Point3(38this.y * value.z - this.z * value.y,39this.z * value.x - this.x * value.z,40this.x * value.y - this.y * value.x41)42}43
44normalize(): Point3 {45const mag = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z) as float3246const tolerance = (1.0 / (1 << 12))47if (mag < tolerance) {48// This semicolon after return this is a workaround for ArkTS bug49return this;50}51return new Point3(this.x / mag, this.y / mag, this.z / mag)52}53}