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

最近公司有一個需求就是在一個固定的區(qū)域(500X500)內(nèi)顯示10個圓,且半徑固定,而且不重疊
因為圓的個數(shù)固定,而且半徑固定,那么就有可能會沒有解決方案。
不過其實也沒有很難,處理好半徑的大值就好了。
效果圖:

思路:
(固定半徑)
step1:先在區(qū)域內(nèi)生成一個隨機(jī)的圓心坐標(biāo),
step2:然后拿一個固定半徑(從大到小拿固定半徑)
step3:判斷圓心和半徑是否合法(是否超邊距,或者兩個圓相交)
step4:如果不合法,重新執(zhí)行step2和step3
step5:如果合法,記為一個新圓
step6:重復(fù)step1~5,直到生成10個圓
(隨機(jī)半徑)
step1:先在區(qū)域內(nèi)生成一個隨機(jī)的圓心坐標(biāo),
step2:根據(jù)圓心坐標(biāo),與其他圓比較,獲取最短的圓心距減去比較圓的半徑(圓心距-R n RnR_n)的值,作為新圓的半徑(這樣就會生成一個相切的圓)
step3:判斷圓心和半徑是否合法(是否超邊距)
step4:如果不合法,重新執(zhí)行step2和step3
step5:如果合法,記為一個新圓
step6:重復(fù)step1~5,直到生成10個圓
代碼:
// 參數(shù)
let obj = {
id: string, // canvas 的id
fix:boolean, // 是否固定半徑,默認(rèn)為false
minMargin: Number, // 兩個圓的最短距離,默認(rèn)為10
minRadius: Number, 最小的圓半徑,默認(rèn)為30
radiuArr: Array, 圓的半徑的數(shù)組,當(dāng)fix為true時該值必須填
total: Number ,圓的個數(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},${b})`
}
}
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)
}
// 獲取一個新圓的半徑,主要判斷半徑與最近的一個圓的距離
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
}
// 生成一個圓,隨機(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怎么實現(xiàn)固定區(qū)域內(nèi)的不重疊隨機(jī)圓”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
名稱欄目:js怎么實現(xiàn)固定區(qū)域內(nèi)的不重疊隨機(jī)圓-創(chuàng)新互聯(lián)
文章地址:http://www.chinadenli.net/article16/dpsdgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站策劃、用戶體驗、網(wǎng)站收錄、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容