簡體   English   中英

用JavaScript指向一個正方形

[英]Point around a square with javascript

AtomicRobot在stackoverflow上給了我下面的代碼。

var x,y;

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

// issues with 3,5,7,8,9,10,11

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

    x -= Math.round(chairSize/2) - squareSize/2;
    y -= Math.round(chairSize/2) - squareSize/2;

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

這是使用javascript在桌子周圍繪制椅子,它對於以下數量的椅子1,2,4,6,12非常有效,距離的繪制是相等的。

但是,隨着以下椅子數量3、5、7、8、9、10、11的出現,似乎取決於椅子的寬度。

是他們的任何人都可以讓我知道為什么可能會發生這種數學不是我的強項,並且真的希望對此有所幫助,您可以在這里看到一個有8個椅子的示例。

http://jsfiddle.net/Ld769/4/

謝謝

我已經在徹底改變這一現狀方面取得了進展,現在我實際上正在了解這一過程,並且可以擴展表格以適合某些人的還款問題????

       var x,y;

// amount of chairs
var totalChairs = 18;
var chairSize = 60;
// square size
var squareSize = Math.round(totalChairs/4) * chairSize;

//alert("squareSize: " + squareSize);
// issues with 5,9,13,17,21
// works with 1,2,4,6,12
var remainder = Math.round(totalChairs/4);

var s1 = 0;
var s2 = 0;
var s3 = 0;
var s4 = 0;

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

    var iter = i;  
    if(iter+1 <= remainder){

         var s1i = s1++;
         console.log("s1i: " + s1i);

         x = s1i * chairSize;
         y = -chairSize;
         newr = remainder*2;

    } else if(iter+1 <= newr){
         var s2i = s2++;
         console.log("s2i: " + s2i);
         y = s2i * chairSize;
         x= squareSize;
         newe = remainder*3;

    } else if(iter+1 <= newe){

         var s3i = s3++;
         console.log("s3i: " + s3i);
         x =  - s3i * chairSize  + squareSize - chairSize;
         y = squareSize;

    }else{
         var s4i = s4++;
         console.log("s4i: " + s4i);
         y = - s4i * chairSize + squareSize - chairSize;
         x= -chairSize;

     }         

    $("#square").css({"width":(squareSize) + "px","height":(squareSize) + "px"});
    $("#square").append("<div class='chair' style='left:"+Math.round(x)+"px;top:"+Math.round(y)+"px;'><p>"+i+"<br/>x"+x+",y"+y+"</p></div>");
    $(".chair").css({"width":(chairSize) + "px","height":(chairSize) + "px"});
}

可以在這里查看jsfiddle版本http://jsfiddle.net/isimpledesign/Ld769/19/

我想弄清楚有多少需要適合4個方面之一。 4 * 2 = 8,不夠,4 * 3 = 12 OK !,這意味着3面變為3面,而1變為2。這意味着最小寬度和高度必須與最密集的面相同(以便就位舒適地)

是否要均勻分開? 只需弄清楚椅子的數量,比如說11張,將其分成360個就可以算出您的分離角度,然后將其投影到正方形上

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM