123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- import VectorType from '../enum/VectorType.js'
- import Geometry from './Geometry.js'
- import SelectState from '../enum/SelectState.js'
- import { mathUtil } from '../MathUtil.js'
- import Constant from '../Constant.js'
- export default class Circle extends Geometry {
- constructor(position,radius, vectorId, floor) {
- super()
- this.center = position
- this.radius = radius
- this.points = []; //顺时针
- this.setPoints()
- this.color = 'black';
- this.floor = floor?floor:0
- this.geoType = VectorType.Circle
- this.setId(vectorId)
- }
- setRadius(radius){
- this.radius = radius
- }
- setPoints(){
- this.points[0] = {
- x:this.center.x - this.radius,
- y:this.center.y + this.radius
- }
- this.points[1] = {
- x:this.center.x + this.radius,
- y:this.center.y + this.radius
- }
- this.points[2] = {
- x:this.center.x + this.radius,
- y:this.center.y - this.radius
- }
- this.points[3] = {
- x:this.center.x - this.radius,
- y:this.center.y - this.radius
- }
- }
- isContain(position){
- for(let i=0;i<this.points.length;++i){
- const dis = mathUtil.getDistance(position, this.points[i])
- if(dis < Constant.minAdsorb){
- return {
- vertex:i
- };
- }
- }
- // const dis = mathUtil.getDistance(position, this.center)
- // if(dis<this.radius){
- // return SelectState.All
- // }
- const flag = mathUtil.isPointInPoly(position,this.points)
- if(flag){
- return SelectState.All
- }
- return null;
- }
- updatePoints(newPosition,index){
- let nextIndex = this.getNextIndex(index)
- let oppositeIndex = this.getNextIndex(nextIndex)
- const line = mathUtil.createLine1(this.points[oppositeIndex],this.points[index])
- if(line == null){
- return
- }
- const join = mathUtil.getJoinLinePoint(newPosition, line)
- const radius = mathUtil.getDistance(join,this.points[oppositeIndex])/2/Math.sqrt(2)
- if(radius<Constant.minAdsorb){
- return;
- }
- mathUtil.clonePoint(this.points[index],join)
- this.center = {
- x:(this.points[index].x + this.points[oppositeIndex].x)/2,
- y:(this.points[index].y + this.points[oppositeIndex].y)/2
- }
- this.radius = radius
- if(index == 0){
- this.points[3] = {
- x:this.points[0].x,
- y:this.points[2].y
- }
- this.points[1] = {
- x:this.points[2].x,
- y:this.points[0].y
- }
- }
- else if(index == 1){
- this.points[0] = {
- x:this.points[3].x,
- y:this.points[1].y
- }
- this.points[2] = {
- x:this.points[1].x,
- y:this.points[3].y
- }
- }
- else if(index == 2){
- this.points[1] = {
- x:this.points[2].x,
- y:this.points[0].y
- }
- this.points[3] = {
- x:this.points[0].x,
- y:this.points[2].y
- }
- }
- else if(index == 3){
- this.points[2] = {
- x:this.points[1].x,
- y:this.points[3].y
- }
- this.points[0] = {
- x:this.points[3].x,
- y:this.points[1].y
- }
- }
- }
- getNextIndex(index){
- let nextIndex = index + 1;
- if(nextIndex == 4){
- nextIndex = 0;
- }
- return nextIndex;
- }
- getLastIndex(index){
- let lastIndex = index - 1;
- if(lastIndex < 0){
- lastIndex = 3
- }
- return lastIndex;
- }
- setColor(color) {
- this.color = color
- }
- }
|