Example usage for com.badlogic.gdx.math Polygon contains

List of usage examples for com.badlogic.gdx.math Polygon contains

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Polygon contains.

Prototype

public boolean contains(float x, float y) 

Source Link

Document

Returns whether an x, y pair is contained within the polygon.

Usage

From source file:com.bladecoder.engineeditor.scneditor.ScnWidgetInputListener.java

License:Apache License

@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

    super.touchDown(event, x, y, pointer, button);
    //      EditorLogger.debug("Touch Down - X: " + x + " Y: " + y);

    Scene scn = scnWidget.getScene();/*from w w w . ja v  a2s  .c o m*/
    if (scn == null)
        return false;

    Vector2 p = new Vector2(Gdx.input.getX(), Gdx.input.getY());
    scnWidget.screenToWorldCoords(p);
    org.set(p);

    if (button == Buttons.LEFT) {
        selActor = scnWidget.getSelectedActor();

        if (scn.getPolygonalNavGraph() != null && scnWidget.getShowWalkZone()) { // Check
            // WALKZONE

            // CHECK WALKZONE VERTEXS
            Polygon wzPoly = scn.getPolygonalNavGraph().getWalkZone();
            float verts[] = wzPoly.getTransformedVertices();

            for (int i = 0; i < verts.length; i += 2) {
                if (p.dst(verts[i], verts[i + 1]) < CanvasDrawer.CORNER_DIST) {
                    draggingMode = DraggingModes.DRAGGING_WALKZONE_POINT;
                    vertIndex = i;
                    return true;
                }
            }

            // CHECK FOR WALKZONE DRAGGING
            if (wzPoly.contains(p.x, p.y)) {
                draggingMode = DraggingModes.DRAGGING_WALKZONE;
                undoOrg.set(wzPoly.getX(), wzPoly.getY());
                return true;
            }

        }

        // SELACTOR VERTEXs DRAGGING
        if (selActor != null
                && (!(selActor instanceof SpriteActor) || !((SpriteActor) selActor).isBboxFromRenderer())
                && !(scnWidget.getSelectedActor() instanceof AnchorActor)) {

            Polygon bbox = selActor.getBBox();
            float verts[] = bbox.getTransformedVertices();
            for (int i = 0; i < verts.length; i += 2) {
                if (p.dst(verts[i], verts[i + 1]) < CanvasDrawer.CORNER_DIST) {
                    draggingMode = DraggingModes.DRAGGING_BBOX_POINT;
                    vertIndex = i;
                    return true;
                }
            }
        }

        BaseActor a = scn.getActorAt(p.x, p.y); // CHECK FOR ACTORS

        if (a != null && a != selActor) {
            selActor = a;
            BaseActor da = Ctx.project.getActor(selActor.getId());
            Ctx.project.setSelectedActor(da);
        }

        if (a != null) {
            draggingMode = DraggingModes.DRAGGING_ACTOR;
            undoOrg.set(selActor.getX(), selActor.getY());
            return true;
        }

        // CHECK FOR DRAGGING DEPTH MARKERS
        Vector2 depthVector = scnWidget.getScene().getDepthVector();
        if (depthVector != null) {
            p.set(0, depthVector.x);
            scnWidget.worldToScreenCoords(p);
            if (Vector2.dst(p.x - 40, p.y, x, y) < 50) {
                draggingMode = DraggingModes.DRAGGING_MARKER_0;
                return true;
            }

            p.set(0, depthVector.y);
            scnWidget.worldToScreenCoords(p);
            if (Vector2.dst(p.x - 40, p.y, x, y) < 50) {
                draggingMode = DraggingModes.DRAGGING_MARKER_100;
                return true;
            }
        }

    }

    return true;
}

From source file:es.eucm.ead.editor.utils.ImageBorderTracer.java

License:Open Source License

@Override
public void create() {
    super.create();

    Pixmap pm;// w  ww  .  j a v  a  2  s .  c o  m

    pm = createSamplePixmap(300, 300, null);
    samplePixmap = new Texture(pm);
    for (Geometry g : GeometryUtils.findBorders(pm, .1, 2)) {
        red.add(GeometryUtils.jtsCoordsToGdx(g.getCoordinates()));
    }

    pm = openImagePixmap();
    imagePixmap = new Texture(pm);
    for (Geometry g : GeometryUtils.findBorders(pm, .1, 2)) {
        Coordinate[] cs = g.getCoordinates();
        for (Coordinate c : cs) {
            c.setCoordinate(new Coordinate(c.x, c.y));
        }
        blue.add(GeometryUtils.jtsCoordsToGdx(cs));
    }

    Gdx.input.setInputProcessor(new InputAdapter() {
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            int polygonsTouched = 0;
            for (Polygon polygon : blue) {
                if (polygon.contains(screenX, Gdx.graphics.getHeight() - screenY)) {
                    polygonsTouched++;
                }
            }
            Gdx.app.log("ImageBorderTracer",
                    "Blue polygon" + (polygonsTouched % 2 == 0 ? " not" : "") + " touched");
            return false;
        }
    });
}

From source file:es.eucm.ead.editor.view.builders.scene.groupeditor.GroupEditor.java

License:Open Source License

/**
 * @return if there was any element in the given coordinates
 *///from  ww w .jav a  2s  .c  o  m
public boolean selectLayer(float x, float y) {
    layersTouched.clear();
    Vector2 origin = Pools.obtain(Vector2.class);
    Vector2 size = Pools.obtain(Vector2.class);
    Vector2 tmp = Pools.obtain(Vector2.class);
    Polygon polygon = Pools.obtain(Polygon.class);

    for (Actor actor : editedGroup.getChildren()) {
        EngineUtils.calculateBounds(actor, origin, size);
        int j = 0;
        for (int i = 0; i < 4; i++) {
            tmp.set(i == 0 || i == 3 ? origin.x : origin.x + size.x, i > 1 ? origin.y : origin.y + size.y);
            actor.localToAscendantCoordinates(this, tmp);
            points[j++] = tmp.x;
            points[j++] = tmp.y;
        }
        polygon.setVertices(points);
        if (polygon.contains(x, y)) {
            layersTouched.add(actor);
        } else {
            for (int i = 0; i < 8; i += 2) {
                if (nearEnough(x, y, points[i], points[i + 1])) {
                    layersTouched.add(actor);
                    break;
                }
            }
        }
    }
    Pools.free(polygon);
    Pools.free(tmp);
    Pools.free(origin);
    Pools.free(size);
    if (layersTouched.size > 0) {
        showLayerSelector(x, y);
        return true;
    }
    return false;
}

From source file:es.eucm.ead.engine.assets.drawables.shapes.GdxBezierShape.java

License:Open Source License

