用javascript指向一个正方形

用javascript指向一个正方形,javascript,math,plot,Javascript,Math,Plot,stackoverflow上的Atomibot向我提供了下面的代码 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/totalC

stackoverflow上的Atomibot向我提供了下面的代码

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>");
}
var x,y;
//椅子数量
var=12;
//正方形
var squareSize=200;
风险价值大小=60;
//与3,5,7,8,9,10,11的问题

对于(var i=0;i Math.PI/4&&angle Math.PI*3/4&&angle Math.PI*5/4&&angle我会计算出需要在四个侧面中的一个侧面安装多少个。4*2=8,不够,4*3=12好!这意味着三个侧面得到三个,一个侧面得到两个。这意味着最小宽度和高度必须与最拥挤的一侧相同(以便坐着舒适)


如果你想让它们均匀地分开?只要计算出椅子的数量,比如说是11,分成360,计算出你的分开角度,然后将其投影到正方形上

你要求桌子和椅子不重叠吗?为什么桌子必须是正方形的?嗨,拉斐尔,桌子必须是正方形的,因为我们正在为variet做这项工作y的形状像圆形,但正方形很棘手是的,要求桌子和椅子不要重叠谢谢;)是的,圆形桌子是正确的选择。如果你想用平方的,你必须告诉更多关于位置的信息。例如,如果椅子必须接触桌子,或者如果两个连续椅子中心之间的距离必须为constant@user1503606:如果你有11把椅子,你会怎样把它们放在一张桌子上?嗨,Amberlamps,是的,这是我发现的11 9等的问题,我认为它们应该是相等的距离,就像它们的12把椅子一样,但少了一把。Raffaele椅子必须刚好接触桌子的外缘,并且一侧的距离应该相等,正如Amberlamps指出的,这对于11 9 ETC来说是很棘手的。目前的代码似乎是按照您建议的均匀划分圆,但投影是错误的。分成几方似乎是一种更有用的追求。
       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"});
}