Пример #1
0
  private void draw_horizon(int rad, Point center, int[] angles) {
    // Draw an arc
    int arc_angle =
        ((angles[0] > angles[1]) ? (360 - angles[0]) + angles[1] : (angles[1] - angles[0]));

    Polygon remainder = new Polygon();

    offgraphics_.setColor(GREEN);
    offgraphics_.fillArc(center.x - rad, center.y - rad, 2 * rad, 2 * rad, angles[0], arc_angle);

    if (pitch_ != 0) {
      if ((pitch_ > 0 && Math.abs(roll_) < 90) || (pitch_ < 0 && Math.abs(roll_) >= 90))
        offgraphics_.setColor(BLUE);

      int cover_angle = (angles[0] + arc_angle / 2 + ((arc_angle < 180) ? 180 : 0)) % 360;

      // System.out.println (points[0] + " " + points[1]);

      // System.out.println (accepted_point);

      remainder.addPoint(
          center.x + polar_to_rect_x(rad, cover_angle),
          center.y - polar_to_rect_y(rad, cover_angle));
      remainder.addPoint(
          center.x + polar_to_rect_x(rad, angles[0]), center.y - polar_to_rect_y(rad, angles[0]));
      remainder.addPoint(
          center.x + polar_to_rect_x(rad, angles[1]), center.y - polar_to_rect_y(rad, angles[1]));
      offgraphics_.fillPolygon(remainder);
      // offgraphics_.setColor (getBackground ());
      // offgraphics_.drawPolygon (remainder);
    }
  }
Пример #2
0
 /** NOTE: the connection from java to matlab */
 public RCvalue java2Matlab() throws EvalException {
   if (rc == null) {
     if (poly != null) {
       Value[] v = new Value[poly.degree()];
       for (int i = 0; i < v.length; i++) {
         Point pt = poly.point(i);
         assert pt.type() == CohoDouble.type
             : "The result type is not CohoDouble, it is " + pt.type();
         //					if(pt.type()!=CohoDouble.type){
         //					throw new RuntimeException("The result type is not CohoDouble, it is "+pt.type() );
         //					}
         v[i] =
             RCvalue.factory()
                 .create(
                     new Value[] {
                       DoubleValue.factory()
                           .create(new Double(((CohoDouble) pt.x()).doubleValue()), null),
                       DoubleValue.factory()
                           .create(new Double(((CohoDouble) pt.y()).doubleValue()), null)
                     },
                     false);
       }
       rc = (RCvalue) (RCvalue.factory().create(v, true));
     } else { // empty polygon
       rc = (RCvalue) (RCvalue.factory().create(new Value[0], true));
     }
   }
   return (rc);
 }
Пример #3
0
  private static void graph(Polygon[] finalPolygons) {
    int weiners = 0;
    String[] Starter = sPoint.split(",");
    String[] Ender = ePoint.split(",");
    int endx = Integer.parseInt(Ender[0]);
    int endy = Integer.parseInt(Ender[1]);
    int G = 40;
    for (int i = 0; i < G; i++) {
      for (int j = 0; j < G; j++) {
        int loketest = 0;
        if ((i == Startx && j == Starty) || i == endx && j == endy) {
          loketest = 2;
        }
        for (Polygon helpme : finalPolygons) {
          if (helpme.contains(i, j)) {
            loketest = 1;
          }
        }
        if (loketest == 1) {
          System.out.print("1");
        } else if (loketest == 2) {
          System.out.print("X");
        } else {
          System.out.print("0");
        }
      }

      System.out.println("     \t");
      weiners++;
    }
  }
