Circle.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import VectorType from '../enum/VectorType.js'
  2. import Geometry from './Geometry.js'
  3. import SelectState from '../enum/SelectState.js'
  4. import { mathUtil } from '../MathUtil.js'
  5. import Constant from '../Constant.js'
  6. export default class Circle extends Geometry {
  7. constructor(position,radius, vectorId, floor) {
  8. super()
  9. this.center = position
  10. this.radius = radius
  11. this.points = []; //顺时针
  12. this.setPoints()
  13. this.color = 'black';
  14. this.floor = floor?floor:0
  15. this.geoType = VectorType.Circle
  16. this.setId(vectorId)
  17. }
  18. setRadius(radius){
  19. this.radius = radius
  20. }
  21. setPoints(){
  22. this.points[0] = {
  23. x:this.center.x - this.radius,
  24. y:this.center.y + this.radius
  25. }
  26. this.points[1] = {
  27. x:this.center.x + this.radius,
  28. y:this.center.y + this.radius
  29. }
  30. this.points[2] = {
  31. x:this.center.x + this.radius,
  32. y:this.center.y - this.radius
  33. }
  34. this.points[3] = {
  35. x:this.center.x - this.radius,
  36. y:this.center.y - this.radius
  37. }
  38. }
  39. isContain(position){
  40. for(let i=0;i<this.points.length;++i){
  41. const dis = mathUtil.getDistance(position, this.points[i])
  42. if(dis < Constant.minAdsorb){
  43. return {
  44. vertex:i
  45. };
  46. }
  47. }
  48. // const dis = mathUtil.getDistance(position, this.center)
  49. // if(dis<this.radius){
  50. // return SelectState.All
  51. // }
  52. const flag = mathUtil.isPointInPoly(position,this.points)
  53. if(flag){
  54. return SelectState.All
  55. }
  56. return null;
  57. }
  58. updatePoints(newPosition,index){
  59. let nextIndex = this.getNextIndex(index)
  60. let oppositeIndex = this.getNextIndex(nextIndex)
  61. const line = mathUtil.createLine1(this.points[oppositeIndex],this.points[index])
  62. if(line == null){
  63. return
  64. }
  65. const join = mathUtil.getJoinLinePoint(newPosition, line)
  66. const radius = mathUtil.getDistance(join,this.points[oppositeIndex])/2/Math.sqrt(2)
  67. if(radius<Constant.minAdsorb){
  68. return;
  69. }
  70. mathUtil.clonePoint(this.points[index],join)
  71. this.center = {
  72. x:(this.points[index].x + this.points[oppositeIndex].x)/2,
  73. y:(this.points[index].y + this.points[oppositeIndex].y)/2
  74. }
  75. this.radius = radius
  76. if(index == 0){
  77. this.points[3] = {
  78. x:this.points[0].x,
  79. y:this.points[2].y
  80. }
  81. this.points[1] = {
  82. x:this.points[2].x,
  83. y:this.points[0].y
  84. }
  85. }
  86. else if(index == 1){
  87. this.points[0] = {
  88. x:this.points[3].x,
  89. y:this.points[1].y
  90. }
  91. this.points[2] = {
  92. x:this.points[1].x,
  93. y:this.points[3].y
  94. }
  95. }
  96. else if(index == 2){
  97. this.points[1] = {
  98. x:this.points[2].x,
  99. y:this.points[0].y
  100. }
  101. this.points[3] = {
  102. x:this.points[0].x,
  103. y:this.points[2].y
  104. }
  105. }
  106. else if(index == 3){
  107. this.points[2] = {
  108. x:this.points[1].x,
  109. y:this.points[3].y
  110. }
  111. this.points[0] = {
  112. x:this.points[3].x,
  113. y:this.points[1].y
  114. }
  115. }
  116. }
  117. getNextIndex(index){
  118. let nextIndex = index + 1;
  119. if(nextIndex == 4){
  120. nextIndex = 0;
  121. }
  122. return nextIndex;
  123. }
  124. getLastIndex(index){
  125. let lastIndex = index - 1;
  126. if(lastIndex < 0){
  127. lastIndex = 3
  128. }
  129. return lastIndex;
  130. }
  131. setColor(color) {
  132. this.color = color
  133. }
  134. }