Ejemplo n.º 1
0
 /**
  * Create a polygon from a RCvalue(matrix) NOTE: the connection from matlab to java TODO: what if
  * there are less than 3 points
  */
 protected PolygonValue(RCvalue u) throws EvalException { // NOTE: matlab to java
   Point[] points;
   if (u.isRow()) {
     // each column should have two double values
     points = new Point[u.size()];
     for (int i = 0; i < u.size(); i++) {
       Value v = u.value(i);
       if (!(v instanceof RCvalue)) rc_bad();
       RCvalue c = (RCvalue) v;
       if (c.size() != 2) rc_bad();
       Value x = c.value(0), y = c.value(1);
       if (!(x instanceof DoubleValue) || !(y instanceof DoubleValue)) rc_bad();
       points[i] = Point.create(((DoubleValue) x).value(), ((DoubleValue) y).value());
     }
   } else {
     if (!(u.value(0) instanceof RCvalue) || !(u.value(1) instanceof RCvalue)) rc_bad();
     RCvalue xMatrix = (RCvalue) (u.value(0)), yMatrix = (RCvalue) u.value(1);
     if (xMatrix.size() != yMatrix.size()) rc_bad();
     points = new Point[xMatrix.size()];
     for (int i = 0; i < xMatrix.size(); i++) {
       Value x = xMatrix.value(i), y = yMatrix.value(i);
       if (!(x instanceof DoubleValue) || !(y instanceof DoubleValue)) rc_bad();
       points[i] = Point.create(((DoubleValue) x).value(), ((DoubleValue) y).value());
     }
   }
   poly = new SimplePolygon(points);
   if (ConvexPolygon.isConvex(
       (SimplePolygon) poly)) // create convexPolygon if it is, simplify the computation later
     // BUGS, SimplePolygon removes duplicated points. Therefore, it is incorrect to construct a
     // convex polygon
     // using points and do not check. We should use the points from poly.
     // poly = new ConvexPolygon(points,false);
     poly = new ConvexPolygon(poly.points(), false);
   if (poly.degree() == u.size()) // SimplePolygon may remove redundant points
   rc = u;
   else rc = null;
 }