protected Pixmap generatePixmap() {
    ArrayList<Float> shape = new ArrayList<Float>();
    float x0, y0, x1, y1, x2, y2, x3, y3;
    EAdList<Integer> pointsList = descriptor.getPoints();
    x0 = pointsList.get(0);/*  w ww.  j  a v a  2 s. c  o  m*/
    y0 = pointsList.get(1);
    shape.add(x0);
    shape.add(y0);

    int pointIndex = 2;

    while (pointIndex < pointsList.size()) {
        int length = pointsList.get(pointIndex++);
        switch (length) {
        case 1:
            x1 = pointsList.get(pointIndex++);
            y1 = pointsList.get(pointIndex++);
            lineTo(x1, y1, shape);
            x0 = x1;
            y0 = y1;
            break;
        case 2:
            x1 = pointsList.get(pointIndex++);
            y1 = pointsList.get(pointIndex++);
            x2 = pointsList.get(pointIndex++);
            y2 = pointsList.get(pointIndex++);
            quadTo(x0, y0, x1, y1, x2, y2, shape);
            x0 = x2;
            y0 = y2;
            break;
        case 3:
            x1 = pointsList.get(pointIndex++);
            y1 = pointsList.get(pointIndex++);
            x2 = pointsList.get(pointIndex++);
            y2 = pointsList.get(pointIndex++);
            x3 = pointsList.get(pointIndex++);
            y3 = pointsList.get(pointIndex++);
            curveTo(x0, y0, x1, y1, x2, y2, x3, y3, shape);
            x0 = x3;
            y0 = y3;
            break;
        default:

        }
    }

    // TODO Probably this can be improved

    EAdPaint p = descriptor.getPaint();
    if (p == null) {
        p = ColorFill.WHITE;
    }

    float f[] = new float[shape.size()];
    for (int i = 0; i < shape.size(); i++) {
        f[i] = shape.get(i);
    }
    Polygon polygon = new Polygon(f);

    Rectangle rectangle = polygon.getBoundingRectangle();

    int borderWidth = p.getBorderWidth();
    int x = (int) rectangle.x;
    int y = (int) rectangle.y;
    int width = (int) (rectangle.x + rectangle.width);
    int height = (int) (rectangle.y + rectangle.height);

    Pixmap pixmap = new Pixmap(width + borderWidth * 2, height + borderWidth * 2, Pixmap.Format.RGBA8888);
    pixmapContains = new Pixmap(width + borderWidth * 2, height + borderWidth * 2, Pixmap.Format.RGBA8888);
    pixmapContains.setColor(0, 0, 0, 1);
    pixmap.setColor(0, 0, 0, 0);
    pixmap.fill();

    if (p.getFill() instanceof ColorFill) {
        ColorFill c = (ColorFill) p.getFill();
        pixmap.setColor(c.getRed() / 255.0f, c.getGreen() / 255.0f, c.getBlue() / 255.0f,
                c.getAlpha() / 255.0f);
    } else if (p.getFill() instanceof LinearGradientFill) {
        LinearGradientFill gradient = (LinearGradientFill) p.getFill();
        usingGradient = true;
        this.initGradientParams(gradient.getColor1(), gradient.getX0(), gradient.getY0(), gradient.getColor2(),
                gradient.getX1(), gradient.getY1());

    } else {
        pixmap.setColor(0, 0, 0, 1);
    }

    for (int i = x; i < width; i++) {
        for (int j = y; j < height; j++) {
            if (polygon.contains(i, j)) {
                if (usingGradient) {
                    this.setColor(pixmap, borderWidth + i, borderWidth + j);
                }
                pixmap.drawPixel(borderWidth + i, borderWidth + j);
                pixmapContains.drawPixel(borderWidth + i, borderWidth + j);
            }
        }
    }
    usingGradient = false;

    if (p.getBorder() != null) {
        if (p.getBorder() instanceof ColorFill) {
            ColorFill c = (ColorFill) p.getBorder();
            pixmap.setColor(c.getRed() / 255.0f, c.getGreen() / 255.0f, c.getBlue() / 255.0f,
                    c.getAlpha() / 255.0f);
        } else if (p.getBorder() instanceof LinearGradientFill) {
            LinearGradientFill gradient = (LinearGradientFill) p.getBorder();
            usingGradient = true;
            initGradientParams(gradient.getColor1(), gradient.getX0(), gradient.getY0(), gradient.getColor2(),
                    gradient.getX1(), gradient.getY1());
        }

        float previousX = 0;
        float previousY = 0;
        float currentX;
        float currentY;
        for (int k = 0; k < shape.size(); k += 2) {
            currentX = shape.get(k);
            currentY = shape.get(k + 1);
            if (k >= 2) {
                for (int i = 1; i <= borderWidth; i++) {
                    if (usingGradient) {
                        this.setColor(pixmap, (int) previousX + i, (int) previousY + i);
                    }
                    pixmap.drawLine((int) previousX + i, (int) previousY + i, (int) currentX + i,
                            (int) currentY + i);
                    pixmapContains.drawLine((int) previousX + i, (int) previousY + i, (int) currentX + i,
                            (int) currentY + i);
                }
            }
            previousX = currentX;
            previousY = currentY;
        }
        for (int i = 1; i <= borderWidth; i++) {
            pixmap.drawLine((int) previousX + i, (int) previousY + i, (int) shape.get(0).intValue() + i,
                    (int) shape.get(1).intValue() + i);
            pixmapContains.drawLine((int) previousX + i, (int) previousY + i, (int) shape.get(0).intValue() + i,
                    (int) shape.get(1).intValue() + i);
        }

    }
    usingGradient = false;
    return pixmap;
}

From source file:es.eucm.ead.engine.entities.actors.EntityGroup.java

License:Open Source License

@Override
public Actor hit(float x, float y, boolean touchable) {
    Actor actor = super.hit(x, y, touchable);
    if (actor == null || actor == this) {
        Array<Polygon> collider = getCollider();
        if (collider != null && collider.size > 0) {
            int polygonsHit = 0;
            for (Polygon p : collider) {
                if (p.contains(x, y)) {
                    polygonsHit++;//from ww  w. ja  va 2 s  .  c o m
                }
            }
            return polygonsHit % 2 == 1 ? this : null;
        }
    }
    return actor;
}

From source file:kyle.game.besiege.Kingdom.java

License:Open Source License

