/* * ********************************************************************************************************************* * * !! * .F88X * X8888Y * .}888888N; * i888888N; .:! .I$WI: * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8& * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I * +888888N; .8888888Y "&&8Y.}8, * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$* * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~ * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi' * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/ * .:R888888I * .&888888I Copyright (c) 2009-2020. 博锐尚格科技股份有限公司 * ~8888' * .!88~ All rights reserved. * * ********************************************************************************************************************* */ import { SPoint } from "./SPoint"; import { SSize } from "./SSize"; /** * 矩形数据类型定义 * * @author 庞利祥 */ export class SRect { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 属性定义 /** 左上角坐标 */ leftTop: SPoint; /** 大小 */ size: SSize; /** 矩形的 X 轴坐标 */ get x(): number { return this.leftTop.x; } set x(value: number) { this.leftTop.x = value; } /** 矩形的 Y 轴坐标 */ get y(): number { return this.leftTop.y; } set y(value: number) { this.leftTop.y = value; } /** 矩形的宽 */ get width(): number { return this.size.width; } set width(v: number) { this.size.width = v; } /** 矩形的高 */ get height(): number { return this.size.height; } set height(v: number) { this.size.height = v; } /** 矩形的左 */ get left(): number { return this.leftTop.x; } set left(v: number) { this.leftTop.x = v; } /** 矩形的上 */ get top(): number { return this.y; } set top(v: number) { this.y = v; } /** 矩形的右 */ get right(): number { return this.x + this.width; } set right(right: number) { this.width = right - this.x; } /** 矩形的下 */ get bottom(): number { return this.y + this.height; } set bottom(value: number) { this.height = value - this.y; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // 构造函数 /** * 构造函数 */ constructor(); /** *构造函数 * * @param leftTop 左上角坐标 * @param rightBottom 右下角坐标 */ constructor(leftTop: SPoint, rightBottom: SPoint); /** * 构造函数 * * @param leftTop 左上角坐标 * @param size 大小 */ constructor(leftTop: SPoint, size: SSize); /** * 构造函数 * * @param x X 轴坐标 * @param y Y 轴坐标 * @param width 宽度 * @param height 高度 */ constructor(x: number, y: number, width: number, height: number); /** * 构造函数 * * @param x X 轴坐标 | 左上角坐标 * @param y Y 轴坐标 | 右下角坐标 | 大小 * @param width 宽度 * @param height 高度 */ constructor( x?: number | SPoint, y?: number | SPoint | SSize, width?: number, height?: number ) { if (x == undefined) { this.leftTop = new SPoint(0, 0); this.size = new SSize(0, 0); } else if (x instanceof SPoint && y instanceof SPoint) { // constructor(leftTop: SPoint, rightBottom: SPoint) this.leftTop = new SPoint(x.x, x.y); this.size = new SSize(y.x - x.x, y.y - x.y); } else if (x instanceof SPoint && y instanceof SSize) { // constructor(leftTop: SPoint, size: SSize) this.leftTop = new SPoint(x.x, x.y); this.size = new SSize(y.width, y.height); } else { // constructor(x: number, y: number, width: number, height: number) this.leftTop = new SPoint(x as number, y as number); this.size = new SSize(width as number, height as number); } } /** * 是否为空 * * @return width 或 height 小于等于 0,返回 true,否则返回 false。 * * @see isNull(), isValid() */ isEmpty(): boolean { return this.size.isEmpty(); } /** * 是否为 Null * * @return width 与 height 都等于 0,返回 true,否则返回 false。 * * @see isEmpty(), isValid() */ isNull(): boolean { return this.size.isNull(); } /** * 是否有效 * * @return width 与 height 都大于 0,返回 true,否则返回 false。 * * @see isEmpty(), isNull() */ isValid(): boolean { return this.size.isValid(); } /** * 是否包含另一个矩阵 * * @return true 包含 */ isIn(rect: SRect): boolean { return ( this.left <= rect.left && this.right >= rect.right && this.bottom >= rect.bottom && this.top <= rect.top ); } /** * 判断矩形空间是否包含点x,y * * @param x X 坐标 * @param y Y 坐标 * @return 如果包含返回 true, 否则返回 false */ contains(x: number, y: number): boolean { return ( x >= this.left && x <= this.right && y >= this.top && y <= this.bottom ); } /** * 计算中心点 * * @return 中心点坐标 */ center(): SPoint { return new SPoint( this.x + this.width / 2.0, this.y + this.height / 2.0 ); } /** * 平移矩形 * * @param dx X 轴位移 * @param dy Y 轴位移 */ translate(dx: number, dy: number): void { this.x += dx; this.y += dy; } /** * 生成平移矩形 * * @param dx X 轴位移 * @param dy Y 轴位移 * @return 移动后的矩形 */ translated(dx: number, dy: number): SRect { return new SRect(this.x + dx, this.y + dy, this.width, this.height); } /** * 调整Rect位置 * * @param dx X 轴位移 * @param dy Y 轴位移 * @param dw 宽度调整 * @param dh 高度调整 */ adjust(dx: number, dy: number, dw: number, dh: number): void { this.x += dx; this.y += dy; this.width += dw; this.height += dh; } /** * 调整 Rect 位置 * * @param dx X 轴位移 * @param dy Y 轴位移 * @param dw 宽度调整 * @param dh 高度调整 * @return 调整后的矩形 */ adjusted(dx: number, dy: number, dw: number, dh: number): SRect { return new SRect( this.x + dx, this.y + dy, this.width + dw, this.height + dh ); } /** * 合并矩形 * * @param rect 合并的矩形 */ union(rect: SRect): void { let r = this.unioned(rect); this.x = r.x; this.y = r.y; this.width = r.width; this.height = r.height; } /** * 生成合并矩形 * * @param rect 合并的矩形 * @return 返回合并后的矩形 */ unioned(rect: SRect): SRect { let left = Math.min(this.left, rect.left); let top = Math.min(this.top, rect.top); let right = Math.max(this.right, rect.right); let bottom = Math.max(this.bottom, rect.bottom); return new SRect(left, top, right - left, bottom - top); } /** * 生成相交矩形 * * @param rect 合并的矩形 * @return 返回合并后的矩形 */ intersected(rect: SRect): SRect { let minX = this.left < rect.left ? this.left : rect.left; let minY = this.top < rect.top ? this.top : rect.top; let maxX = this.right > rect.right ? this.right : rect.right; let maxY = this.bottom > rect.bottom ? this.bottom : rect.bottom; if ( this.width + rect.width > maxX - minX && this.height + rect.height > maxY - minY ) { return new SRect(); } return new SRect(); } /** * 相交矩形 * * @param rect 合并的矩形 */ intersect(rect: SRect): SRect { let left = Math.min(this.left, rect.left); let top = Math.min(this.top, rect.top); let right = Math.max(this.right, rect.right); let bottom = Math.max(this.bottom, rect.bottom); return new SRect(left, top, right - left, bottom - top); } }