Example usage for com.badlogic.gdx.utils Array ensureCapacity

List of usage examples for com.badlogic.gdx.utils Array ensureCapacity

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils Array ensureCapacity.

Prototype

public T[] ensureCapacity(int additionalCapacity) 

Source Link

Document

Increases the size of the backing array to accommodate the specified number of additional items.

Usage

From source file:com.agateau.pixelwheels.map.LapPositionTableIO.java

License:Open Source License

public static LapPositionTable load(TiledMap map) {
    MapLayer layer = map.getLayers().get("Sections");
    Assert.check(layer != null, "No 'Sections' layer found");
    MapObjects objects = layer.getObjects();

    Array<Line> lines = new Array<Line>();
    lines.ensureCapacity(objects.getCount());
    Set<String> names = new HashSet<String>();
    for (MapObject obj : objects) {
        String name = obj.getName();
        Assert.check(!name.isEmpty(), "Section line is missing a name");
        Assert.check(!names.contains(name), "Duplicate section line " + name);
        names.add(name);//from   w w  w.jav a  2s .c om

        float order;
        try {
            order = Float.parseFloat(name);
        } catch (NumberFormatException e) {
            throw new RuntimeException("Invalid section name " + name);
        }
        Assert.check(obj instanceof PolylineMapObject,
                "'Sections' layer should only contain PolylineMapObjects");
        Polyline polyline = ((PolylineMapObject) obj).getPolyline();
        float[] vertices = polyline.getTransformedVertices();
        Assert.check(vertices.length == 4,
                "Polyline with name " + order + "should have 2 points, not " + (vertices.length / 2));
        Line line = new Line();
        line.x1 = vertices[0];
        line.y1 = vertices[1];
        line.x2 = vertices[2];
        line.y2 = vertices[3];
        line.order = order;
        lines.add(line);
    }
    lines.sort();

    LapPositionTable table = new LapPositionTable();
    for (int idx = 0; idx < lines.size; ++idx) {
        Line line1 = lines.get(idx);
        Line line2 = lines.get((idx + 1) % lines.size);
        float[] vertices = { line1.x1, line1.y1, line2.x1, line2.y1, line2.x2, line2.y2, line1.x2, line1.y2 };
        Polygon polygon = new Polygon(vertices);
        table.addSection(idx, polygon);
    }
    return table;
}

From source file:com.andgate.ikou.model.MasterSector.java

License:Open Source License

private TileSector ensureSectorColumn(Array<TileSector> sectorsRow, int columnIndex) {
    int columnCount = columnIndex + 1;
    if (columnCount > sectorsRow.size) {
        int distance = columnCount - sectorsRow.size;
        sectorsRow.ensureCapacity(distance);

        while (distance > 0) {
            sectorsRow.add(null);/*from  w w w .  j av a 2  s.com*/
            distance--;
        }
    }

    TileSector sector = sectorsRow.get(columnIndex);
    if (sector == null) {
        sector = new TileSector();
        sectorsRow.set(columnIndex, sector);
    }

    return sector;
}

From source file:com.infunity.isometricgame.shared.utils.dermetfan.GeometryUtils.java

License:Apache License

/** @see #intersectSegments(float, float, float, float, float[], boolean, com.badlogic.gdx.utils.FloatArray) */
public static void intersectSegments(Vector2 a, Vector2 b, float[] segments, boolean polygon,
        Array<Vector2> intersections) {
    FloatArray fa = Pools.obtain(FloatArray.class);
    intersectSegments(a.x, a.y, b.x, b.y, segments, polygon, fa);
    if (fa.size < 1) {
        intersections.clear();//from   w ww .j  a v a2 s . co m
        Pools.free(fa);
        return;
    }
    intersections.ensureCapacity(fa.size / 2 - intersections.size);
    for (int i = 1; i < fa.size; i += 2)
        if (intersections.size > i / 2)
            intersections.get(i / 2).set(fa.get(i - 1), fa.get(i));
        else
            intersections.add(new Vector2(fa.get(i - 1), fa.get(i)));
    Pools.free(fa);
}

From source file:com.kotcrab.vis.editor.module.physicseditor.util.trace.TextureConverter.java

License:Apache License

