使用X,Y坐标绘制圆内的点 [英] Use X,Y coordinates to plot points inside a circle

查看:397
本文介绍了使用X,Y坐标绘制圆内的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javascript中是否有一种方法可以绘制x,y坐标,使它们分成圆形而不是正方形?

Is there a way in javascript to plot x,y coordinates so they fall into a circle rather than a square?

例如,如果我有以下代码:

For example if I have the following code:

  circleRadius = 100;
  context.drawImage(img_elem, dx, dy, dw, dh);

我需要找出落在100像素圆圈内的x,y值的组合。

I need to figure out a combination of x,y values that would fall inside a 100 pixel circle.

谢谢!

推荐答案


  1. 选择一个x在-100和100之间随机

  2. 一个圆由 x ^ 2 + y ^ 2 = r ^ 2 定义,在你的情况下等于100 ^ 2 = 10000

  3. 从这个等式你可以得到 y ^ 2 = 10000 - x ^ 2 因此,选择了x和 y = +/- sqrt(10000 - x ^ 2)的点将在圆圈上显示。

  4. 在第3点找到的两个坐标之间随意选择一个y

  5. 你已经设置了!

  1. choose an x at random between -100 and 100
  2. a circle is defined by x^2 + y^2 = r^2, which in your case equals 100^2 = 10000
  3. From this equation you can get that y^2 = 10000 - x^2 , therefore the points with a chosen x and y = +/-sqrt(10000 - x^2) will lye on the circle.
  4. choose an y at random between the two coordinates found at point 3
  5. You're set!

编辑:
在JS中:

In JS:

var radius = 100;
x = Math.random() * 2 * radius - radius;
ylim = Math.sqrt(radius * radius - x * x);
y = Math.random() * 2 * ylim - ylim;

另一个编辑: 一个jsFiddle示例

这篇关于使用X,Y坐标绘制圆内的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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