目前我有点脑子不太灵光。 我有一个问题,需要计算中心点周围的点的位置,假设它们与中心点和彼此之间都是等距的。
这个点的数量是可变的,所以是DrawCirclePoints(int x)
我相信有一个简单的解决方案,但我就是想不出来 :)
目前我有点脑子不太灵光。 我有一个问题,需要计算中心点周围的点的位置,假设它们与中心点和彼此之间都是等距的。
这个点的数量是可变的,所以是DrawCirclePoints(int x)
我相信有一个简单的解决方案,但我就是想不出来 :)
给定半径长度r、以弧度为单位的角度t和圆心坐标(h,k),您可以按照以下方式计算圆周上一点的坐标(这是伪代码,您需要将其调整为您所使用的语言):
float x = r*cos(t) + h;
float y = r*sin(t) + k;
一个圆的中心坐标为(x0,y0)
,半径为r
。在圆上选择一点,使其与圆心构成的线段与$x$轴的夹角为角度theta
,该点坐标为(x0 + r cos theta, y0 + r sin theta)
。现在在0到2pi之间等间距地选择角度theta
的值。
这里有一个使用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}
祝你好运!
将数字放置在一个圆形路径上
// 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);
}
使用上面的答案之一作为基础,这是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
}
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;
}
$newx
和$newy
的括号不正确,导致坐标远超圆形半径。尝试使用$newx = (int)($center->getX() + ($radius * cos($angle)));
以及类似的方式处理$newy
。 - Jasonx = r*cos(t)
,y = r*sin(t)
。以下是我的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
因为我需要在网络上完成这个任务,所以这里提供了@scottyab之前回答的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)
这是一个基于@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)