Complicated ArrayList structure problem
in
Programming Questions
•
1 year ago
I am trying to set up a structure that will let me add or remove polygons from a future geometry sketch. I also want to be able to change the number of vertices for a polygon and the vertices' positions. I have never used an ArrayList of ArrayLists and can't see what is wrong with the last line in setup() (highlighted in red). This structure seems a bit complicated, if there is a suggestion of a better way to do it I am willing to change the structure.
- ArrayList<ArrayList> polygons = new ArrayList<ArrayList>();
- void setup() {
- size(800, 800);
- // -------------------------------------------------------------------
- // Structure of polygons (ArrayList)
- // 0 = Polygon x position (Float)
- // 1 = Polygon y position (Float)
- // 2 = Vertices (ArrayList)
- // - 2.0 = Number of vertices (Integer)
- // - 2.1 = Vertices x position (Float)
- // - 2.2 = Vertices y position (Float)
- // -------------------------------------------------------------------
- polygons.add(new ArrayList<Float>()); // X position
- polygons.add(new ArrayList<Float>()); // Y position
- polygons.add(new ArrayList<ArrayList>()); // Vertices
- polygons.get(2).add(new ArrayList<Integer>()); // Number of vertices
- polygons.get(2).add(new ArrayList<Float>()); // Vertices x position
- polygons.get(2).add(new ArrayList<Float>()); // Vertices y position
- // Initialize the first element of polygons
- polygons.get(0).add(width/2);
- polygons.get(1).add(height/2);
- // Why doesn't this work?
- // I want it to be a triangle later so it should have 3 vertices
- polygons.get(2).get(0).add(3);
- }
- void draw() {
- }
1