You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
vue-webtopo-svgeditor/src/utils/index.ts

75 lines
2.1 KiB
TypeScript

/**
*
* @param len
*/
export const randomString = (len?: number) => {
len = len || 10;
const str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const maxPos = str.length;
let random_str = '';
for (let i = 0; i < len; i++) {
random_str += str.charAt(Math.floor(Math.random() * maxPos));
}
return random_str;
};
// 通过泛型定义通用类型保护函数
export const isOfType = <T>(target: unknown, prop: keyof T): target is T => {
return (target as T)[prop] !== undefined;
};
/**
*
* @param length /
* @param scale
* @returns
*/
export const getCoordinateOffset = (length: number, scale: number) => {
return (length / 2) * (scale - 1);
};
// 角度转弧度
// Math.PI = 180 度
export const angleToRadian = (angle: number) => {
return (angle * Math.PI) / 180;
};
/**
*
* @param {Object} point
* @param {Object} center
* @param {Number} rotate
* @return {Object}
* https://www.zhihu.com/question/67425734/answer/252724399 旋转矩阵公式
*/
export const calculateRotatedPointCoordinate = (
point: { x: number; y: number },
center: { x: number; y: number },
rotate: number
) => {
/**
*
* a(x, y)
* c(x, y)
* n(x, y)
* θ tan ??
* nx = cosθ * (ax - cx) - sinθ * (ay - cy) + cx
* ny = sinθ * (ax - cx) + cosθ * (ay - cy) + cy
*/
return {
x:
(point.x - center.x) * Math.cos(angleToRadian(rotate)) -
(point.y - center.y) * Math.sin(angleToRadian(rotate)) +
center.x,
y:
(point.x - center.x) * Math.sin(angleToRadian(rotate)) +
(point.y - center.y) * Math.cos(angleToRadian(rotate)) +
center.y
};
};
// 求两点之间的中点坐标
export const getCenterPoint = (p1: { x: number; y: number }, p2: { x: number; y: number }) => {
return {
x: p1.x + (p2.x - p1.x) / 2,
y: p1.y + (p2.y - p1.y) / 2
};
};