Ich habe im Moment ein bisschen Gedankenfreude ... Ich habe ein Problem, bei dem ich die Position von Punkten um einen zentralen Punkt berechnen muss, vorausgesetzt, sie sind alle gleich weit vom Zentrum und vom Zentrum entfernt gegenseitig.
Die Anzahl der Punkte ist variabel, also DrawCirclePoints(int x)
Ich bin sicher, dass es eine einfache Lösung gibt, aber für mein Leben sehe ich es einfach nicht :)
Ein Punkt im Winkel Theta auf dem Kreis, dessen Mittelpunkt (x0,y0)
und dessen Radius r
ist, ist (x0 + r cos theta, y0 + r sin theta)
. Wählen Sie nun theta
-Werte, die gleichmäßig zwischen 0 und 2pi liegen.
Bei einer Radiuslänge r und einem Winkel t im Bogenmaß und dem Mittelpunkt eines Kreises (h, k) können Sie die Koordinaten eines Punktes auf dem Umfang wie folgt berechnen (Pseudo) -code, müssen Sie ihn an Ihre Sprache anpassen):
float x = r*cos(t) + h;
float y = r*sin(t) + k;
Hier ist eine Lösung mit 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);
}
}
Beispielausgabe von 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}
Viel Glück!
Verwenden Sie eine der obigen Antworten als Basis, hier das Java/Android-Beispiel:
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
}
Ich musste dies im Web tun, also hier eine Kaffeescript-Version von @ - scottyab's answer:
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)
PHP-Lösung:
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;
}
Für die Vollendung ist das, was Sie als "Position von Punkten um einen zentralen Punkt beschreiben (vorausgesetzt, sie sind alle gleich weit vom Zentrum entfernt") "nichts anderes als" Polarkoordinaten ". Und Sie fragen nach einem Weg zum Konvertieren zwischen polaren und kartesischen Koordinaten der als x = r*cos(t)
, y = r*sin(t)
angegeben ist.
Hier ist eine R
-Version basierend auf der obigen @ Pirjan-Antwort.
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)
Arbeitslösung in 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;
}
}
Der Winkel zwischen jedem Punkt wird 2Pi/x
sein. Sie können also sagen, dass für Punkte n= 0 to x-1
der Winkel von einem definierten Nullpunkt 2nPi/x
ist.
Angenommen, Ihr erster Punkt ist bei (r,0)
(wobei r der Abstand vom Mittelpunkt ist), dann sind die Positionen relativ zum Mittelpunkt:
rCos(2nPi/x),rSin(2nPi/x)
Basierend auf der Antwort von Daniel, hier ist mein Take mit 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]))
Platzieren einer Zahl auf einer Kreisbahn
// 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);
}