Du verwendest einen veralteten Browser. Es ist möglich, dass diese oder andere Websites nicht korrekt angezeigt werden. Du solltest ein Upgrade durchführen oder ein alternativer Browser verwenden.
Ok. Ich hab es initialisiert..
aber wie krieg da jetzt die x[] und y[] werte rein?
sodass ich für points.x alle meine x-koordinaten habe und für points.y alle y-koordinaten?
Sorry steh gerade aufm Schlauch..sitze schon zulange davor..
int[] x = new int[10];
int[] y = new int[10];
Point[] points = new Point[10];
for (int i = 0; i < points.length; i++) {
points[i] = new Point(x[i], y[i]);
}
..........
Point[] points = new Point[n];
for (int i = 0; i < sides; i++) {
x[i] = cx + Math.cos(i * theta) * 1;
y[i] = cy + Math.sin(i * theta) * 1;
points[i] = new Point(x[i], y[i]);
this.points = points;
System.out.println(points[i].x);
}
System.out.println(points[0].x);
}
Bei meiner ersten Ausgabe -->
1.0
-0.4999999999999998
-0.5000000000000004
Kriege ich die Punkte, die ich brauche,also in der Schleife gibt er mit das richtige raus.
Bei meiner zweiten Ausgabe-->
-0.5000000000000004
das kriege ich für points[0].x,points[1].x,points[2].x
immer das gleiche??
warum nicht..
points[0].x --> 1.0
points[1].x---> -0.4999999999999998
points[2].x --> -0.5000000000000004
Hier mein kompletter Quellcode, für diese Klasse...
Für diese Aufgabe habe ich noch die Klasse Point, Angle und App, die main() beinhaltet.
Java:
public class Polygon {
private Point[] points;
public int sides;
private double theta, radius, cx, cy;
private double x[];
private double y[];
public Polygon(Point[] points) {
this.points = points;
}// end polygon points
// Punkte rotieren
public Polygon rotate(Angle phi) {
for (int i = 0; i < this.points.length; i++) {
this.points[i].rotate(phi);
}
return new Polygon(new Point[] { new Point(points[0].x, points[0].y) });
}
// Punkte verschieben
public Polygon translate(Point point) {
for (int i = 0; i < this.points.length; i++) {
points[i].shift(Point.x, Point.y);
}
return new Polygon(new Point[] { new Point(points[0].x, points[0].y) });
}
// Eckpunkte generieren
public Polygon(int n) {
Point args = new Point(n);
sides = n; // n-ecken
x = new double[sides];
y = new double[sides];
theta = 2.0 * Math.PI / sides;
cx = 0; // Mittelpunkt
cy = 0;
Point[] points = new Point[n];
for (int i = 0; i < sides; i++) {
x[i] = cx + Math.cos(i * theta) * 1;
y[i] = cy + Math.sin(i * theta) * 1;
points[i] = new Point(x[i], y[i]);
this.points = points;
}
}// end polygon n
public void draw() {
}
}// end Isogon
Hat keiner eine Idee, warum meine Punkte in Point[], alle mit dem gleichen Punkt besetzt sind,
sodass ich für points[0].x ,points[1].x,points[2].x......... immer den gleichen Wert hab und nicht die gewünschten x-Koordinaten, die ich ja in x[0], x[1],... ja habe??
Java:
import gdp.stdlib.*;
public class Polygon {
private Point[] points;
public int sides;
private double theta, radius, cx, cy;
private double x[];
private double y[];
public Polygon(Point[] points) {
this.points = points;
}// end polygon points
// Punkte rotieren
public Polygon rotate(Angle phi) {
for (int i = 0; i < this.points.length; i++) {
points[i].rotate(phi);
}
System.out.println(points[0].x + " " + points[1].x + " " + points[2].x);
return new Polygon(points);
}
// Punkte verschieben
public Polygon translate(Point point) {
for (int i = 0; i < this.points.length; i++) {
this.points[i].shift(Point.x, Point.y);
}
System.out.println(points[0].x + " " + points[1].x + " " + points[2].x);
return new Polygon(points);
}
// Eckpunkte generieren
public Polygon(int n) {
Point args = new Point(n);
sides = n; // n-ecken
this.x = new double[sides];
this.y = new double[sides];
theta = 2.0 * Math.PI / sides;
cx = 0; // Mittelpunkt
cy = 0;
Point[] points = new Point[n];
for (int i = 0; i < sides; i++) {
x[i] = cx + Math.cos(i * theta) * 1;
y[i] = cy + Math.sin(i * theta) * 1;
points[i] = new Point(x[i], y[i]);
this.points = points;
System.out.println(points[0].x + " " + points[1].x + " " + points[2].x);
}
System.out.println(points[0].x + " " + points[1].x + " " + points[2].x);
}// end polygon n
public void draw() {
System.out.println(points[0].x + " " + points[1].x + " " + points[2].x);
/*
double[] x1 = new double[points.length];
double[] y1 = new double[points.length];
for(int i=0;i<points.length;i++){
x1[0] = points[0].x;
x1[1] = points[1].x;
x1[2] = points[2].x;
y1[i] = Point.y;
System.out.println(x1[i]);
}
*/
}
}// end Isogon
Ich habe es gerade bei mir mal ausgeführt und bekomme drei verschiedene Punkte ausgegeben.
Was mir aber auffällt:
- Du erzeugst im Konstruktor eine lokale Variable "points". Entferne die und arbeite besser direkt auf dem privaten Point-Array. Es müllt den Code nur unnötig zu.
- die Variable Point args wird nie benutzt
- Warum erzeugst du die x- und y-Arrays? Du arbeitest nie mit ihnen, sondern speicherst die Werte direkt in einer Point-Instanz. Auch das würde ich vereinfachen und mit lokalen Variablen arbeiten, die ins Array geschrieben werden.
(Außerdem kostet dich die Erzeugung eines Arrays mehr Speicher als ein Integer, aber das ist wahrscheinlich Korinthenkackerei....)
- Außerdem würde ich Zeile 59 und 61 entfernen und stattdessen die draw()-Methode aufrufen. Man versucht, Redundanz und Dopplungen zu vermeiden, um eventuelle Änderungen nur an einer Stelle vornehmen zu müssen.
Erstmal Danke für die Tipps.
Ich sollte wohl noch ergänzend meine andere Klassen zeigen, die zu diesem Programm gehören.
Da wäre die erste Klasse, mein "Main" Klasse. Hier darf ich nichts verändern.. mein Programm muss mit diesen Angaben funktionieren:
Java:
import gdp.stdlib.StdDraw;
public class App {
public static void main(String[] args) {
if (args.length == 0) System.exit(-1);
int n = Integer.parseInt(args[0]);
double myscale = 5.0;
Angle delta = new Angle(360. / n);
Angle phi = new Angle(0.0);
Point pos = new Point(3., 0);
StdDraw.setXscale(-myscale, myscale);
StdDraw.setYscale(-myscale, myscale);
for (int i = 3; i < 3+n; i++) {
new Polygon(i).rotate(phi).translate(pos.rotate(phi)).draw();
phi = Angle.add(delta);
}
StdDraw.show();
}
}
Dazu muss ich drei weitere Klassen benutzen.
Einmal die,die ich hier schon gepostet hab... Polygon
und dann noch die hier:
Java:
import gdp.stdlib.StdDraw;
public class Point {
public static double x;
public static double y;
public static int args;
public Point(double x, double y){
this.x = x;
this.y = y;
}//end Point
public Point(int k){
this.args = k;
}
public Point rotate (Angle phi){
double xOld = Point.x;
double yOld = Point.y;
Point.x = xOld * Math.cos(Math.toRadians(phi.angle)) - yOld * Math.sin(Math.toRadians(phi.angle));
Point.y = xOld * Math.sin(Math.toRadians(phi.angle)) + yOld * Math.cos(Math.toRadians(phi.angle));
return new Point(Point.x,Point.y);
}//end rotate
public Point shift (double dx, double dy) {
Point.x = Point.x + dx;
Point.y = Point.y + dy;
return new Point(Point.x,Point.y);
}//end shift
}//end class Point
Java:
[code=Java]
public class Angle {
public static double angle;
Angle(double angle) {
Angle.angle = Math.toRadians(angle);
}
public static Angle add(Angle angle) {
Angle.angle = Math.toRadians(Angle.angle + (360 / Point.args));
return angle;
}
}
Also es muss new Polygon(i).rotate(phi).translate(pos.rotate(phi)).draw(); ausgeführt werden und für die Methode draw()
brauch ich zwei double[] damit ich meine Polygone zeichnen kann, mit dem Befehl StdDraw.polygon(double[]x,double[]y)
Mich deucht ein static Fehler in der Point-Klasse:
Analogie:
Java:
public class Test {
public static void main(String... args) {
StaticFail[] fail = new StaticFail[10];
for (int i = 0; i < fail.length; i++) {
fail[i] = new StaticFail(i);
}
for (int i = 0; i < fail.length; i++) {
System.out.println(fail[i].x);
}
}
static class StaticFail {
public static int x;
public StaticFail(int x) {
this.x = x;
}
}
}
Ja das war das Problem...zu oft falsch static angewendet.
Jetzt kriege ich die Punkte richtig.
Ich hab dafür mit getter und setter gearbeitet,danach hat es funktioniert.