圆坐标阵列在Javascript [英] Circle coordinates to array in Javascript

查看:162
本文介绍了圆坐标阵列在Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是圆的坐标添加到JavaScript的一个数组的最佳方式?到目前为止,我只能够做一个半圈,但我需要一个返回圆整到两个不同的数组公式:xValues​​和yValues​​。 (我试图获取坐标,所以我可以沿着路径动画的对象。)

下面是我到目前为止有:

 圈:函数(半径,步骤,的centerX,centerY){
    VAR xValues​​ = [的centerX];
    VAR yValues​​ = [centerY];
    对于(VAR I = 1; I<步骤;我++){
    xValues​​ [I] =(的centerX +半径* Math.cos(Math.PI *的i /步骤-Math.PI / 2));
    yValues​​ [I] =(centerY +半径* Math.sin(Math.PI *的i /步骤-Math.PI / 2));
   }
}


解决方案

您回路应设置这样来代替:

 为(VAR I = 0; I<步骤;我++){
    xValues​​ [I] =(+的centerX半径* Math.cos(2 * Math.PI * I /步));
    yValues​​ [I] =(centerY +半径* Math.sin(2 * Math.PI * I /步));
}


  • 在0开始你的循环

  • 在整个2 * PI范围步,不只是PI。

  • 您不应该在 VAR xValues​​ = [的centerX]; VAR yValues​​ = [centerY]; - 圆的中心不是它的一部分

What's the best way to add the coordinates of a circle to an array in JavaScript? So far I've only been able to do a half circle, but I need a formula that returns the whole circle to two different arrays: xValues and yValues. (I'm trying to get the coordinates so I can animate an object along a path.)

Here's what I have so far:

circle: function(radius, steps, centerX, centerY){
    var xValues = [centerX];
    var yValues = [centerY];
    for (var i = 1; i < steps; i++) {
    	xValues[i] = (centerX + radius * Math.cos(Math.PI * i / steps-Math.PI/2));
    	yValues[i] = (centerY + radius * Math.sin(Math.PI * i / steps-Math.PI/2));
   }
}

解决方案

Your loop should be set up like this instead:

for (var i = 0; i < steps; i++) {
    xValues[i] = (centerX + radius * Math.cos(2 * Math.PI * i / steps));
    yValues[i] = (centerY + radius * Math.sin(2 * Math.PI * i / steps));
}

  • Start your loop at 0
  • Step through the entire 2 * PI range, not just PI.
  • You shouldn't have the var xValues = [centerX]; var yValues = [centerY]; -- the center of the circle is not a part of it.

这篇关于圆坐标阵列在Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