private static Array<Array<Vector2>> createPolygon(PolygonCreationAssistance pca) throws Exception {
    Array<Array<Vector2>> polygons = new Array<Array<Vector2>>();
    Array<Vector2> polygon;
    Array<Vector2> holePolygon;
    Vector2 holeEntrance = null;/*from   w w  w .  j  ava 2 s .  c  o m*/
    Vector2 polygonEntrance = null;
    Array<Vector2> blackList = new Array<Vector2>();
    // First of all: Check the array you just got.
    if (pca.IsValid()) {
        boolean searchOn;
        do {
            if (polygons.size == 0) {
                polygon = CreateSimplePolygon(pca, new Vector2(), new Vector2());
                if (polygon != null && polygon.size > 2) {
                    polygonEntrance = GetTopMostVertex(polygon);
                }
            } else if (polygonEntrance != null) {
                polygon = CreateSimplePolygon(pca, polygonEntrance,
                        new Vector2(polygonEntrance.x - 1f, polygonEntrance.y));
            } else {
                break;
            }
            searchOn = false;
            if (polygon != null && polygon.size > 2) {
                if (pca.HoleDetection) {
                    do {
                        holeEntrance = GetHoleHullEntrance(pca, polygon, holeEntrance);
                        if (holeEntrance != null) {
                            if (!vectorListContains(blackList, holeEntrance)) {
                                blackList.add(holeEntrance);
                                holePolygon = CreateSimplePolygon(pca, holeEntrance,
                                        new Vector2(holeEntrance.x + 1, holeEntrance.y));
                                if (holePolygon != null && holePolygon.size > 2) {
                                    holePolygon.add(holePolygon.get(0));
                                    Reference<Integer> vertex2IndexRef = new Reference<Integer>(0);
                                    Reference<Integer> vertex1IndexRef = new Reference<Integer>(0);
                                    if (SplitPolygonEdge(polygon, EdgeAlignment.Vertical, holeEntrance,
                                            vertex1IndexRef, vertex2IndexRef)) {

                                        polygon.ensureCapacity(holePolygon.size);
                                        for (int i = holePolygon.size - 1; i <= 0; i--) {
                                            polygon.insert(vertex2IndexRef.v, holePolygon.get(i));
                                        }
                                    }
                                }
                            } else {
                                break;
                            }
                        } else {
                            break;
                        }
                    } while (true);
                }
                polygons.add(polygon);
                if (pca.MultipartDetection) {
                    // 1: 95 / 151
                    // 2: 232 / 252
                    //
                    polygonEntrance = new Vector2();
                    while (GetNextHullEntrance(pca, polygonEntrance, polygonEntrance)) {
                        boolean inPolygon = false;
                        for (int i = 0; i < polygons.size; i++) {
                            polygon = polygons.get(i);
                            if (InPolygon(pca, polygon, polygonEntrance)) {
                                inPolygon = true;
                                break;
                            }
                        }
                        if (!inPolygon) {
                            searchOn = true;
                            break;
                        }
                    }
                }
            }
        } while (searchOn);
    } else {
        throw new Exception(
                "Sizes don't match: Color array must contain texture width * texture height elements.");
    }
    return polygons;
}

From source file:com.kotcrab.vis.editor.serializer.ArraySerializer.java

License:Apache License

@Override
public Array read(Kryo kryo, Input input, Class<Array> type) {
    Array array = new Array();
    kryo.reference(array);//from   www  .  j av  a 2 s  .  com
    int length = input.readInt(true);
    array.ensureCapacity(length);
    if (genericType != null) {
        Class elementClass = genericType;
        Serializer serializer = kryo.getSerializer(genericType);
        genericType = null;
        for (int i = 0; i < length; i++)
            array.add(kryo.readObjectOrNull(input, elementClass, serializer));
    } else {
        for (int i = 0; i < length; i++)
            array.add(kryo.readClassAndObject(input));
    }
    return array;
}

From source file:com.torrosoft.sopistan.gui.graphics.DrawResolver.java

License:Open Source License

/**
 * TODO doc smooth/*from  w  w w. ja va2  s. c  o  m*/
 * 
 * @param input
 * @param output
 */
private static void smooth(Array<Vector2> input, Array<Vector2> output) {
    // expected size
    output.clear();
    output.ensureCapacity(input.size * 2);

    // first element
    output.add(input.get(0));

    // average elements
    for (int i = 0; i < input.size - 1; i++) {
        Vector2 p0 = input.get(i);
        Vector2 p1 = input.get(i + 1);

        Vector2 Q = new Vector2(0.75f * p0.x + 0.25f * p1.x, 0.75f * p0.y + 0.25f * p1.y);
        Vector2 R = new Vector2(0.25f * p0.x + 0.75f * p1.x, 0.25f * p0.y + 0.75f * p1.y);
        output.add(Q);
        output.add(R);
    }

    // last element
    output.add(input.get(input.size - 1));
}