Mittelpunkt mit Höhe und Breite bestimmen

ufo401

Mitglied
[CODE lang="java" title="Coordinate"]package de.hsalbsig.inf.geo;

public class Coordinate {
public static final double DEFAULT_X = 0.0;
public static final double DEFAULT_Y = 0.0;

public double x,y;

public Coordinate(double x, double y) {
this.x = x;
this.y = y;
}

public Coordinate() {
this(DEFAULT_X,DEFAULT_Y);
}

public Coordinate(Coordinate other) {
this(other.x, other.y);
}

public double getX() { return x;
}

public void setX(double x) {
this.x = x;
}

public double getY() {
return y;
}

public void setY(double y) {
this.y = y;
}

public double getDistance(Coordinate other) {
double dx = x - other.x;
double dy = y - other.y;

return Math.sqrt((dx * dx) + (dy * dy));
}

@Override
public String toString() {
return "Coordinate: x= " + x + " , y= " + y;
}

public static void main(String[] args) {
Coordinate c1 = new Coordinate(-5, 12);
Coordinate c2 = new Coordinate(2,10);

System.out.println(c1);
System.out.println(c2);
c1.getDistance(c2);
System.out.println("c1 -> c2: " + c1.getDistance(c2));
}


} // end of class


[/CODE]
[CODE lang="java" title="GeoFigure"]package de.hsalbsig.inf.geo;

public abstract class GeoFigure extends Coordinate{

public Coordinate center;
private int width;
private int height;
private int area;

// Standardkonstruktor
public GeoFigure(int width, int height, Coordinate center) {
this.width = width;
this.height = height;
this.area = area(width, height);
}

// Defaultkonstruktor
public GeoFigure() {
width = 0;
height = 0;
area = 0;
center = null;
}


public int area(int width, int height) {
return 0;
}

// getter
public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public int getArea() {
return area;
}

public void setCenter(Coordinate center) {
this.center = center;
}

public String toString() {
return "Width: " + width + "\nHeight: " + height + "\nArea: " + area + "\nCenter: " + center;
}

[CODE lang="java" title="Rectangle"]package de.hsalbsig.inf.geo;

public class Rectangle extends GeoFigure{

public Rectangle(int width, int height, Coordinate center) {
super(width, height, center);
}

// Methode überladen
public int area(int width, int height) {
return width * height;
}

public Coordinate center(double width, double height) {
double x = width / 2;
double y = height / 2;
center(x,y);
return center;
}
}// end of class
[/CODE]

[CODE lang="java" title="Rectangle"]package de.hsalbsig.inf.geo;

public class Rectangle extends GeoFigure{

public Rectangle(int width, int height, Coordinate center) {
super(width, height, center);
}

// Methode überladen
public int area(int width, int height) {
return width * height;
}

public Coordinate center(double width, double height) {
double x = width / 2;
double y = height / 2;
center(x,y);
return center;
}
}// end of class
[/CODE]

Hallo liebe Community, ich habe mal alle Codes die für meine Frage relevant sind erstmal hier oben gepostet damit ihr mich verstehen könnten :)
Ich soll mittels Höhe und Breite den Mittelpunkt des (in dem Fall Rechtecks) bestimmen. Nun aber die Frage, wie kommt man von zwei Längen auf eine zwei dimensionale Koordinate....
Muss ich da jetzt die zwei Werte durch 2 teilen und dadurch irgendwie den Mittelpunkt bestimmten, oder bin ich da auf der komplett falschen Spur? Und wenn ja, wie bekomme ich dadurch einen zwei dimensionalen Punkt mit x und y???
Bin über jede Hilfe dankbar...
 

Robert Zenz

Top Contributor
Ich soll mittels Höhe und Breite den Mittelpunkt des (in dem Fall Rechtecks) bestimmen. Nun aber die Frage, wie kommt man von zwei Längen auf eine zwei dimensionale Koordinate....
Gar nicht, du musst natuerlich auch zumindest die Koordinaten von einer Ecke wissen. Bei der linken, oberen Ecke ist es dann eben diese Koordinate plus die Haelfte der jeweiligen Laenge.
 

ufo401

Mitglied
Ja das habe ich auch schon vermutet... Die Aufgaben sind halt meistens so kompliziert gestellt das man das nicht wirklich versteht 😄
 

Neue Themen


Oben