2014-11-04 103 views
0

我想計算圓周的座標。 我無法得到它打印的座標,我不確定它是否計算它們。 我的代碼:用JQuery迭代和打印座標

HTML:

<p>Here is the coordinates: </p> 

JS:

#screen size will use screen.width and screen.height later 
var sW = 1920; 
var sH = 1080; 

#Two arrays with the coordinates stored in them 
var XcircleCoordinates; 
var YcircleCoordinates; 

#rows should be rows in database. I will use php to get the information, but assume 4 for now. 
var rows = 4; 

#radius and center of circle. 
var radius = 200; 
var center = [sW/2,sH/2]; 

function xCord(i){ 
    XcircleCoordinates[i] = radius*Math.cos((2.0*Math.PI*i)/rows)+center[0]; 
    return XcircleCoordinates[i]; 
} 

function yCord(i){ 
    YcircleCoordinates[i] = radius*Math.sin((2.0*Math.PI*i)/rows)+center[1]; 
    return YcircleCoordinates[i]; 
} 

for (var i = 0; i < 10; i++){ 
    $('p').prepend(xCord(i)); 
} 

回答

0

你的代碼有一些語法錯誤,下面應該工作。

//screen size will use screen.width and screen.height later 
 
var sW = 1920; 
 
var sH = 1080; 
 

 
//two arrays with the coordinates stored in them 
 
var XcircleCoordinates = new Array(); 
 
var YcircleCoordinates = new Array(); 
 

 
//rows should be rows in database. I will use php to get the information, but assume 4 for now. 
 
var rows = 4; 
 

 
//radius and center of circle. 
 
var radius = 200; 
 
var center = [sW/2,sH/2]; 
 

 
function xCord(i){ 
 
    XcircleCoordinates[i] = radius*Math.cos((2.0*Math.PI*i)/rows)+center[0]; 
 
    return XcircleCoordinates[i]; 
 
} 
 

 
function yCord(i){ 
 
    YcircleCoordinates[i] = radius*Math.sin((2.0*Math.PI*i)/rows)+center[1]; 
 
    return YcircleCoordinates[i]; 
 
} 
 

 
for (var i = 0; i < 10; i++){ 
 
    $('p').prepend(xCord(i)); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<p>Here is the coordinates: </p>

+0

它仍然不會寫任何東西。 對jQuery來說太複雜了嗎? 我是否需要將xCord(i)作爲字符串傳遞? – Artmole 2014-11-04 09:26:29

+0

我將其更改爲代碼段。這行得通。 – 2014-11-04 09:34:28