SMathUtil.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 { SLine, SPoint, SRect } from "@persagy-web/draw";
  27. import { MinDis } from "../types/MinDis";
  28. import { Point } from "../types/Point";
  29. import { PointToLine } from "../types/PointToLine";
  30. import { Outline } from "../types/Outline";
  31. // @ts-ignore
  32. import { intersect } from "polybooljs";
  33. export class SMathUtil {
  34. /**
  35. * 计算点到点距离
  36. *
  37. * @return 距离
  38. * @param x1
  39. * @param y1
  40. * @param x2
  41. * @param y2
  42. */
  43. static pointDistance(
  44. x1: number,
  45. y1: number,
  46. x2: number,
  47. y2: number
  48. ): number {
  49. return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  50. } // Function pointDistance()
  51. /**
  52. * 计算点到点集中点最小距离,返回该点和该距离
  53. *
  54. * @param p 第一个点
  55. * @param arr 点集
  56. * @return 距离,点坐标
  57. */
  58. static getMinDisPoint(p: SPoint, arr: Point[]): MinDis | null {
  59. if (!arr.length) {
  60. return null;
  61. }
  62. let minDis = {
  63. MinDis: SMathUtil.pointDistance(p.x, p.y, arr[0].X, -arr[0].Y),
  64. Point: arr[0]
  65. };
  66. for (let i = 1; i < arr.length; i++) {
  67. let ds = SMathUtil.pointDistance(p.x, p.y, arr[i].X, -arr[i].Y);
  68. if (ds < minDis.MinDis) {
  69. minDis.MinDis = ds;
  70. minDis.Point = arr[i];
  71. }
  72. }
  73. return minDis;
  74. } // Function getMinDisPoint()
  75. /**
  76. * 计算点到线段垂线与线段的交点
  77. *
  78. * @param p 点
  79. * @param l 线段
  80. * @return 距离
  81. */
  82. static pointToLine(p: SPoint, l: SLine): PointToLine {
  83. let d = {
  84. MinDis: Number.MAX_SAFE_INTEGER,
  85. Line: new SLine(),
  86. Point: new SPoint()
  87. };
  88. let bgX = Math.max(l.x1, l.x2);
  89. let smX = Math.min(l.x1, l.x2);
  90. if (l.dx == 0) {
  91. // l.dx为0 说明线段是垂直于x轴的
  92. let bgY = Math.max(l.y1, l.y2);
  93. let smY = Math.min(l.y1, l.y2);
  94. if (p.y > smY && p.y < bgY) {
  95. d.MinDis = Math.abs(p.x - l.x1);
  96. d.Line = l;
  97. d.Point = new SPoint(l.x1, p.y);
  98. }
  99. } else if (l.dy == 0) {
  100. // l.dy为0 说明线段是平行于x轴的
  101. if (p.x > smX && p.x < bgX) {
  102. d.MinDis = Math.abs(p.y - l.y1);
  103. d.Line = l;
  104. d.Point = new SPoint(p.x, l.y1);
  105. }
  106. } else {
  107. // 直线1
  108. let k1 = (l.y1 - l.y2) / (l.x1 - l.x2);
  109. let b1 = l.y1 - k1 * l.x1;
  110. // 直线2
  111. let k2 = -1 / k1;
  112. let b2 = p.y - k2 * p.x;
  113. // 交点
  114. let x = (b1 - b2) / (k2 - k1);
  115. let y = k1 * x + b1;
  116. if (x > smX && x < bgX) {
  117. d.MinDis = SMathUtil.pointDistance(p.x, p.y, x, y);
  118. d.Line = l;
  119. d.Point = new SPoint(x, y);
  120. }
  121. }
  122. return d;
  123. } // Function pointDistance()
  124. /**
  125. * 计算点到点集中线段最小距离,返回该点和该距离
  126. *
  127. * @param p 第一个点
  128. * @param arr 点集
  129. * @return 距离,点坐标
  130. */
  131. static getMinDisLine(p: SPoint, arr: Point[]): PointToLine | null {
  132. if (arr.length < 2) {
  133. return null;
  134. }
  135. let PTL = SMathUtil.pointToLine(
  136. p,
  137. new SLine(
  138. arr[arr.length - 1].X,
  139. -arr[arr.length - 1].Y,
  140. arr[0].X,
  141. -arr[0].Y
  142. )
  143. );
  144. for (let i = 0; i < arr.length - 1; i++) {
  145. let temp = SMathUtil.pointToLine(
  146. p,
  147. new SLine(arr[i].X, -arr[i].Y, arr[i + 1].X, -arr[i + 1].Y)
  148. );
  149. if (temp.MinDis < PTL.MinDis) {
  150. PTL = temp;
  151. }
  152. }
  153. return PTL;
  154. } // Function getMinDisPoint()
  155. /**
  156. * 计算矩形是否有交集(外包矩形算法)
  157. *
  158. * @param rect1 矩形1
  159. * @param rect2 矩形2
  160. * @return boolean
  161. * */
  162. static rectIntersection(rect1: SRect, rect2: SRect): boolean {
  163. let minX = rect1.x < rect2.x ? rect1.x : rect2.x;
  164. let minY = rect1.y < rect2.y ? rect1.y : rect2.y;
  165. let maxX = rect1.right > rect2.right ? rect1.right : rect2.right;
  166. let maxY = rect1.bottom > rect2.bottom ? rect2.bottom : rect2.bottom;
  167. return (
  168. rect1.width + rect2.width > maxX - minX &&
  169. rect1.height + rect2.height > maxY - minY
  170. );
  171. } // Function rectIntersection()
  172. /**
  173. * 计算线段交点
  174. *
  175. * @param line1 线段1
  176. * @param line2 线段2
  177. * @return SPoint 交点 null 平行但不重合 'repeat' 重合
  178. */
  179. static lineIntersection(
  180. line1: SLine,
  181. line2: SLine
  182. ): SPoint | null | string {
  183. let k1 = line1.dy / line1.dx;
  184. let b1 = line1.y1 - k1 * line1.x1;
  185. let k2 = line2.dy / line2.dx;
  186. let b2 = line2.y1 - k2 * line2.x1;
  187. if (k1 == k2) {
  188. if (b1 == b2) {
  189. return "repeat";
  190. }
  191. return null;
  192. }
  193. let intersectionX = (b2 - b1) / (k1 - k2);
  194. let intersectionY = k1 * intersectionX + b1;
  195. let minX = Math.min(line1.x1, line1.x2);
  196. let maxX = Math.min(line2.x1, line2.x2);
  197. if (intersectionX >= minX && intersectionX <= maxX) {
  198. return new SPoint(intersectionX, intersectionY);
  199. }
  200. return null;
  201. } // Function lineIntersection()
  202. /**
  203. * 转化数据格式为[][]
  204. *
  205. * @param SP SPoint[]
  206. * @return Arr number[][]
  207. */
  208. static transferToArray(SP: SPoint[]): number[][] {
  209. return SP.map((t): number[] => {
  210. return [t.x, t.y];
  211. });
  212. } // Function transferToArray()
  213. /**
  214. * 转化数据格式为SPoint[]
  215. *
  216. * @param arr number[][]
  217. * @return SP SPoint[]
  218. */
  219. static transferToSPoint(arr: number[][]): SPoint[] {
  220. return arr.map(
  221. (t): SPoint => {
  222. return new SPoint(t[0], t[1]);
  223. }
  224. );
  225. } // Function transferToSPoint()
  226. /**
  227. * 计算数组中每一项的交集,并返回外轮廓与内轮廓
  228. *
  229. */
  230. static getIntersectInArray(array: number[][][]): Outline[] {
  231. let outlineList: Outline[] = [];
  232. if (!array.length) {
  233. // 无数据不做处理
  234. } else if (array.length == 1) {
  235. // 只有一条则为外轮廓
  236. let outline: Outline = {
  237. Outer: [],
  238. Inner: []
  239. };
  240. outline.Outer = array[0].map(
  241. (t): SPoint => {
  242. return new SPoint(t[0], t[1]);
  243. }
  244. );
  245. outlineList.push(outline);
  246. } else {
  247. // 多条的时候,最后一条未外轮廓,倒序遍历与之相交的为内轮廓,不相交为另外的外轮廓
  248. let poly2 = {
  249. regions: [],
  250. inverted: false
  251. };
  252. // @ts-ignore
  253. poly2.regions.push(array[array.length - 1]);
  254. let indexArr: number[] = [array.length - 1];
  255. for (let i = array.length - 2; i > 0; i--) {
  256. let poly1 = {
  257. regions: [],
  258. inverted: false
  259. };
  260. // @ts-ignore
  261. poly1.regions.push(array[i]);
  262. let intersectObj = intersect(poly1, poly2);
  263. console.log(intersectObj);
  264. if (!intersectObj.regions.length) {
  265. indexArr.unshift(i);
  266. poly2.regions = [];
  267. // @ts-ignore
  268. poly2.regions.push(array[i]);
  269. }
  270. }
  271. indexArr.unshift(0);
  272. console.log(indexArr);
  273. for (let i = 0; i < indexArr.length - 1; i++) {
  274. let axArr = array.slice(
  275. i == 0 ? 0 : indexArr[i] + 1,
  276. indexArr[i + 1]
  277. );
  278. let outline: Outline = {
  279. Outer: [],
  280. Inner: []
  281. };
  282. outline.Outer = array[indexArr[i + 1]].map(
  283. (t): SPoint => {
  284. return new SPoint(t[0], t[1]);
  285. }
  286. );
  287. outline.Inner = axArr.map((t): SPoint[] => {
  288. return t.map(
  289. (item): SPoint => {
  290. return new SPoint(item[0], item[1]);
  291. }
  292. );
  293. });
  294. outlineList.push(outline);
  295. }
  296. }
  297. console.log(outlineList);
  298. return outlineList;
  299. } // Function getIntersectInArray()
  300. /**
  301. * 计算轮廓线面积
  302. * */
  303. static calculateArea(arr: SPoint[]): number {
  304. let sum = 0;
  305. let n = arr.length;
  306. arr[n] = arr[0];
  307. for (let i = 1; i <= n; i++) {
  308. sum += arr[i].x * arr[i - 1].y - arr[i - 1].x * arr[i].y;
  309. }
  310. return sum / 2;
  311. } // Function calculateArea()
  312. } // Class SMathUtil