-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>");

}

数学绝对不是我的强项,只是想我会在这里发帖,看看是否有人可以帮助我指出正确的方向,如果我得到它,我会继续前进并更新???

我需要这种方式来代表站在大广场周围的小圆圈,但会有随机数量的人,他们都需要等距。

我昨天发布了关于圆形物体的同一篇文章,现在我在正方形上,我无法理解数学,任何帮助。

Sore that this has been voted down just thought i would update with a post putting all these together http://devsforrest.com/116/plot-positions-around-shapes-with-javascript Hope it helps someone else

4

1 回答 1

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

于 2012-11-30T15:52:42.720 回答