public static class SensorDatabase
{
private Point[] inactiveDatabase, x, y;
/**
* Constructs a database of sensor readings
*
* @param inactiveAValues
* @param inactiveBValues
* @param inactiveCValues
* @param inactiveDValues
* @param xAValues
* @param xBValues
* @param xCValues
* @param xDValues
* @param yAValues
* @param yBValues
* @param yCValues
* @param yDValues
*/
public SensorDatabase(double[] inactiveAValues,
double[] inactiveBValues, double[] inactiveCValues,
double[] inactiveDValues, double[] xAValues, double[] xBValues,
double[] xCValues, double[] xDValues, double[] yAValues,
double[] yBValues, double[] yCValues, double[] yDValues)
{
try
{
this.inactiveDatabase = new Point[inactiveAValues.length];
this.x = new Point[xAValues.length];
this.y = new Point[yAValues.length];
for (int i = 0; i < inactiveAValues.length; i++)
{
this.inactiveDatabase[i] = new Point(inactiveAValues[i],
inactiveBValues[i], inactiveCValues[i],
inactiveDValues[i]);
}
for (int i = 0; i < xAValues.length; i++)
{
this.x[i] = new Point(xAValues[i], xBValues[i],
xCValues[i], xDValues[i]);
this.y[i] = new Point(yAValues[i], yBValues[i],
yCValues[i], yDValues[i]);
}
}
catch (Exception e)
{
throw new RuntimeException("Error: array indices didn't match!");
}
}
/**
* Constructs a database of sensor readings
*
* @param inactiveDatabase
* @param x
* @param y
*/
public SensorDatabase(Point[] inactiveDatabase, Point[] x, Point[] y)
{
if (x.length != y.length)
{
throw new RuntimeException(
"Error: x and y lengths aren't the same");
}
this.inactiveDatabase = inactiveDatabase.clone();
this.x = x.clone();
this.y = y.clone();
}
/**
* Finds the closest x value
*
* @return
*/
public Point computeClosestX()
{
Point closest = this.x[0];
double distance = Point.computeDistance(this.inactiveDatabase[0],
this.x[0]);
for (int i = 1; i < this.x.length; i++)
{
if (distance > Point.computeDistance(this.inactiveDatabase[i],
this.x[i]))
{
closest = this.x[i];
distance = Point.computeDistance(this.inactiveDatabase[i],
this.x[i]);
}
}
return closest;
}
/**
* Finds the closest y value
*
* @return
*/
public Point computeClosestY()
{
Point closest = this.y[0];
double distance = Point.computeDistance(this.inactiveDatabase[0],
this.y[0]);
for (int i = 1; i < this.y.length; i++)
{
if (distance > Point.computeDistance(this.inactiveDatabase[i],
this.y[i]))
{
closest = this.y[i];
distance = Point.computeDistance(this.inactiveDatabase[i],
this.y[i]);
}
}
return closest;
}
public static class Point
{
public double a, b, c, d;
/**
* Constructs a point
*
* @param a
* @param b
* @param c
* @param d
*/
public Point(double a, double b, double c, double d)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
/**
* Computes the distance between p1 and p2 as defined as:
*
* sqrt((p1.a - p2.a)^2 + (p1.b - p2.b)^2 + (p1.c - p2.c)^2 + (p1.d
* - p2.d, 2)^2);
*
* @param p1
* @param p2
* @return
*/
public static double computeDistance(Point p1, Point p2)
{
return Math.sqrt(Math.pow(p1.a - p2.a, 2)
+ Math.pow(p1.b - p2.b, 2) + Math.pow(p1.c - p2.c, 2)
+ Math.pow(p1.d - p2.d, 2));
}
}
}