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ù)半徑從大到小畫圓。
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)遮罩以及彈出可移動(dòng)登錄窗口
本篇文章是對(duì)使用js實(shí)現(xiàn)遮罩以及彈出可移動(dòng)登錄窗口的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
JS桶排序的簡(jiǎn)單理解與實(shí)現(xiàn)方法示例
這篇文章主要介紹了JS桶排序的簡(jiǎn)單理解與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了js桶排序的概念、原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2019-11-11
如何使用Bootstrap的modal組件自定義alert,confirm和modal對(duì)話框
本文我將為大家介紹Bootstrap中的彈出窗口組件Modal,此組件簡(jiǎn)單易用,效果大氣漂亮且很實(shí)用,感興趣的朋友一起學(xué)習(xí)吧2016-03-03
用javascript實(shí)現(xiàn)截取字符串包含中文處理的函數(shù)
一直不知道js可以截取中文字符,呵呵,原理用正則表達(dá)式,匹配中文的長(zhǎng)度,中文算兩個(gè),因?yàn)樗阋粋€(gè),是個(gè)好東西,推薦大家收藏2008-04-04
基于JS實(shí)現(xiàn)新聞列表無(wú)縫向上滾動(dòng)實(shí)例代碼
當(dāng)新聞?shì)^多,并且空前有限的時(shí)候,使用滾動(dòng)是一個(gè)不錯(cuò)的選擇,本章節(jié)就通過(guò)代碼實(shí)例介紹一下如何實(shí)現(xiàn)此效果,對(duì)無(wú)縫向上滾動(dòng)實(shí)例代碼感興趣的朋友一起學(xué)習(xí)吧2016-01-01
js函數(shù)setTimeout延遲執(zhí)行的簡(jiǎn)單介紹
設(shè)置指定的JS函數(shù)在指定的時(shí)間后執(zhí)行,可以利用setTimeout()函數(shù)。2013-07-07