public void updateArmyPolygon(Army army) {
    if (army.containing != null) {

        // first check if it's left it's previous polygon
        // if not, return, if so, remove from previous container
        if (army.containing.polygon != null
                && army.containing.polygon.contains(army.getCenterX(), army.getCenterY())) {
            if (!army.containing.armies.contains(army, true))
                army.containing.armies.add(army);
            return;
        }/*from   ww  w  .  j  a v  a  2  s  .c  o m*/

        army.containing.armies.removeValue(army, true);

        for (Center adjacent : army.containing.neighbors) {
            // checks if in connected (all connected have polygon)
            if (adjacent.polygon != null) {
                Polygon p = adjacent.polygon;
                if (p.contains(army.getCenterX(), army.getCenterY())) {
                    adjacent.armies.add(army);
                    army.containing = adjacent;
                    return;
                }
            }
        }
        army.containing = null;
        //         System.out.println("nothing adjacent found for " + army.getName());
    }
    // container is null (slow during initialization stages - plan is to initialize armies with accurate center)
    for (Center center : map.connected) {
        Polygon p = center.polygon;
        if (p.contains(army.getCenterX(), army.getCenterY())) {
            center.armies.add(army);
            army.containing = center;
            //                  System.out.println("completely differnt polygon");
            return;
        }
    }
}

From source file:org.catrobat.catroid.sensing.CollisionDetection.java

License:Open Source License

public static boolean checkCollisionForPolygonsInPolygons(Polygon[] first, Polygon[] second) {
    for (Polygon firstPolygon : first) {
        int containedIn = 0;
        for (Polygon secondPolygon : second) {
            if (secondPolygon.contains(firstPolygon.getTransformedVertices()[0],
                    firstPolygon.getTransformedVertices()[1])) {
                containedIn++;//from   ww  w.  j  a  v a2  s. c  o  m
            }
        }
        if (containedIn % 2 != 0) {
            return true;
        }
    }

    for (Polygon secondPolygon : second) {
        int containedIn = 0;
        for (Polygon firstPolygon : first) {
            if (firstPolygon.contains(secondPolygon.getTransformedVertices()[0],
                    secondPolygon.getTransformedVertices()[1])) {
                containedIn++;
            }
        }
        if (containedIn % 2 != 0) {
            return true;
        }
    }
    return false;
}

From source file:org.catrobat.catroid.sensing.CollisionDetection.java

License:Open Source License

public static double collidesWithFinger(Look look) {
    /*The touching points are expanded to circles with Constants.COLLISION_WITH_FINGER_TOUCH_RADIUS
    to simulate the real touching area of the finger (which is not only a point, but a small area)
    To improve performance first check if the circle is contained in the bounding box of that polygon
    If that's the case, check if any line segment of the polygon intersects with the circle, to evaluate
    a collision. If there is no intersection, but the circle is contained in an uneven number of polygons
    it means that there still is a collision
    Example:/*from   www.  ja v  a  2s . c om*/
       _______
      |  ___ x|  point "o" is not colliding, while point "x" is colliding
      | | o | |
      | |___| |
      |_______|
    */
    ArrayList<PointF> touchingPoints = TouchUtil.getCurrentTouchingPoints();
    Vector2 start = new Vector2();
    Vector2 end = new Vector2();
    Vector2 center = new Vector2();
    float touchRadius = Constants.COLLISION_WITH_FINGER_TOUCH_RADIUS;

    for (PointF point : touchingPoints) {
        center.set(point.x, point.y);
        int containedIn = 0;
        for (Polygon polygon : look.getCurrentCollisionPolygon()) {
            Rectangle boundingRectangle = polygon.getBoundingRectangle();
            boundingRectangle.x -= touchRadius;
            boundingRectangle.y -= touchRadius;
            boundingRectangle.width += touchRadius * 2;
            boundingRectangle.height += touchRadius * 2;
            if (boundingRectangle.contains(point.x, point.y)) {
                float[] vertices = polygon.getTransformedVertices();
                int f = 0;
                while (f < polygon.getVertices().length - 3) {
                    start.x = vertices[f++];
                    start.y = vertices[f++];
                    end.x = vertices[f++];
                    end.y = vertices[f++];
                    if (Intersector.intersectSegmentCircle(start, end, center, touchRadius * touchRadius)) {
                        return 1d;
                    }
                }
                start.x = vertices[vertices.length - 2];
                start.y = vertices[vertices.length - 1];
                end.x = vertices[0];
                end.y = vertices[1];
                if (Intersector.intersectSegmentCircle(start, end, center, touchRadius * touchRadius)) {
                    return 1d;
                }
                if (polygon.contains(point.x, point.y)) {
                    containedIn++;
                }
            }
        }
        if (containedIn % 2 != 0) {
            return 1d;
        }
    }
    return 0d;
}