Ich habe ein Array von einem Unit-Objekt und das Unit-Objekt hat cx, cy, und cr. Ich möchte nur die Umrisse der Kreise zeigen (denke ein ven-Diagramm ohne die überlappenden Bits). Ich habe es geschafft, aber es macht es extrem langsam, da es eine verschachtelte for-Schleife ist. Hier ist der Code:Wie kann dieser Code optimiert werden, um nur den Umriss eines Arrays von Kreisen zu zeigen?
(das ist alles in einem Verfahren, das Zyklus durch alle Einheiten)
ArrayList<Integer> validangles = new ArrayList<Integer>();
public void getValidAngles(ArrayList<Unit> units) { //get all the angles that aren't overlapping
ArrayList<Integer> invalidAngles = new ArrayList<Integer>();
for (int i = 0; i < units.size(); i++) { //cycle through all other units
Unit c2 = units.get(i);
if (this != c2) { //make sure it is not the same unit
for (int ia = 0; ia < 360; ia += 10) { //cycle through the angles
double ca = Math.toRadians(ia);
Point p = new Point(//point on the circle
(int) Math.round((c2.getCx() + (cr * Math.cos(ca)))),
(int) Math.round((c2.getCy() + (cr * Math.sin(ca)))));
if (overlapping(p)) {
invalidAngles.add(ia-180); //this angle should not be shown
}
}
}
}
validangles.clear();
for (int i = 0; i < 360; i += 10) {
if (!invalidAngles.contains(i-180)) {
validangles.add(i-180);
}
}
}
public void drawValidAngles(Graphics g2) {
for(int i : validangles) {
int x = (int)Math.round(cx+cr*Math.cos(Math.toRadians(i)));
int y = (int)Math.round(cy+cr*Math.sin(Math.toRadians(i)));
g2.drawLine(x, y, x, y);
}
}
Das Problem ist, dass, wenn ich nach oben von ein paar hundert Einheiten, die gemeinsam sein werden, verlangsamt das Programm um eine Tonne, weil es ein Forloop von Einheiten in einem anderen Forloop von Einheiten verschachtelt.