73 lines
1.6 KiB
TypeScript
Raw Normal View History

// 坐标点
export interface Point {
x: number
y: number
}
// 矩形
export interface Rect {
// 左上角 X 轴坐标
left: number
// 左上角 Y 轴坐标
top: number
// 右下角 X 轴坐标
right: number
// 右下角 Y 轴坐标
bottom: number
// 矩形宽度
width: number
// 矩形高度
height: number
}
/**
*
* @param a A
* @param b B
*/
export const isOverlap = (a: Rect, b: Rect): boolean => {
return (
a.left < b.left + b.width &&
a.left + a.width > b.left &&
a.top < b.top + b.height &&
a.height + a.top > b.top
)
}
/**
*
* @param hotArea
* @param point
*/
export const isContains = (hotArea: Rect, point: Point): boolean => {
return (
point.x >= hotArea.left &&
point.x < hotArea.right &&
point.y >= hotArea.top &&
point.y < hotArea.bottom
)
}
/**
*
*
*
* 1. 1
* 2. X 1
* 3. Y 1
* 4.
*
* @param a
* @param b
*/
export const createRect = (a: Point, b: Point): Rect => {
// 计算矩形的范围
const [left, left2] = [a.x, b.x].sort()
const [top, top2] = [a.y, b.y].sort()
const right = left2 + 1
const bottom = top2 + 1
const height = bottom - top
const width = right - left
return { left, right, top, bottom, height, width }
}