1/* 2 * Copyright (c) 2022-2025 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 Point { 19 coordinates: Float32Array 20 21 constructor (x: float32, y: float32) { 22 this.coordinates = new Float32Array(2) 23 this.coordinates[0] = x 24 this.coordinates[1] = y 25 } 26 27 get x(): float32 { 28 return this.coordinates[0] as float32 29 } 30 31 get y(): float32 { 32 return this.coordinates[1] as float32 33 } 34 35 offsetXY(dx: float32, dy: float32): Point { 36 return new Point(this.x + dx, this.y + dy) 37 } 38 39 offset(vec: Point): Point { 40 return this.offsetXY(vec.x, vec.y) 41 } 42 43 scale(scale: float32): Point { 44 return this.scaleXY(scale, scale) 45 } 46 47 scaleXY(sx: float32, sy: float32): Point { 48 return new Point(this.x * sx, this.y * sy) 49 } 50 51 static ZERO = new Point(0.0 as float32, 0.0 as float32) 52 53 toArray(): Float32Array { 54 return this.coordinates 55 } 56 57 static flattenArray(points: Array<Point>): Float32Array { 58 let array = new Float32Array(points.length * 2) 59 for (let i = 0; i < points.length; i++) { 60 array[i * 2] = points[i].x 61 array[i * 2 + 1] = points[i].y 62 } 63 return array 64 } 65 66 static fromArray(points: Float32Array): Array<Point> { 67 if (points.length % 2 != 0) 68 throw new Error("Expected " + points.length + " % 2 == 0") 69 70 let array = new Array<Point>(points.length / 2) 71 for (let i = 0; i < points.length / 2; i++) { 72 array[i] = new Point(points[i * 2] as float32, points[i * 2 + 1] as float32) 73 } 74 return array 75 } 76} 77