Пример #4
0
          public Value eval(RCvalue args) throws EvalException {
            if ((args.size() < 0) || (3 < args.size()))
              throw new EvalException("usage: reduce(polygon, [errtol,[edgeReducible] ] ])");
            if (!(args.value(0) instanceof PolygonValue))
              throw new EvalException("reduce: first operand must be a polygon");

            double errtol = defaultErrtol;
            if (args.size() >= 2) { // get error tolerance
              if (!(args.value(1) instanceof DoubleValue))
                throw new EvalException("reduce: second operand must be an integer");
              errtol = ((DoubleValue) (args.value(1))).value();
            }
            boolean edgeReducible = true;
            if (args.size() >= 3) {
              double temp = ((DoubleValue) (args.value(2))).value();
              edgeReducible = (temp > 0); // temp = 1, edge reducible, otherwise, not
            }

            Polygon.EndCondition ec = new Polygon.CostEndCondition(errtol);
            if (errtol >= 3) {
              int maxV = (int) Math.round(errtol);
              ec = new Polygon.DegreeEndCondition(maxV);
            }
            PolygonValue p = (PolygonValue) (args.value(0));
            Polygon poly = p.polygon();
            if (poly instanceof ConvexPolygon) { // NOTE: connection for reduce
              if (edgeReducible) return new PolygonValue(((ConvexPolygon) poly).reduce(ec));
              else return p; // can't reduce because convex polygon can only reduce edge
            } else {
              return new PolygonValue(poly.reduce(ec, true, edgeReducible));
            }
          }
Пример #5
0
 public Value eval(RCvalue args) throws EvalException {
   if (args.size() != 1) throw new EvalException("usage: hull(polygon)");
   if (!(args.value(0) instanceof PolygonValue))
     throw new EvalException("hull: argument must be a polygon");
   PolygonValue pv = (PolygonValue) (args.value(0));
   Polygon poly = pv.polygon();
   return new PolygonValue(poly.convexHull()); // NOTE: connection for convex hull
 }
Пример #6
0
  private Polygon outline(int x, int y, double direction) {
    Polygon shape = new Polygon();

    shape.addPoint(x, y);
    addPointRelative(shape, x, y, getOuterRadius(), direction - getAngle());
    addPointRelative(shape, x, y, getInnerRadius(), direction);
    addPointRelative(shape, x, y, getOuterRadius(), direction + getAngle());
    shape.addPoint(x, y); // Closing the polygon (TEG 97-04-23)
    return shape;
  }
Пример #7
0
  public Geobuf.Data.Geometry polyToGeobuf(Polygon poly) {
    Geobuf.Data.Geometry.Builder builder =
        Geobuf.Data.Geometry.newBuilder().setType(Geobuf.Data.Geometry.Type.POLYGON);

    Stream<LineString> interiorRings =
        IntStream.range(0, poly.getNumInteriorRing()).mapToObj(poly::getInteriorRingN);

    Stream.concat(Stream.of(poly.getExteriorRing()), interiorRings)
        .forEach(r -> addRing(r, builder));

    return builder.build();
  }
Пример #8
0
  /**
   * Adds a polygon. Note that the vertices of the polygon are passed using a float[][] and NOT a
   * float[]. Eg.
   *
   * <p>{{x0, y0, z0}, {x1, y1, z1}, ...}
   *
   * <p>We *flatten* the data once it is encapsulated inside this class; outside this class we want
   * clarity; inside this class we want speed!
   */
  public int addPolygon(float[][] vs, Color c, boolean doubleSided) {
    Polygon polygon = new Polygon(vs.length, c, doubleSided);
    for (int i = 0; i < vs.length; i++) {
      polygon.addPoint(this.addPoint(vs[i][0], vs[i][1], vs[i][2]));
    }
    polygons[polynext] = polygon;

    // if this was the last polygon then we may now set the bounding box
    if (polynext == polygons.length - 1) {
      this.box.setBB();
    }
    return polynext++;
  }
