javascript實(shí)現(xiàn)畫(huà)不相交的圓
更新時(shí)間:2015年04月07日 10:08:10 投稿:hebedich
這篇文章主要介紹了javascript實(shí)現(xiàn)畫(huà)不相交的圓,這個(gè)也是阿里巴巴的筆試題,只不過(guò)忘記當(dāng)時(shí)是不是要求必須用canvas來(lái)實(shí)現(xiàn),下篇文章再寫(xiě)吧。
效果
html代碼
復(fù)制代碼 代碼如下:
<canvas id="my_canvas" width="500" height="400">
your browser does not support canvas
</canvas>
<button id="my_btn">Another Circle</button>
javascript代碼
復(fù)制代碼 代碼如下:
var context=document.getElementById("my_canvas");
context=context.getContext("2d");
var circles=[];
var width=500;
var height=400;
var max_radius=30;
var min_radius=20;
var count=0;
window.onload=function(){
var btn=document.getElementById("my_btn");
btn.onclick=function(){
var time=new Date();
start=time.getTime();
make_circle();
}
}
function Circle(x,y,r,color){
this.x=x;
this.y=y;
this.r=r;
this.color=color;
}
function make_circle(){
var x=Math.floor(Math.random()*width)+1;
var y=Math.floor(Math.random()*height)+1;
var r=Math.floor(Math.random()*(max_radius-min_radius))+min_radius;
var color="rgb("+(Math.floor(Math.random()*256))+","+(Math.floor(Math.random()*256))+","+(Math.floor(Math.random()*256))+")";//make different color
var circle=new Circle(x,y,r,color);
if(test1(circle)&&test2(circle)){
circles.push(circle);
context.strokeStyle=color;
context.beginPath();
context.arc(x,y,r,0,Math.PI*2,true);
context.closePath();
context.stroke();
count=0;
}
else{
count++;
if(count>10000){//if it loops too many times,we can assume that there is no space for new circle
alert("no more circle");
return false;
}
make_circle();
}
}
function test1(circle){//test if the new circle intersects with the others
var len=circles.length;
for(var i=0;i<len;i++){
var x1=circles[i].x;
var y1=circles[i].y;
var r1=circles[i].r;
var x2=circle.x;
var y2=circle.y;
var r2=circle.r;
if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<(r2+r1)*(r2+r1)){
return false;
}
}
return true;
}
function test2(circle){//test if the new circle touchs the border
if((circle.x+circle.r)>width||(circle.y+circle.r)>height||(circle.x-circle.r)<0||(circle.y-circle.r)<0){
return false;
}
else{
return true;
}
}
以上所述就是本文的全部?jī)?nèi)容了,希望能夠?qū)Υ蠹沂炀氄莆誮avascript有所幫助。
您可能感興趣的文章:
- Javascript 圓角div的實(shí)現(xiàn)代碼
- javascript動(dòng)畫(huà)之圓形運(yùn)動(dòng),環(huán)繞鼠標(biāo)運(yùn)動(dòng)作小球
- javascript橢圓旋轉(zhuǎn)相冊(cè)實(shí)現(xiàn)代碼
- JavaScript中圓括號(hào)()和方括號(hào)[]的特殊用法疑問(wèn)解答
- 原生javascript模仿win8等待提示圓圈進(jìn)度條
- javascript圓盤(pán)抽獎(jiǎng)程序?qū)崿F(xiàn)原理和完整代碼例子
- javascript結(jié)合Canvas 實(shí)現(xiàn)簡(jiǎn)易的圓形時(shí)鐘
- JavaScript中使用Math.PI圓周率屬性的方法
- javascript實(shí)現(xiàn)給定半徑求出圓的面積
相關(guān)文章
javascript性能優(yōu)化之事件委托實(shí)例詳解
這篇文章主要介紹了javascript性能優(yōu)化之事件委托用法,結(jié)合實(shí)例形式對(duì)比分析了JavaScript中事件委托的具體用法與優(yōu)點(diǎn),需要的朋友可以參考下2015-12-12微信小程序?qū)崿F(xiàn)文字從右向左無(wú)限滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)文字從右向左無(wú)限滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12JavaScript中的HTTP通信專(zhuān)家Axios用法探索
Axios是一個(gè)基于Promise的HTTP客戶(hù)端,專(zhuān)為瀏覽器和node.js設(shè)計(jì),本文主要為大家詳細(xì)介紹了Axios的具體使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01jquery動(dòng)態(tài)遍歷Json對(duì)象的屬性和值的方法
下面小編就為大家?guī)?lái)一篇jquery動(dòng)態(tài)遍歷Json對(duì)象的屬性和值的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07