SGraphAreaGroupItem.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2009-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. import { SGraphItem } from "../SGraphItem";
  27. import {
  28. SPainter,
  29. SPath,
  30. SPoint,
  31. SPolygonUtil,
  32. SRect
  33. } from "@persagy-web/draw/lib";
  34. import { AreaGroup } from "../types/AreaGroup";
  35. import { SGraphStyleItem } from "./SGraphStyleItem";
  36. /**
  37. * 区域组
  38. *
  39. * @author 郝建龙 <haojianlong@sagacloud.cn>
  40. */
  41. export class SGraphAreaGroupItem extends SGraphStyleItem {
  42. /** X 坐标最小值 */
  43. private minX = Number.MAX_SAFE_INTEGER;
  44. /** X 坐标最大值 */
  45. private maxX = Number.MIN_SAFE_INTEGER;
  46. /** Y 坐标最小值 */
  47. private minY = Number.MAX_SAFE_INTEGER;
  48. /** Y 坐标最大值 */
  49. private maxY = Number.MIN_SAFE_INTEGER;
  50. /** 墙轮廓线坐标 list */
  51. private readonly pointList: SPoint[][][] = [];
  52. /** path 对象 */
  53. private pathList: SPath[] = [];
  54. /**
  55. * 构造函数
  56. *
  57. * @param parent 父类
  58. * @param data 轮廓线及风格数据
  59. */
  60. constructor(parent: SGraphItem | null, data: AreaGroup) {
  61. super(parent);
  62. // outline 属性符合条件
  63. if (data.outline.length && data.outline[0] && data.outline[0].length) {
  64. let tempArr = data.outline;
  65. this.minX = tempArr[0][0][0].x;
  66. this.maxX = this.minX;
  67. this.minY = tempArr[0][0][0].y;
  68. this.maxY = this.minY;
  69. // 处理轮廓点数组,同时计算最大最小值
  70. this.pointList = tempArr.map((t): SPoint[][] => {
  71. let sPath = new SPath();
  72. let tempArr = t.map((it): SPoint[] => {
  73. let array = it.map(
  74. (item): SPoint => {
  75. let x = item.x,
  76. y = item.y;
  77. if (x < this.minX) {
  78. this.minX = x;
  79. }
  80. if (y < this.minY) {
  81. this.minY = y;
  82. }
  83. if (x > this.maxX) {
  84. this.maxX = x;
  85. }
  86. if (y > this.maxY) {
  87. this.maxY = y;
  88. }
  89. return new SPoint(x, y);
  90. }
  91. );
  92. sPath.polygon(array);
  93. return array;
  94. });
  95. this.pathList.push(sPath);
  96. return tempArr;
  97. });
  98. } else {
  99. this.minX = 0;
  100. this.maxX = 0;
  101. this.minY = 0;
  102. this.maxY = 0;
  103. }
  104. }
  105. /**
  106. * Item 对象边界区域
  107. *
  108. * @return 对象边界区域
  109. */
  110. boundingRect(): SRect {
  111. return new SRect(
  112. this.minX,
  113. this.minY,
  114. this.maxX - this.minX,
  115. this.maxY - this.minY
  116. );
  117. }
  118. /**
  119. * 判断点是否在区域内
  120. *
  121. * @param x 点 x 坐标
  122. * @param y 点 y 坐标
  123. * @return 是否在区域内
  124. */
  125. contains(x: number, y: number): boolean {
  126. // 遍历点集
  127. for (let j = 0; j < this.pointList.length; j++) {
  128. let arr = this.pointList[j];
  129. // 空数组 或者 不在第一个轮廓内
  130. if (arr.length < 1 || !SPolygonUtil.pointIn(x, y, arr[0])) {
  131. continue;
  132. }
  133. // 在第一个轮廓内
  134. if (arr.length == 1) {
  135. return true;
  136. }
  137. let flag = false;
  138. // 是否在后边的轮廓内
  139. for (let i = 1; i < arr.length; i++) {
  140. if (SPolygonUtil.pointIn(x, y, arr[i])) {
  141. flag = true;
  142. break;
  143. }
  144. }
  145. // 在后边的轮廓内
  146. if (flag) {
  147. continue;
  148. }
  149. return true;
  150. }
  151. return false;
  152. }
  153. /**
  154. * Item 绘制操作
  155. *
  156. * @param painter 绘制对象
  157. */
  158. onDraw(painter: SPainter): void {
  159. painter.pen.color = this.strokeColor;
  160. painter.brush.color = this.fillColor;
  161. // 边宽是否需要转成像素
  162. if (this.widthIsPx) {
  163. painter.pen.lineWidth = painter.toPx(this.lineWidth);
  164. } else {
  165. painter.pen.lineWidth = this.lineWidth;
  166. }
  167. this.pathList.forEach((t): void => {
  168. painter.drawPath(t);
  169. });
  170. }
  171. }