Пример #9
0
  /**
   * make sure outer ring is CCW and holes are CW
   *
   * @param p polygon to check
   */
  Polygon makeGoodSHAPEPolygon(Polygon p) {
    LinearRing outer;
    LinearRing[] holes = new LinearRing[p.getNumInteriorRing()];
    Coordinate[] coords;

    coords = p.getExteriorRing().getCoordinates();

    if (cga.isCCW(coords)) {
      outer = reverseRing((LinearRing) p.getExteriorRing());
    } else {
      outer = (LinearRing) p.getExteriorRing();
    }

    for (int t = 0; t < p.getNumInteriorRing(); t++) {
      coords = p.getInteriorRingN(t).getCoordinates();

      if (!(cga.isCCW(coords))) {
        holes[t] = reverseRing((LinearRing) p.getInteriorRingN(t));
      } else {
        holes[t] = (LinearRing) p.getInteriorRingN(t);
      }
    }

    return new Polygon(outer, holes, new PrecisionModel(), 0);
  }
Пример #10
0
  /**
   * Draws a single arrow head
   *
   * @param aG the canvas to draw on;
   * @param aXpos the X position of the arrow head;
   * @param aYpos the (center) Y position of the arrow head;
   * @param aFactor +1 to have a left-facing arrow head, -1 to have a right-facing arrow head;
   * @param aArrowWidth the total width of the arrow head;
   * @param aArrowHeight the total height of the arrow head.
   */
  public static final void drawArrowHead(
      final Graphics2D aG,
      final int aXpos,
      final int aYpos,
      final int aFactor,
      final int aArrowWidth,
      final int aArrowHeight) {
    final double halfHeight = aArrowHeight / 2.0;
    final int x1 = aXpos + (aFactor * aArrowWidth);
    final int y1 = (int) Math.ceil(aYpos - halfHeight);
    final int y2 = (int) Math.floor(aYpos + halfHeight);

    final Polygon arrowHead = new Polygon();
    arrowHead.addPoint(aXpos, aYpos);
    arrowHead.addPoint(x1, y1);
    arrowHead.addPoint(x1, y2);

    aG.fill(arrowHead);
  }
Пример #11
0
  public Geobuf.Data.Geometry multiPolyToGeobuf(MultiPolygon poly) {
    Geobuf.Data.Geometry.Builder builder =
        Geobuf.Data.Geometry.newBuilder().setType(Geobuf.Data.Geometry.Type.MULTIPOLYGON);

    // first we specify the number of polygons
    builder.addLengths(poly.getNumGeometries());

    for (int i = 0; i < poly.getNumGeometries(); i++) {
      Polygon p = (Polygon) poly.getGeometryN(i);
      // how many rings there are
      builder.addLengths(p.getNumInteriorRing() + 1);

      Stream<LineString> interiorRings =
          IntStream.range(0, p.getNumInteriorRing()).<LineString>mapToObj(p::getInteriorRingN);

      Stream.concat(Stream.of(p.getExteriorRing()), interiorRings)
          .forEach(r -> addRing(r, builder));
    }

    return builder.build();
  }
Пример #12
0
  // From: http://forum.java.sun.com/thread.jspa?threadID=378460&tstart=135
  void drawArrow(
      Graphics2D g2d,
      int xCenter,
      int yCenter,
      int x,
      int y,
      float stroke,
      BasicStroke drawStroke) {
    double aDir = Math.atan2(xCenter - x, yCenter - y);
    // Line can be dashed.
    g2d.setStroke(drawStroke);
    g2d.drawLine(x, y, xCenter, yCenter);
    // make the arrow head solid even if dash pattern has been specified
    g2d.setStroke(lineStroke);
    Polygon tmpPoly = new Polygon();
    int i1 = 12 + (int) (stroke * 2);
    // make the arrow head the same size regardless of the length length
    int i2 = 6 + (int) stroke;
    tmpPoly.addPoint(x, y);
    tmpPoly.addPoint(x + xCor(i1, aDir + .5), y + yCor(i1, aDir + .5));
    tmpPoly.addPoint(x + xCor(i2, aDir), y + yCor(i2, aDir));
    tmpPoly.addPoint(x + xCor(i1, aDir - .5), y + yCor(i1, aDir - .5));
    tmpPoly.addPoint(x, y); // arrow tip
    g2d.drawPolygon(tmpPoly);

    // Remove this line to leave arrow head unpainted:
    g2d.fillPolygon(tmpPoly);
  }
