此刻我对此有一点空白。 我有一个问题,我需要计算中心点周围点的位置,假设它们距离中心和彼此都是等距的。
点数是可变的,所以它是DrawCirclePoints(int x)
我确信这是一个简单的解决方案,但对于我的生活,我只是看不到它:)
答案 0 :(得分:77)
如果半径长度 r 且角度 t 以弧度为单位,圆圈的中心(h,k),则可以计算坐标圆周上的一个点如下(这是伪代码,你必须使它适应你的语言):
float x = r*cos(t) + h;
float y = r*sin(t) + k;
答案 1 :(得分:61)
中心为(x0,y0)
且半径为r
的圆上的角度θ为(x0 + r cos theta, y0 + r sin theta)
。现在选择theta
值均匀分布在0到2pi之间。
答案 2 :(得分:48)
这是使用C#的解决方案:
void DrawCirclePoints(int points, double radius, Point center)
{
double slice = 2 * Math.PI / points;
for (int i = 0; i < points; i++)
{
double angle = slice * i;
int newX = (int)(center.X + radius * Math.Cos(angle));
int newY = (int)(center.Y + radius * Math.Sin(angle));
Point p = new Point(newX, newY);
Console.WriteLine(p);
}
}
DrawCirclePoints(8, 10, new Point(0,0));
的示例输出:
{X=10,Y=0}
{X=7,Y=7}
{X=0,Y=10}
{X=-7,Y=7}
{X=-10,Y=0}
{X=-7,Y=-7}
{X=0,Y=-10}
{X=7,Y=-7}
祝你好运!
答案 3 :(得分:9)
使用上述答案之一作为基础,这里是Java / Android示例:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF bounds = new RectF(canvas.getClipBounds());
float centerX = bounds.centerX();
float centerY = bounds.centerY();
float angleDeg = 90f;
float radius = 20f
float xPos = radius * (float)Math.cos(Math.toRadians(angleDeg)) + centerX;
float yPos = radius * (float)Math.sin(Math.toRadians(angleDeg)) + centerY;
//draw my point at xPos/yPos
}
答案 4 :(得分:3)
我必须在网上这样做,所以这里是@ scottyab's上面的coffeescript版本答案:
points = 8
radius = 10
center = {x: 0, y: 0}
drawCirclePoints = (points, radius, center) ->
slice = 2 * Math.PI / points
for i in [0...points]
angle = slice * i
newX = center.x + radius * Math.cos(angle)
newY = center.y + radius * Math.sin(angle)
point = {x: newX, y: newY}
console.log point
drawCirclePoints(points, radius, center)
答案 5 :(得分:2)
PHP解决方案:
class point{
private $x = 0;
private $y = 0;
public function setX($xpos){
$this->x = $xpos;
}
public function setY($ypos){
$this->y = $ypos;
}
public function getX(){
return $this->x;
}
public function getY(){
return $this->y;
}
public function printX(){
echo $this->x;
}
public function printY(){
echo $this->y;
}
}
function drawCirclePoints($points, $radius, &$center){
$pointarray = array();
$slice = (2*pi())/$points;
for($i=0;$i<$points;$i++){
$angle = $slice*$i;
$newx = (int)(($center->getX() + $radius) * cos($angle));
$newy = (int)(($center->getY() + $radius) * sin($angle));
$point = new point();
$point->setX($newx);
$point->setY($newy);
array_push($pointarray,$point);
}
return $pointarray;
}
答案 6 :(得分:2)
为了完成,你所描述的“中心点周围点的位置(假设它们与中心距离都相等)”只不过是“极坐标”。而且您要求Convert between polar and Cartesian coordinates x = r*cos(t)
,y = r*sin(t)
,{{1}}。
答案 7 :(得分:1)
Java中的工作解决方案:
import java.awt.event.*;
import java.awt.Robot;
public class CircleMouse {
/* circle stuff */
final static int RADIUS = 100;
final static int XSTART = 500;
final static int YSTART = 500;
final static int DELAYMS = 1;
final static int ROUNDS = 5;
public static void main(String args[]) {
long startT = System.currentTimeMillis();
Robot bot = null;
try {
bot = new Robot();
} catch (Exception failed) {
System.err.println("Failed instantiating Robot: " + failed);
}
int mask = InputEvent.BUTTON1_DOWN_MASK;
int howMany = 360 * ROUNDS;
while (howMany > 0) {
int x = getX(howMany);
int y = getY(howMany);
bot.mouseMove(x, y);
bot.delay(DELAYMS);
System.out.println("x:" + x + " y:" + y);
howMany--;
}
long endT = System.currentTimeMillis();
System.out.println("Duration: " + (endT - startT));
}
/**
*
* @param angle
* in degree
* @return
*/
private static int getX(int angle) {
double radians = Math.toRadians(angle);
Double x = RADIUS * Math.cos(radians) + XSTART;
int result = x.intValue();
return result;
}
/**
*
* @param angle
* in degree
* @return
*/
private static int getY(int angle) {
double radians = Math.toRadians(angle);
Double y = RADIUS * Math.sin(radians) + YSTART;
int result = y.intValue();
return result;
}
}
答案 8 :(得分:1)
以下是基于上述@Pirijan答案的R
版本。
points <- 8
radius <- 10
center_x <- 5
center_y <- 5
drawCirclePoints <- function(points, radius, center_x, center_y) {
slice <- 2 * pi / points
angle <- slice * seq(0, points, by = 1)
newX <- center_x + radius * cos(angle)
newY <- center_y + radius * sin(angle)
plot(newX, newY)
}
drawCirclePoints(points, radius, center_x, center_y)
答案 9 :(得分:1)
这是我用JavaScript在圆上找到一个点,并计算出圆顶的角度(度)的方法。
const centreX = 50; // centre x of circle
const centreY = 50; // centre y of circle
const r = 20; // radius
const angleDeg = 45; // degree in angle from top
const radians = angleDeg * (Math.PI/180);
const pointY = centreY - (Math.cos(radians) * r); // specific point y on the circle for the angle
const pointX = centreX + (Math.sin(radians) * r); // specific point x on the circle for the angle
答案 10 :(得分:0)
每个点之间的角度将为2Pi/x
,因此您可以说,对于点n= 0 to x-1
,定义的0点的角度为2nPi/x
。
假设您的第一个点位于(r,0)
(其中r是距中心点的距离),那么相对于中心点的位置将是:
rCos(2nPi/x),rSin(2nPi/x)
答案 11 :(得分:0)
基于上面Daniel的回答,这是我使用Python3的看法。
import numpy
shape = []
def circlepoints(points,radius,center):
slice = 2 * 3.14 / points
for i in range(points):
angle = slice * i
new_x = center[0] + radius*numpy.cos(angle)
new_y = center[1] + radius*numpy.sin(angle)
p = (new_x,new_y)
shape.append(p)
return shape
print(circlepoints(100,20,[0,0]))
答案 12 :(得分:0)
在循环路径中放置数字
// variable
let number = 12; // how many number to be placed
let size = 260; // size of circle i.e. w = h = 260
let cx= size/2; // center of x(in a circle)
let cy = size/2; // center of y(in a circle)
let r = size/2; // radius of a circle
for(let i=1; i<=number; i++) {
let ang = i*(Math.PI/(number/2));
let left = cx + (r*Math.cos(ang));
let top = cy + (r*Math.sin(ang));
console.log("top: ", top, ", left: ", left);
}