我找到了this website,你尝试绘制一个完美的圆圈。为了好玩,我编写了这个小型Java应用程序,它从屏幕中间画一个圆圈:
// here is my massive array
private static int[] circle = {0, -100, 1, -100, 2, -100, 3, -100, 4, -100, 5, -100, 6, -100, 7, -100, 8, -100, 9, -100, 10, -100, 11, -99, 12, -99, 13, -99, 14, -99, 15, -99, 16, -99, 17, -98, 18, -98, 19, -98, 20, -98, 21, -98, 22, -97, 23, -97, 24, -97, 25, -97, 26, -96, 27, -96, 28, -96, 29, -96, 30, -95, 31, -95, 32, -95, 33, -94, 34, -94, 35, -94, 36, -93, 37, -93, 38, -92, 39, -92, 40, -92, 41, -91, 42, -91, 43, -90, 44, -90, 45, -89, 46, -89, 47, -88, 48, -88, 49, -87, 50, -86, 51, -86, 52, -85, 53, -85, 54, -84, 55, -83, 56, -84, 57, -83, 58, -82, 59, -82, 60, -81, 61, -80, 62, -79, 63, -79, 64, -78, 65, -77, 66, -76, 67, -75, 68, -74, 69, -73, 70, -72, 71, -71, 72, -70, 73, -69, 74, -68, 75, -67, 76, -66, 77, -65, 78, -64, 79, -63, 79, -62, 80, -61, 81, -60, 82, -59, 82, -58, 83, -57, 84, -56, 83, -55, 84, -54, 85, -53, 85, -52, 86, -51, 86, -50, 87, -49, 88, -48, 88, -47, 89, -46, 89, -45, 90, -44, 90, -43, 91, -42, 91, -41, 92, -40, 92, -39, 92, -38, 93, -37, 93, -36, 94, -35, 94, -34, 94, -33, 95, -32, 95, -31, 95, -30, 96, -29, 96, -28, 96, -27, 96, -26, 97, -25, 97, -24, 97, -23, 97, -22, 98, -21, 98, -20, 98, -19, 98, -18, 98, -17, 99, -16, 99, -15, 99, -14, 99, -13, 99, -12, 99, -11, 100, -10, 100, -9, 100, -8, 100, -7, 100, -6, 100, -5, 100, -4, 100, -3, 100, -2, 100, -1, 100, 0, 100, 1, 100, 2, 100, 3, 100, 4, 100, 5, 100, 6, 100, 7, 100, 8, 100, 9, 100, 10, 100, 11, 99, 12, 99, 13, 99, 14, 99, 15, 99, 16, 99, 17, 98, 18, 98, 19, 98, 20, 98, 21, 98, 22, 97, 23, 97, 24, 97, 25, 97, 26, 96, 27, 96, 28, 96, 29, 96, 30, 95, 31, 95, 32, 95, 33, 94, 34, 94, 35, 94, 36, 93, 37, 93, 38, 92, 39, 92, 40, 92, 41, 91, 42, 91, 43, 90, 44, 90, 45, 89, 46, 89, 47, 88, 48, 88, 49, 87, 50, 86, 51, 86, 52, 85, 53, 85, 54, 84, 55, 83, 56, 84, 57, 83, 58, 82, 59, 82, 60, 81, 61, 80, 62, 79, 63, 79, 64, 78, 65, 77, 66, 76, 67, 75, 68, 74, 69, 73, 70, 72, 71, 71, 72, 70, 73, 69, 74, 68, 75, 67, 76, 66, 77, 65, 78, 64, 79, 63, 79, 62, 80, 61, 81, 60, 82, 59, 82, 58, 83, 57, 84, 56, 83, 55, 84, 54, 85, 53, 85, 52, 86, 51, 86, 50, 87, 49, 88, 48, 88, 47, 89, 46, 89, 45, 90, 44, 90, 43, 91, 42, 91, 41, 92, 40, 92, 39, 92, 38, 93, 37, 93, 36, 94, 35, 94, 34, 94, 33, 95, 32, 95, 31, 95, 30, 96, 29, 96, 28, 96, 27, 96, 26, 97, 25, 97, 24, 97, 23, 97, 22, 98, 21, 98, 20, 98, 19, 98, 18, 98, 17, 99, 16, 99, 15, 99, 14, 99, 13, 99, 12, 99, 11, 100, 10, 100, 9, 100, 8, 100, 7, 100, 6, 100, 5, 100, 4, 100, 3, 100, 2, 100, 1, 100, 0,100,-1,100,-2,100,-3,100,-4,100,-5,100,-6,100,-7,100,-8,100,-9,100,-10,100,-11,99,-12,99,-13,99,-14,99,-15,99,-16,99,-17,98,-18,98,-19,98,-20,98,-21,98,-22,97,-23,97,-24,97,-25,97,-26,96,-27,96,-28,96,-29,96,-30,95,-31,95,-32,95,-33,94,-34,94,-35,94,-36,93,-37,93,-38,92,-39,92,-40,92,-41,91,-42,91,-43,90,-44,90,-45,89,-46,89,-47,88,-48,88,-49,87,-50,86,-51,86,-52,85,-53,85,-54,84,-55,83,-56,84,-57,83,-58,82,-59,82,-60,81,-61,80,-62,79,-63,79,-64,78,-65,77,-66,76,-67,75,-68,74,-69,73,-70,72,-71,71,-72,70,-73,69,-74,68,-75,67,-76,66,-77,65,-78,64,-79,63,-79,62,-80,61,-81,60,-82,59,-82,58,-83,57,-84,56,-83,55,-84,54,-85,53,-85,52,-86,51,-86,50,-87,49,-88,48,-88,47,-89,46,-89,45,-90,44,-90,43,-91,42,-91,41,-92,40,-92,39,-92,38,-93,37,-93,36,-94,35,-94,34,-94,33,-95,32,-95,31,-95,30,-96,29,-96,28,-96,27,-96,26,-97,25,-97,24,-97,23,-97,22,-98,21,-98,20,-98,19,-98,18,-98,17,-99,16,-99,15,-99,14,-99,13,-99,12,-99,11,-100,10,-100,9,-100,8,-100,7,-100,6,-100,5,-100,4,-100,3,-100,2,-100,1,-100,0,-100,-1,-100,-2,-100,-3,-100,-4,-100,-5,-100,-6,-100,-7,-100,-8,-100,-9,-100,-10,-100,-11,-99,-12,-99,-13,-99,-14,-99,-15,-99,-16,-99,-17,-98,-18,-98,-19,-98,-20,-98,-21,-98,-22,-97,-23,-97,-24,-97,-25,-97,-26,-96,-27,-96,-28,-96,-29,-96,-30,-95,-31,-95,-32,-95,-33,-94,-34,-94,-35,-94,-36,-93,-37,-93,-38,-92,-39,-92,-40,-92,-41,-91,-42,-91,-43,-90,-44,-90,-45,-89,-46,-89,-47,-88,-48,-88,-49,-87,-50,-86,-51,-86,-52,-85,-53,-85,-54,-84,-55,-83,-56,-84,-57,-83,-58,-82,-59,-82,-60,-81,-61,-80,-62,-79,-63,-79,-64,-78,-65,-77,-66,-76,-67,-75,-68,-74,-69,-73,-70,-72,-71,-71,-72,-70,-73,-69,-74,-68,-75,-67,-76,-66,-77,-65,-78,-64,-79,-63,-79,-62,-80,-61,-81,-60,-82,-59,-82,-58,-83,-57,-84,-56,-83,-55,-84,-54,-85,-53,-85,-52,-86,-51,-86,-50,-87,-49,-88,-48,-88,-47,-89,-46,-89,-45,-90,-44,-90,-43,-91,-42,-91,-41,-92,-40,-92,-39,-92,-38,-93,-37,-93,-36,-94,-35,-94,-34,-94,-33,-95,-32,-95,-31,-95,-30,-96,-29,-96,-28,-96,-27,-96,-26,-97,-25,-97,-24,-97,-23,-97,-22,-98,-21,-98,-20,-98,-19,-98,-18,-98,-17,-99,-16,-99,-15,-99,-14,-99,-13,-99,-12,-99,-11,-100,-10,-100,-9,-100,-8,-100,-7,-100,-6,-100,-5,-100,-4,-100,-3,-100,-2,-100,-1,-100,0};
public static void main(String[] args) {
try {
// Waiting for the website to load
Thread.sleep(3000);
} catch (InterruptedException e) {}
// Getting the center of the screen
Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
// Drawing the circle by clicking on each point in the array relative to the middle of the screen
for (int x = 0; x < circle.length; x+=2) {
// Calculating the x and y coordinates
int xx = p.x + circle[x];
int yy = p.y + circle[x+1];
click(xx, yy);
// waiting 10ms so the website can calculate the action
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
}
public static void click(int x, int y) {
try {
Robot bot = new Robot();
bot.mouseMove(x, y);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (Exception e) {}
}
该应用程序可在本网站上使用。但我的问题是,你可以看到,我必须保存一个数组中的圆的每个点,并且该数组非常大。如果我想扩展半径(100px),我需要重写所有这些点。有更简单的方法吗?也许通过使用等式或类似的东西。
答案 0 :(得分:1)
您可以使用Circle Equations绘制近乎完美的圆圈。由于您的屏幕分辨率,您无法绘制完美的圆圈。
您只需要x和y坐标。
public Point [] generateCoordinates(Point center, int diameter) {
//You will use algorithm here
}
这是一个示例算法;
https://www.tutorialspoint.com/computer_graphics/circle_generation_algorithm.htm
答案 1 :(得分:1)
您可以通过{cos(angle), sin(angle)}
计算给定角度(以弧度为单位)的圆周上的点的坐标。
因此,我们可以获得点列表的一种方法是循环并获取每个n
度的坐标,其中n
是可配置的(对于更大的半径,您需要更多的点)画一个光滑的圆圈)。
我首先声明一个Point类来保存这些值:
class Point
{
final int x;
final int y;
Point(final double x, final double y)
{
this.x = (int) x; // we're dealing with pixels, so just truncate it
this.y = (int) y;
}
@Override
public String toString()
{
return "{" + x + ", " + y + "}";
}
}
然后我们可以执行以下操作来填充Points数组:
final int NUM_POINTS = 1000;
final double RADIUS = 100d;
final Point[] points = new Point[NUM_POINTS];
for (int i = 0; i < NUM_POINTS; ++i)
{
final double angle = Math.toRadians(((double) i / NUM_POINTS) * 360d);
points[i] = new Point(
Math.cos(angle) * RADIUS,
Math.sin(angle) * RADIUS
);
}
System.out.println(Arrays.toString(points));
答案 2 :(得分:0)
https://www.varsitytutors.com/hotmath/hotmath_help/topics/equation-of-a-circle
你应该使用这个等式,这样你就可以调整圆的大小和精度
答案 3 :(得分:0)
我用这段代码生成我的数组,但速度很慢,这就是为什么@Michael的答案更好......
int r = Integer.parseInt(rad) / 2;
String[] circle = {"0", "0"};
String[] circleq = {"0", "0"};
String[] circlew = {"0", "0"};
String[] circlee = {"0", "0"};
for (int x = 0; x < r; x++) {
double yy;
yy = Math.sqrt((r*r) - (x*x));
int y = (int) Math.round(yy);;
circle = add(circle, Integer.parseInt("-" + x));
circle = add(circle, Integer.parseInt("-" + y));
}
String[] circle2 = circle;
for (int c = 0; c < circle2.length; c++) {
if ((c & 1) == 0 || c == 0) {
circleq = add(circleq, Integer.parseInt(circle2[c]));
} else {
circleq = add(circleq, Integer.parseInt(circle2[c].replace("-", "")));
}
}
for (int c = 0; c < circle2.length; c++) {
circlew = add(circlew, Integer.parseInt(circle2[c].replace("-", "")));
}
for (int c = 0; c < circle2.length; c++) {
if ((c & 1) == 0 || c == 0) {
circlee = add(circlee, Integer.parseInt(circle2[c].replace("-", "")));
} else {
circlee = add(circlee, Integer.parseInt(circle2[c]));
}
}
它将圆圈的每个四分之一保存在一个单独的数组中。