Skip to content

Commit

Permalink
Modified MathUtil#mean() return type to Centroid. Furthermore, add ne…
Browse files Browse the repository at this point in the history
…w function to Point which returns the number of dimensions.
  • Loading branch information
Christian Vogel committed Aug 15, 2011
1 parent 8834252 commit a709324
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/de/jail/geometry/schemas/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public Point(int dim) {
vector = new double[dim];
}

public int getDimension() {
return vector.length;
}

/**
* Addition of this point with a defined one.
*
Expand All @@ -30,8 +34,8 @@ public Point add(final Point other) {
throw new IllegalArgumentException("argument must not be null");
}

if(!this.equals(other)) {
throw new IllegalArgumentException("points not equal means calculation not possible");
if(this.getDimension() != other.getDimension()) {
throw new IllegalArgumentException("points not equal, means calculation not possible");
}

double[] otherVector = other.getVector();
Expand Down
11 changes: 8 additions & 3 deletions src/de/jail/utils/MathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.Collection;

import de.jail.geometry.schemas.Centroid;
import de.jail.geometry.schemas.Point;

/**
Expand Down Expand Up @@ -91,9 +92,11 @@ public static float square(float arg) {
* Calculates the mean from a collection of points.
*
* @param points collection of points
* @return calculated mean
* @return calculated mean point
*
* @see Centroid
*/
public static Point mean(Collection<Point> points) {
public static Centroid mean(Collection<Point> points) {
if(points == null) {
throw new IllegalArgumentException("argument must not be null");
}
Expand All @@ -112,6 +115,8 @@ public static Point mean(Collection<Point> points) {
}
}

return mean.multiply(points.size());
Point center = mean.multiply(1.0/(double)points.size());

return new Centroid(center);
}
}

0 comments on commit a709324

Please sign in to comment.