js實(shí)現(xiàn)固定區(qū)域內(nèi)的不重疊隨機(jī)圓
本文實(shí)例為大家分享了js實(shí)現(xiàn)固定區(qū)域內(nèi)的不重疊隨機(jī)圓,供大家參考,具體內(nèi)容如下
關(guān)鍵詞:js、固定區(qū)域、不重疊、隨機(jī)圓,半徑固定、半徑隨機(jī)
最近公司有一個(gè)需求就是在一個(gè)固定的區(qū)域(500X500)內(nèi)顯示10個(gè)圓,且半徑固定,而且不重疊
因?yàn)閳A的個(gè)數(shù)固定,而且半徑固定,那么就有可能會(huì)沒(méi)有解決方案。
不過(guò)其實(shí)也沒(méi)有很難,處理好半徑的最大值就好了。
效果圖:

思路:
(固定半徑)
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" style="border:1px solid #d3d3d3;">
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次半徑都沒(méi)有合適的話,終止進(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種方案都沒(méi)有合適可用的話,終止進(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ù)半徑從大到小畫(huà)圓。
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
原生js實(shí)現(xiàn)頁(yè)面滾動(dòng)動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)頁(yè)面滾動(dòng)動(dòng)畫(huà),使用了requestAnimationFrame,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感為興趣的小伙伴們可以參考一下2022-01-01
JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)跨年倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)跨年倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
js中substring和substr的詳細(xì)介紹與用法
這篇文章介紹了js中substring和substr的用法,有需要的朋友可以參考一下2013-08-08
BootStrap的alert提示框的關(guān)閉后再顯示怎么解決
這篇文章主要介紹了BootStrap的alert提示框的關(guān)閉后再顯示問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-05-05

