第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

關(guān)于星星切邊的問題,我想的方法太消耗資源了,而且只適用于星星數(shù)少的時候,請問有誰知道精簡的算法???

我的基本思路就是每生成一個星星就與之前的進行比較計算位置加上半徑,但是越到最后比較的就越多,所以如果星星數(shù)太多的話還會卡,請問誰有什么比較好的算法么?

哦,這里的代碼也寫了不讓星星出夜空范圍的代碼,這個比較簡單,算一下就好了,

還是求問星星之間切邊的算法~?

for(var i=0;i<20;i++){

? ? ? ? ? ? // random()返回一個0~1

? ? ? ? ? ? // 10~20的隨機數(shù)


? ? ? ? ? ? var r=Math.random()*10+10;

? ? ? ? ? ? var x=Math.random()*canvas.width;

? ? ? ? ? ? var y=Math.random()*canvas.height;

? ? ? ? ? ? var a=Math.random()*360;

? ? ? ? ? ? //防止星星出了星空的邊界

? ? ? ? ? ? if(x+a>canvas.width||y+a>canvas.height||x-a<0||y-a<0){

? ? ? ? ? ? i--;

? ? ? ? ? ? continue;

? ? ? ? ? ? }

? ? ? ? ? ? //防止星星重疊

? ? ? ? ? ? for(var j=0;j<stars.length;j++){

? ? ? ? ? ? console.log("j is "+j);

? ? ? ? ? ? var maxRadius=r>stars[j].radius?r:stars[j].radius;

? ? ? ? ? ? if(Math.abs(x-stars[j].x)<maxRadius||Math.abs(y-stars[j].y)<maxRadius){

? ? ? ? ? ? flag=false;

? ? ? ? ? ? i--;

? ? ? ? ? ? break;

? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? console.log("i is "+i);

? ? ? ? ? ? if(flag){

? ? ? ? ? ? var star={

? ? ? ? ? ? x:x,

? ? ? ? ? ? y:y,

? ? ? ? ? ? radius:r

? ? ? ? ? ? };

? ? ? ? ? ? stars.push(star);

? ? ? ? ? ? console.log("產(chǎn)生第"+(i+1)+"個星星");

? ? ? ? ? ? drawStar(context,x,y,r,r/2,a);

? ? ? ? ? ? }

? ? ? ? ? ? flag=true;


? ? ? ? ? ? }



正在回答

4 回答

var?position?=?[];
????window.onload?=function(){
????????var?canvas?=?document.getElementById('canvas');
????????var?context?=?canvas.getContext('2d');
????????context.fillStyle?=?"#000";
????????context.fillRect(0,0,canvas.width,canvas.height);
????????for(var?i=0;i<100;i++){
????????????var?r?=?Math.random()*10?+10;
????????????var?x1?=?(canvas.width-2*r)*Math.random()+r;?
????????????var?y1?=?(canvas.height-2*r)*Math.random()+r;
????????????var?a?=?Math.random()*360;
????????????for(var?j=0;j<position.length;j++){
????????????????while((Math.pow(position[j].x-x1,2)+Math.pow(position[j].y-y1,2)<Math.pow(position[j].r+r,2))){
????????????????????x1?=?(canvas.width-2*r)*Math.random()+r;?
????????????????????y1?=?(canvas.height-2*r)*Math.random()+r;
????????????????????j?=?0;
????????????????}????????????????
????????????}
????????????var?p?=?{x:x1,y:y1,r:r};
????????????position.push(p);
????????????drawStar(context,r/2,r,x1,y1,a);
????????}??
????}

我測試你的代碼的時候,i取30就動不了,這是我寫的代碼,至少i等于100還可以,可以參考參考

0 回復(fù) 有任何疑惑可以回復(fù)我~

maxRadius不應(yīng)該是兩個圓的半徑之和嗎?

0 回復(fù) 有任何疑惑可以回復(fù)我~

//防止星星出了星空的邊界 直接從隨機值開始下手是最好處理的

var option= {
? ?r : Math.random() * 10 + 10,
? ?rotate : Math.random() * 360 };
option.x = Math.random() * (canvas.width - (option.r * 2)) + option.r;
option.y = Math.random() * (canvas.height - (option.r * 2)) + option.r;

4 回復(fù) 有任何疑惑可以回復(fù)我~

可以默認(rèn)從中心向外擴散生成星星,這樣就不涉及到循環(huán)比較了

1 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

關(guān)于星星切邊的問題,我想的方法太消耗資源了,而且只適用于星星數(shù)少的時候,請問有誰知道精簡的算法???

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號