js怎么實(shí)現(xiàn)固定區(qū)域內(nèi)的不重疊隨機(jī)圓

這篇文章給大家分享的是有關(guān)js怎么實(shí)現(xiàn)固定區(qū)域內(nèi)的不重疊隨機(jī)圓的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)是專業(yè)的靈川網(wǎng)站建設(shè)公司,靈川接單;提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行靈川網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

最近公司有一個(gè)需求就是在一個(gè)固定的區(qū)域(500X500)內(nèi)顯示10個(gè)圓,且半徑固定,而且不重疊

因?yàn)閳A的個(gè)數(shù)固定,而且半徑固定,那么就有可能會(huì)沒有解決方案。

不過其實(shí)也沒有很難,處理好半徑的最大值就好了。

效果圖:

js怎么實(shí)現(xiàn)固定區(qū)域內(nèi)的不重疊隨機(jī)圓

思路:

(固定半徑)

step1:先在區(qū)域內(nèi)生成一個(gè)隨機(jī)的圓心坐標(biāo),
step2:然后拿一個(gè)固定半徑(從大到小拿固定半徑)
step3:判斷圓心和半徑是否合法(是否超邊距,或者兩個(gè)圓相交)
step4:如果不合法,重新執(zhí)行step2和step3
step5:如果合法,記為一個(gè)新圓
step6:重復(fù)step1~5,直到生成10個(gè)圓

(隨機(jī)半徑)

step1:先在區(qū)域內(nèi)生成一個(gè)隨機(jī)的圓心坐標(biāo),
step2:根據(jù)圓心坐標(biāo),與其他圓比較,獲取最短的圓心距減去比較圓的半徑(圓心距-R n  RnR_n)的值,作為新圓的半徑(這樣就會(huì)生成一個(gè)相切的圓)
step3:判斷圓心和半徑是否合法(是否超邊距)
step4:如果不合法,重新執(zhí)行step2和step3
step5:如果合法,記為一個(gè)新圓
step6:重復(fù)step1~5,直到生成10個(gè)圓

代碼:

// 參數(shù)
let obj = {
  id: string, // canvas 的id
  fix:boolean, // 是否固定半徑,默認(rèn)為false
  minMargin: Number, // 兩個(gè)圓的最短距離,默認(rèn)為10
  minRadius: Number, 最小的圓半徑,默認(rèn)為30
  radiuArr: Array, 圓的半徑的數(shù)組,當(dāng)fix為true時(shí)該值必須填
  total: Number ,圓的個(gè)數(shù),默認(rèn)為10
}
<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="500" height="500" >
Your browser does not support the HTML5 canvas tag.
</canvas>

<script>
class Circle {
  constructor(x, y, r, color){
    this.x = x
    this.y = y
    this.r = r,
    this.c = color ? color : this.getRandomColor()
  }
  getRandomColor(){ 
    let r = Math.floor(Math.random()*100) + 155
    let g = Math.floor(Math.random()*100) + 155
    let b = Math.floor(Math.random()*100) + 155
    return `rgb(${r},${g},$)` 
  } 
}

class RandomCircle {

  constructor(obj) {
    this.c      = document.getElementById(obj.id);
    this.ctx     = this.c.getContext("2d");
    this.dWidth   = this.c.width;
    this.dHeight   = this.c.height

    this.fix     = obj.fix || false;
    this.minMargin  = obj.minMargin || 10 
    this.minRadius  = obj.minRadius || 30
    this.radiuArr  = obj.radiuArr || [80,70,60,50,45,40,40,35,35,30]
    this.total = obj.total || 10
    this.circleArray = []
    this.circleNumber = 1
  }


  drawOneCircle(c) {
    let ctx = this.ctx;
    ctx.beginPath();
    ctx.strokeStyle = c.c;
    ctx.fillStyle=c.c;
    ctx.arc(c.x, c.y, c.r, 0, 2*Math.PI);
    ctx.stroke();
    ctx.fill();

    ctx.fillStyle='black';
    ctx.fillText('No:'+this.circleNumber, c.x-10, c.y-5);
    ctx.fillText('R:'+c.r, c.x-10, c.y+5);
    this.circleNumber ++ 
  }

  check(x,y,r) {
    return !(x+r > this.dWidth || x-r < 0 || y + r > this.dHeight || y-r < 0)
  }

  // 獲取一個(gè)新圓的半徑,主要判斷半徑與最近的一個(gè)圓的距離
  getR(x,y) {
    if (this.circleArray.length === 0) return Math.floor(Math.random()*20 + 80)
    let lenArr = this.circleArray.map(c => {
      let xSpan = c.x-x
      let ySpan = c.y-y
      return Math.floor(Math.sqrt(Math.pow(xSpan,2) + Math.pow(ySpan,2))) - c.r
    })
    let minCircleLen = Math.min(...lenArr)
    let minC = this.circleArray[lenArr.indexOf(minCircleLen)]
    let tempR = this.fix ? this.radiuArr[this.circleArray.length] : minCircleLen - this.minMargin
    let bool = this.fix ? (tempR <= minCircleLen - minC.r) : (tempR >= this.minRadius)
    return bool ? tempR : false
  }

  // 生成一個(gè)圓,隨機(jī)生成圓心。
  // 如果連續(xù)生成200次半徑都沒有合適的話,終止進(jìn)程
  createOneCircle(){
    let x,y,r;
    let createCircleTimes = 0
    while(true) {
      createCircleTimes ++ 
      x = Math.floor(Math.random()*this.dWidth)
      y = Math.floor(Math.random()*this.dHeight)
      let TR = this.getR(x,y)
      if (!TR) {
        continue;
      } else {
        r = TR
      }
      if (this.check(x,y,r) || createCircleTimes > 200) {
        break
      }

    }
    this.check(x,y,r) && this.circleArray.push(new Circle(x, y, r))

  }

  // 如果生成100次新圓都失敗的話,終止方案。
  // 如果生成100種方案都沒有合適可用的話,終止進(jìn)程。
  init() {
    let n = 0
    while(this.circleArray.length < this.total) {
      this.circleArray = []
      let i = 0;
      while (this.circleArray.length < this.total) {
        this.createOneCircle()
        i ++ 
        if (i >= 100) {
          break;
        }
      }
      n ++ 
      if (n > 100) {
        break;
      }
    }
    // 根據(jù)半徑從大到小畫圓。
    this.circleArray.sort( (a,b) => b.r-a.r).forEach(c => {
      this.drawOneCircle(c)
    })
  }
}


let p = new RandomCircle({id: 'myCanvas', total: 20})
p.init()

console.log(p.circleArray)

</script> 
</body>
</html>

感謝各位的閱讀!關(guān)于“js怎么實(shí)現(xiàn)固定區(qū)域內(nèi)的不重疊隨機(jī)圓”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

名稱欄目:js怎么實(shí)現(xiàn)固定區(qū)域內(nèi)的不重疊隨機(jī)圓
當(dāng)前網(wǎng)址:http://muchs.cn/article8/isjdop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站策劃、搜索引擎優(yōu)化、自適應(yīng)網(wǎng)站、ChatGPT、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)