使用JQuery迭代和打印坐标

Iteration and printing of coordinates with JQuery

本文关键字:打印 坐标 迭代 JQuery 使用      更新时间:2023-09-26

我想计算圆的坐标。我无法让它打印坐标,我也不确定它是否计算出坐标。我的代码:

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

您的代码几乎没有语法错误,下面应该可以工作。

//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>