在圆上绘制点
Plotting Points on Circle
我在 java 中玩耍,并试图创建我自己的观点版本 class:
public class Location {
public double x, y;
public Location(double x, double y) {
this.x = x;
this.y = y;
}
public double dist(Location location) {
return Math.sqrt(Math.pow(location.x - x, 2) + Math.pow(location.y - y, 2));
}
//Rotates the point the amount in angle around the point center
public void rotate(Location center, double angle) {
//Also tried this
/*double current = Math.atan2(center.y - y, center.x - x);
x = center.x + (Math.cos(current + angle) * dist(center));
y = center.y + (Math.sin(current + angle) * dist(center));*/
//Current code
x = center.x + (Math.cos(angle) * dist(center));
y = center.y + (Math.sin(angle) * dist(center));
}
}
但是,无论我怎样尝试,rotate()
函数返回的数据都略有偏差。此函数输出的不是完美的圆,而是一个奇怪的缩小形状。
public class Circle {
//Should output circle
public static void main(String[] args) {
for (int i = 0; i < 18; i++) {
Location point = new Location(100, 100);
point.rotate(new Location(200, 200), Math.toRadians(i * 20));
System.out.print("(" + point.x + ", " + point.y + ")");
}
}
}
当我把这些坐标输出到this plotting site this is the image I get:
我的数学和Java: Plotting points uniformly on a circle using Graphics2d一样,所以我不知道发生了什么。
计算 dist(center)
一次,将其存储在一个变量中,然后使用该变量更新 x
和 y
:
double d = dist(center);
x = center.x + (Math.cos(angle) * d);
y = center.y + (Math.sin(angle) * d);
dist(center)
依赖于x
,所以在计算y
.
的新值时更新x
后会得到不同的值
我在 java 中玩耍,并试图创建我自己的观点版本 class:
public class Location {
public double x, y;
public Location(double x, double y) {
this.x = x;
this.y = y;
}
public double dist(Location location) {
return Math.sqrt(Math.pow(location.x - x, 2) + Math.pow(location.y - y, 2));
}
//Rotates the point the amount in angle around the point center
public void rotate(Location center, double angle) {
//Also tried this
/*double current = Math.atan2(center.y - y, center.x - x);
x = center.x + (Math.cos(current + angle) * dist(center));
y = center.y + (Math.sin(current + angle) * dist(center));*/
//Current code
x = center.x + (Math.cos(angle) * dist(center));
y = center.y + (Math.sin(angle) * dist(center));
}
}
但是,无论我怎样尝试,rotate()
函数返回的数据都略有偏差。此函数输出的不是完美的圆,而是一个奇怪的缩小形状。
public class Circle {
//Should output circle
public static void main(String[] args) {
for (int i = 0; i < 18; i++) {
Location point = new Location(100, 100);
point.rotate(new Location(200, 200), Math.toRadians(i * 20));
System.out.print("(" + point.x + ", " + point.y + ")");
}
}
}
当我把这些坐标输出到this plotting site this is the image I get:
我的数学和Java: Plotting points uniformly on a circle using Graphics2d一样,所以我不知道发生了什么。
计算 dist(center)
一次,将其存储在一个变量中,然后使用该变量更新 x
和 y
:
double d = dist(center);
x = center.x + (Math.cos(angle) * d);
y = center.y + (Math.sin(angle) * d);
dist(center)
依赖于x
,所以在计算y
.
x
后会得到不同的值