2012-11-30 37 views
-1

我想以方格的形式繪製座標,在這裏它是硬編碼來顯示我之後的東西。我想繪製一個正方形的座標

http://jsfiddle.net/zwkny/

// amount of chairs 
var aoc = 4; 
// table width 
var tw = 200; 
// table height 
var th = 200; 

// chair width height 
var cwh = 60/2; 

var space = tw * 4/aoc; 

var left = [-30,100,-30,-160]; 
var top = [-160,-30,100,-30]; 

// straing point 
var sp = 12; 

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


    var x = cwh + space * i/aoc; 
    console.log(x); 
    //var y = space/2 - cwh * i; 

    $("#center").append("<div class='chair' style='left:"+left[i]+"px;top:"+top[i]+"px;'>"+i+"</div>"); 

} 

數學是絕對不是我的強項只是想我要張貼在這裏看到,如果有人能幫助我指出正確的方向我一直走下去,更新,如果我得到它???

我需要這種方式來代表人們站在大廣場周圍的小圈子,但會有隨機數量的人,他們都需要在相等的距離。

我昨天發佈了關於一個圓對象的相同的帖子,現在我在廣場上,我只是無法讓我的頭在數學,任何幫助。

瘡,這已被否決只是想我會後把所有這些一起更新 http://devsforrest.com/116/plot-positions-around-shapes-with-javascript 希望它可以幫助別人

回答

1
var x,y; 

// amount of chairs 
var totalChairs = 12; 
// square size 
var squareSize = 200; 
var chairSize = 20; 

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

var angle = 2*Math.PI * i/totalChairs; 

if (angle > Math.PI/4 && angle <= Math.PI* 3/4){ 
    x = (squareSize/2)/Math.tan(angle); 
    y = -squareSize/2; 
} else if (angle > Math.PI* 3/4 && angle <= Math.PI* 5/4){ 
    x = -squareSize/2; 
    y = (squareSize/2) * Math.tan(angle); 
} else if (angle > Math.PI* 5/4 && angle <= Math.PI* 7/4){ 
    x = -(squareSize/2)/Math.tan(angle); 
    y = -squareSize/2 + squareSize; 
} else { 
    x = -squareSize/2 + squareSize; 
    y = -(squareSize/2) * Math.tan(angle); 
} 

x -= chairSize/2; 
y -= chairSize/2; 

$("#center").append("<div class='chair' style='left:"+x+"px;top:"+y+"px;'></div>"); 
} 

Demo

+0

哇感謝這個AtomicRobot!我想到我將在週末修改數學;) – user1503606

相關問題