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
7
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
import { float32 } from "@koalaui/compat"
19
coordinates: Float32Array
21
constructor (x: float32, y: float32) {
22
this.coordinates = new Float32Array(2)
23
this.coordinates[0] = x
24
this.coordinates[1] = y
28
return this.coordinates[0] as float32
32
return this.coordinates[1] as float32
35
offsetXY(dx: float32, dy: float32): Point {
36
return new Point(this.x + dx, this.y + dy)
39
offset(vec: Point): Point {
40
return this.offsetXY(vec.x, vec.y)
43
scale(scale: float32): Point {
44
return this.scaleXY(scale, scale)
47
scaleXY(sx: float32, sy: float32): Point {
48
return new Point(this.x * sx, this.y * sy)
51
static ZERO = new Point(0.0 as float32, 0.0 as float32)
53
toArray(): Float32Array {
54
return this.coordinates
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
66
static fromArray(points: Float32Array): Array<Point> {
67
if (points.length % 2 != 0)
68
throw new Error("Expected " + points.length + " % 2 == 0")
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)