请注意,我将使用的大多数数字只是示例,因为它需要动态
我正在尝试使用UTF Full Block图标(=█)创建一个具有动态高度,宽度和圆扇形的圆(然后将它们用于Minecraft全息图中,该图居中,因此没有一堆使其居中的空间)。
例如,假设我有一个25x25(直径为25)的圆,其中有12个(几乎相等)的扇区,这些扇区中的每个扇区都会获得不同的颜色,圆的中心是(0,0)。 如何将这个圆“划分”为多个扇区并将一个扇区ID(例如0-11)放在坐标上?
在下面看到我当前使用的代码之后,getSectionNumber方法根本不起作用,并且可能会被忽略:
public class HologramCircle {
private final int height;
private final int width;
public HologramCircle(final int height, final int width) {
this.height = height;
this.width = width;
}
public List<String> getLines() {
final float width_r = (float) width / 2;
final float height_r = (float) height / 2;
final float ratio = width_r / height_r;
final double maxblocks_x;
final double maxblocks_y;
if ((width_r * 2) % 2 == 0) {
maxblocks_x = Math.ceil(width_r - .5) * 2 + 1;
} else {
maxblocks_x = Math.ceil(width_r) * 2;
}
if ((height_r * 2) % 2 == 0) {
maxblocks_y = Math.ceil(height_r - .5) * 2 + 1;
} else {
maxblocks_y = Math.ceil(height_r) * 2;
}
final List<String> lines = Lists.newArrayList();
for (double y = -maxblocks_y / 2 + 1; y <= maxblocks_y / 2 - 1; y++) {
final StringBuilder line = new StringBuilder();
for (double x = -maxblocks_x / 2 + 1; x <= maxblocks_x / 2 - 1; x++) {
if (shouldBeFilled(x, y, width_r, ratio)) {
System.out.println(
String.format("Section id for x: %s and y: %s is %s", x, y, getSectorNumber(x, y, width_r, 12))
);
line.append("█");
}
}
lines.add(line.toString());
}
return lines;
}
private int getSectorNumber(final double x, final double y, final double radius, final int sectorAmount) {
final double degreesPerSector = 360 / sectorAmount;
final double rad = Math.atan2(y, x);
final double degrees = rad * (180 / Math.PI);
System.out.println("rad: " + rad + ", degrees: " + degrees);
for (int i = 0; i < sectorAmount; i++) {
final double startDegrees = degreesPerSector * i;
final double endDegrees = startDegrees + degreesPerSector;
if (degrees >= startDegrees && degrees <= endDegrees) {
return i;
}
}
return -1;
}
private double distance(final double x, final double y, final double ratio) {
return Math.sqrt((Math.pow(y * ratio, 2)) + Math.pow(x, 2));
}
private boolean shouldBeFilled(final double x, final double y, final double radius, final float ratio) {
return distance(x, y, ratio) <= radius;
}
我的解决方案:
我在getSectionNumber中拥有的代码已经朝着正确的方向发展,我最终只增加了180°,所以它的范围是0-360。这是我最后得到的代码:
package com.dbsoftwares.dangerwheel.hologram;
import com.google.common.collect.Lists;
import net.md_5.bungee.api.ChatColor;
import java.util.List;
public class HologramCircle {
private final int height;
private final int width;
public HologramCircle(final int height, final int width) {
this.height = height;
this.width = width;
}
public List<String> getLines() {
final float width_r = (float) width / 2;
final float height_r = (float) height / 2;
final float ratio = width_r / height_r;
final double maxblocks_x;
final double maxblocks_y;
if ((width_r * 2) % 2 == 0) {
maxblocks_x = Math.ceil(width_r - .5) * 2 + 1;
} else {
maxblocks_x = Math.ceil(width_r) * 2;
}
if ((height_r * 2) % 2 == 0) {
maxblocks_y = Math.ceil(height_r - .5) * 2 + 1;
} else {
maxblocks_y = Math.ceil(height_r) * 2;
}
final List<String> lines = Lists.newArrayList();
for (double y = -maxblocks_y / 2 + 1; y <= maxblocks_y / 2 - 1; y++) {
final StringBuilder line = new StringBuilder();
for (double x = -maxblocks_x / 2 + 1; x <= maxblocks_x / 2 - 1; x++) {
if (shouldBeFilled(x, y, width_r, ratio)) {
final int sector = getSectorNumber(x, y, 12);
line.append(ChatColor.values()[sector].toString());
line.append("█");
}
}
lines.add(line.toString());
}
return lines;
}
private int getSectorNumber(final double x, final double y, final int sectorAmount) {
final double degreesPerSector = 360 / sectorAmount;
final double rad = Math.atan2(y, x);
final double degrees = rad * (180 / Math.PI) + 180;
for (int i = 0; i < sectorAmount; i++) {
final double startDegrees = degreesPerSector * i;
final double endDegrees = startDegrees + degreesPerSector;
if (degrees >= startDegrees && degrees <= endDegrees) {
return i;
}
}
return -1;
}
private double distance(final double x, final double y, final double ratio) {
return Math.sqrt((Math.pow(y * ratio, 2)) + Math.pow(x, 2));
}
private boolean shouldBeFilled(final double x, final double y, final double radius, final float ratio) {
return distance(x, y, ratio) <= radius;
}
}
This is my result now, it's not perfect, but this for sure fits my needs