# Inserting a vertex into a PShape

**URL:** <https://discourse.processing.org/t/inserting-a-vertex-into-a-pshape/6620>\
**Category:** Coding Questions\
**Created:** [December 15, 2018, 2:59am UTC](https://discourse.processing.org/t/inserting-a-vertex-into-a-pshape/6620 "2018-12-15T02:59:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![William3954](https://avatars.discourse-cdn.com/v4/letter/w/6a8cbe/32.png) [@William3954](https://discourse.processing.org/u/William3954)\
**Post date:** [December 15, 2018, 2:59am UTC](https://discourse.processing.org/t/inserting-a-vertex-into-a-pshape/6620/1 "2018-12-15T02:59:24Z")

</div>

Is it possible to insert a vertex into a PShape at a specific index point? Example, I have a PShape with 10 vertex, I want to add a vertex between v3 and v4 which would increase the PShape vertex count to 11.  
Currently, I am storing the vertex in an arraylist and recreating the PShape when a point needs to be inserted.

---

<div class="post-metadata">

**Author:** ![figraham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/figraham/32/4161_2.png) [@figraham](https://discourse.processing.org/u/figraham)\
**Post date:** [December 15, 2018, 5:24am UTC](https://discourse.processing.org/t/inserting-a-vertex-into-a-pshape/6620/2 "2018-12-15T05:24:24Z")

</div>

Unfortunately adding a vertex after a PShape has been created seems like it’s not possible right now. You can check out the [full javadoc here](http://processing.github.io/processing-javadocs/core/). If you wanted you could create a class that extends PShape and edits the [vertices array](https://github.com/processing/processing/blob/5391a001a0625066182b11618f3e014e482327d7/core/src/processing/core/PShape.java#L199) directly. This is probably more work than it’s worth your solution seems like a great way around the problem. I would probably just take it one step further and wrap the PShape in another class with an ArrayList to make it easier to keep track of. This also has the advantage you don’t have to keep track of when the shape needs to be updated. Here’s my basic idea for a class:

```auto
class PShapeContainer {
  ArrayList<PVector> vertices;
  PShape shape;
  PShapeContainer(ArrayList<PVector> vertices) {
    this.vertices = vertices;
    shape = new PShape();
    updateShape();
  }
  void updateShape() {
    PShape tempShape = new PShape();
    // do your shape creating stuff here
    shape = tempShape;
  }
  PShape getShape() {
    if (shape.getVertexCount() != vertices.size()) updateShape();
    return shape;
  }
  boolean addVertex(int index, PVector vertex) {
    if (index < vertices.size()) {
      vertices.add(index, vertex);
      return true;
    } else {
      return false;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![William3954](https://avatars.discourse-cdn.com/v4/letter/w/6a8cbe/32.png) [@William3954](https://discourse.processing.org/u/William3954)\
**Post date:** [December 15, 2018, 6:22am UTC](https://discourse.processing.org/t/inserting-a-vertex-into-a-pshape/6620/3 "2018-12-15T06:22:55Z")

</div>

Thanks for the reply and the concept sketch. Had already started to implement a solution close to what you proposed so I think I am headed in the right direction. Thanks again.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [December 17, 2018, 8:51pm UTC](https://discourse.processing.org/t/inserting-a-vertex-into-a-pshape/6620/4 "2018-12-17T20:51:52Z")

</div>

Old related discussion: [https://forum.processing.org/two/discussion/18064/remove-a-vertex-from-pshape](https://forum.processing.org/two/discussion/18064/remove-a-vertex-from-pshape)
