问题
I found this website where you try drawing a perfect circle. Just for fun I coded this small Java application, that draws a circle from the middle of your screen:
// 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) {}
}
The application is made to be used on this website. But my problem is as you can see, that I have to save each point of the circle in an array and that array is very big. If I wanted to expand the radius (100px) I would need to rewrite all that points. Is there a easier way of doing so? Maybe by using an equation or something like that.
回答1:
You can calculate the coordinates of a point on the circumference of a circle for a given angle (in radians) by doing {cos(angle), sin(angle)}
.
So one way we can get a list of points is to loop around and get the coordinates for every n
degrees, where n
is configurable (you'll need more points for a larger radius to draw a smooth circle).
I'd first declare a Point class to hold these values:
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 + "}";
}
}
Then we can do the following to populate an array of 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:
You can use Circle Equations to draw a nearly perfect circle. You can not draw a perfect circle because of your screen resolution.
You only need x and y coordinates.
public Point [] generateCoordinates(Point center, int diameter) {
//You will use algorithm here
}
Here is an example algorithm;
https://www.tutorialspoint.com/computer_graphics/circle_generation_algorithm.htm
回答3:
https://www.varsitytutors.com/hotmath/hotmath_help/topics/equation-of-a-circle
you should use this equation, so you can adjust circle size and precision
回答4:
I used this code to generate my array but its very slow, thats why the answer by @Michael is better...
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]));
}
}
It saves each quarter of the circle in a seperate array.
来源:https://stackoverflow.com/questions/48382677/how-to-calculate-the-coordinates-of-points-in-a-circle-using-java