test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. function rotatePoint(point, deg, clockwise) {
  2. const [x, y] = point
  3. // 将角度转换为弧度,针旋转
  4. let rad = (deg / 360) * 2 * Math.PI
  5. if (clockwise) {
  6. rad = -rad
  7. }
  8. // 计算旋转后的坐标
  9. const xPrime = x * Math.cos(rad) - y * Math.sin(rad)
  10. const yPrime = x * Math.sin(rad) + y * Math.cos(rad)
  11. return [xPrime, yPrime]
  12. }
  13. function movePoint(point, dPoint) {
  14. const [x, y] = point
  15. const [dx, dy] = dPoint
  16. // 计算新的坐标
  17. const xPrime = x + dx
  18. const yPrime = y + dy
  19. return [xPrime, yPrime]
  20. }
  21. function rotatePointByOrigin(point, originPoint, deg, clockwise) {
  22. const [x, y] = point
  23. const [xOrigin, yOrigin] = originPoint
  24. const pPrime = movePoint(rotatePoint(movePoint([x, y], [-xOrigin, -yOrigin]), deg, clockwise), [xOrigin, yOrigin])
  25. return pPrime
  26. }
  27. function getBoxBorderPointsCanvasCoordinate(x, y, width, height, rotation, clockwise) {
  28. const tl = [x, y]
  29. const tr = movePoint(tl, [width, 0])
  30. const br = movePoint(tl, [width, -height]);
  31. const bl = movePoint(tl, [0, -height]);
  32. // 旋转矩形
  33. const tlPrime = rotatePointByOrigin(tl, tl, rotation, clockwise)
  34. const trPrime = rotatePointByOrigin(tr, tl, rotation, clockwise)
  35. const brPrime = rotatePointByOrigin(br, tl, rotation, clockwise)
  36. const blPrime = rotatePointByOrigin(bl, tl, rotation, clockwise)
  37. return [tlPrime, trPrime, brPrime, blPrime]
  38. }
  39. function getCenterOfBox(box) {
  40. const [topLeft, topRight, bottomRight, bottomLeft] = box
  41. const centerX = (topLeft[0] + bottomRight[0]) / 2
  42. const centerY = (topLeft[1] + bottomRight[1]) / 2
  43. return [centerX, centerY]
  44. }
  45. function rotateBox(box, deg, clockwise) {
  46. const center = getCenterOfBox(box)
  47. const [tl, tr, br, bl] = box
  48. const tlPrime = rotatePointByOrigin(tl, center, deg, clockwise)
  49. const trPrime = rotatePointByOrigin(tr, center, deg, clockwise)
  50. const brPrime = rotatePointByOrigin(br, center, deg, clockwise)
  51. const blPrime = rotatePointByOrigin(bl, center, deg, clockwise)
  52. return [tlPrime, trPrime, brPrime, blPrime]
  53. }
  54. function proportionToAbsolute(point, canvas) {
  55. const [x, y] = point
  56. const [canvasWidth, canvasHeight] = canvas
  57. return [x * canvasWidth / 100, y * canvasHeight / 100]
  58. }
  59. function absoluteToProportion(point, canvas) {
  60. const [x, y] = point
  61. const [canvasWidth, canvasHeight] = canvas
  62. return [x / canvasWidth * 100, y / canvasHeight * 100]
  63. }
  64. // const rotatedBox = rotateBox(box, -53.86516686148042, true)
  65. // console.log(rotatedBox)
  66. // const originBox = getBoxBorderPointsCanvasCoordinate(6.038732394366197, -18.489583333333336, 72.46478873239437, 22.656249999999993, 0 ,true)
  67. // console.log(originBox)
  68. const canvas = [710, 1029]
  69. function example(options) {
  70. const { x, y, rotation, clockwise } = options
  71. const pointAbsolute = proportionToAbsolute([x, y], canvas)
  72. const [width, height] = proportionToAbsolute([options.width, options.height], canvas)
  73. console.log('Absolute position:', pointAbsolute)
  74. console.log('Absolute size:', [width, height])
  75. const box = getBoxBorderPointsCanvasCoordinate(pointAbsolute[0], pointAbsolute[1], width, height, rotation, clockwise)
  76. console.log('Box border points:', box)
  77. console.log('Box border points in proportion:', box.map(p => absoluteToProportion(p, canvas)))
  78. const rotatedBox = rotateBox(box, -rotation, clockwise)
  79. console.log('Origin box:', rotatedBox)
  80. console.log('Origin box in proportion:', rotatedBox.map(p => absoluteToProportion(p, canvas)))
  81. }
  82. example({
  83. x: 34.4261492111812,
  84. y: -2.974398908479869,
  85. width: 71.89865757042229,
  86. height: 22.65625000000022,
  87. rotation: 53.86516686148042,
  88. clockwise: true,
  89. })