Пример #13
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;
 }
Пример #14
0
  /**
   * The GraphicDrawer.paint() Function allows the user to draw pre-defined graphics into a given
   * Graphics Object. The user may give the rectangle properties which defines at best the desired
   * area to draw with a Int id which requests the desired graphic, whenever it's vectorial or
   * raster graphics
   *
   * @author LuisArturo
   */
  public void paint(int id, Dimension r, Graphics g) {
    Graphics2D g2;
    switch (id) {
      case FILE:
        {
          g2 = (Graphics2D) g;
          try {
            FileInputStream img = new FileInputStream("archivo.png");
            BufferedImage in = ImageIO.read(img);
            g2.drawImage(
                in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        break;
      case REDO:
        {
          g2 = (Graphics2D) g;
          g2.setColor(color);
          g2.fillArc(
              r.width * 2 / 10, r.height * 2 / 10, r.width * 6 / 10, r.height * 6 / 10, 270, 225);
          Polygon p = new Polygon();
          p.addPoint(r.width * 2 / 10, r.height * 2 / 10);
          p.addPoint(r.width * 2 / 10, r.height * 5 / 10);
          p.addPoint(r.width * 5 / 10, r.height * 5 / 10);
          g2.fillPolygon(p);
          g2.setColor(g2.getBackground());
          g2.fillArc(
              r.width * 3 / 10, r.height * 3 / 10, r.width * 4 / 10, r.height * 4 / 10, 270, 225);
        }
        break;
      case TEXT:
        {
          g2 = (Graphics2D) g;
          g2.setColor(color);
          g2.setFont(new Font("Serif", Font.ITALIC | Font.BOLD, r.height * 6 / 10));
          g2.drawChars("A".toCharArray(), 0, 1, r.width * 3 / 10, r.height * 7 / 10);
        }
        break;
      case SELECT:
        {
          g2 = (Graphics2D) g;
          try {
            FileInputStream img = new FileInputStream("mano.png");
            BufferedImage in = ImageIO.read(img);
            g2.drawImage(
                in, r.width * 1 / 10, r.height * 1 / 10, r.width * 2 / 3, r.height * 2 / 3, null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        break;
      case PENCIL:
        {
          g2 = (Graphics2D) g;
          try {
            FileInputStream img = new FileInputStream("lapiz.png");
            BufferedImage in = ImageIO.read(img);
            g2.drawImage(
                in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        break;

      case ARROW:
        {
          g2 = (Graphics2D) g;
          try {
            FileInputStream img = new FileInputStream("arrow.png");
            BufferedImage in = ImageIO.read(img);
            g2.drawImage(
                in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        break;
      case ZOOM:
        {
          g2 = (Graphics2D) g;
          try {
            FileInputStream img = new FileInputStream("zoom.png");
            BufferedImage in = ImageIO.read(img);
            g2.drawImage(
                in, r.width * 2 / 10, r.height * 2 / 10, r.width * 2 / 3, r.height * 2 / 3, null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        break;
    }
  }
Пример #15
0
 private void addPointRelative(Polygon shape, int x, int y, double radius, double angle) {
   shape.addPoint(x + (int) (radius * Math.cos(angle)), y + (int) (radius * Math.sin(angle)));
 }
Пример #16
0
 public Value eval(RCvalue args) throws EvalException {
   if (args.size() != 2) throw new EvalException("usage: intersect(poly1,poly2)");
   Polygon p1 = ((PolygonValue) args.value(0)).polygon();
   Polygon p2 = ((PolygonValue) args.value(1)).polygon();
   return new PolygonValue(p1.intersect(p2));
 }