可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm having a bit of a mind blank on this at the moment. I've got a problem where I need to calculate the position of points around a central point, assuming they're all equidistant from the center and from each other.
The number of points is variable so it's DrawCirclePoints(int x) I'm sure there's a simple solution, but for the life of me, I just can't see it :)
回答1:
A point at angle theta on the circle whose centre is (x0,y0) and whose radius is r is (x0 + r cos theta, y0 + r sin theta). Now choose theta values evenly spaced between 0 and 2pi.
回答2:
Given a radius length r and an angle t in radians and a circle's center (h,k), you can calculate the coordinates of a point on the circumference as follows (this is pseudo-code, you'll have to adapt it to your language):
float x = r*cos(t) + h; float y = r*sin(t) + k;
回答3:
Here's a solution using C#:
void DrawCirclePoints(int points, double radius, Point center) { double slice = 2 * Math.PI / points; for (int i = 0; i
Sample output from 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}
Good luck!
回答4:
Using one of the above answers as a base, here's the Java/Android example:
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 }
回答5:
I had to do this on the web, so here's a coffeescript version of @scottyab's answer above:
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)
回答6:
PHP Solution:
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;$igetX() + $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; }
回答7:
For the sake of completion, what you describe as "position of points around a central point(assuming they're all equidistant from the center)" is nothing but "Polar Coordinates". And you are asking for way to Convert between polar and Cartesian coordinates which is given as x = rcos(t), y = rsin(t).
回答8:
Here is an R version based on the @Pirijan answer above.
points
回答9:
The angle between each of your points is going to be 2Pi/x so you can say that for points n= 0 to x-1 the angle from a defined 0 point is 2nPi/x.
Assuming your first point is at (r,0) (where r is the distance from the centre point) then the positions relative to the central point will be:
rCos(2nPi/x),rSin(2nPi/x)
回答10:
Working Solution 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; } }