Why is my Ico-Sphere not a Sphere

please format code with </> button * homework policy * asking questions
<public class isoSphere
{

Icosehedron ico = new Icosehedron();

ArrayList vNew;
ArrayList cul;
ArrayList vecL = new ArrayList();
ArrayList drawOrder = new ArrayList();

public PVector MiddlePoint(PVector p1, PVector p2)
{

if (vecL.indexOf(p1) > vecL.indexOf(p2)){
return new PVector((p1.x + p2.x) / 2.0, (p1.y + p2.y) / 2.0, (p1.z + p2.z) / 2.0);
}

else{
return new PVector((p2.x + p1.x) / 2.0, (p2.y + p1.y) / 2.0, (p2.z + p1.z) / 2.0);
}

}

public isoSphere(int times)
{

vecL = ico.vec;
drawOrder = ico.faceI;

for (int t = 0; t < times; t++)
{

  vNew = new ArrayList<PVector>();
  cul = new ArrayList<Integer>();
  
for (int v = 0; v < drawOrder.size(); v++)
{
  
  PVector a = vecL.get(drawOrder.get(v));
  v++;
  PVector b = vecL.get(drawOrder.get(v));
  v++;
  PVector c = vecL.get(drawOrder.get(v));
  
  PVector nav = this.MiddlePoint(a, b);
  PVector nbv = this.MiddlePoint(b, c);
  PVector ncv = this.MiddlePoint(c, a);
  
  boolean vecA = false;
  boolean vecB = false;
  boolean vecC = false;
  boolean vecAl = false;
  boolean vecnB = false;
  boolean vecCl = false;
  
  
  for (int bud = 0; bud < vNew.size(); bud++)
  {
    
    if ( a == vNew.get(bud))
    {
      vecA = true;
    }
     else if ( b == vNew.get(bud))
    {
      vecB = true;
    }
     else if ( c == vNew.get(bud))
    {
      vecC = true;
    }
     else if ( nav == vNew.get(bud))
    {
      vecAl = true;
    }
     else if ( nbv == vNew.get(bud))
    {
      vecnB = true;
    }
     else if ( ncv == vNew.get(bud))
    {
      vecCl = true;
    }
    
  }
  
  if(vecA != true)
  {
    vNew.add(a);
  }
   if(vecAl != true)
  {
    vNew.add(nav);
  }
   if(vecB != true)
  {
    vNew.add(b);
  }
   if(vecnB != true)
  {
    vNew.add(nbv);
  }
   if(vecC != true)
  {
    vNew.add(c);
  }
   if(vecCl != true)
  {
    vNew.add(ncv);
  }
    
    int aint = vNew.indexOf(a); int bint = vNew.indexOf(b);
    int cint = vNew.indexOf(c); int navint = vNew.indexOf(nav);
    int nbvint = vNew.indexOf(nbv); int ncvint = vNew.indexOf(ncv);

       cul.add(aint); cul.add(navint); cul.add(ncvint);  
       cul.add(bint); cul.add(nbvint); cul.add(navint);  
       cul.add(cint); cul.add(ncvint); cul.add(nbvint);  
       cul.add(navint); cul.add(nbvint); cul.add(ncvint);  
    
}

 vecL = vNew;
drawOrder =  cul;

}

}

public PVector getVertex(int i) { return vecL.get(i); }

public int getFace(int i) {return drawOrder.get(i); }

public void drawVertex()
{

for (int f = 0; f < vecL.size(); f++)
{

 PVector v = this.getVertex(f);
 
 if (f <= 3)
 {
   
   stroke(255, 0, 0);
 
 }
 
 else if (f >= 8)
 {
   
   stroke(0, 0, 255);
   
 }
 
 else
 {
   
   stroke(0, 255, 0);
   
 }
 
  strokeWeight(20);
  
  point(v.x, v.y, v.z);
  
}

}

public void drawLines()
{

strokeWeight(4);

for(int i = 0; i < vecL.size(); i ++)
{

PVector l1 = this.getVertex(i);
i++;
PVector l2 = this.getVertex(i);
i++;
PVector l3 = this.getVertex(i);
i++;
PVector l4 = this.getVertex(i);

stroke(255, 0, 255);
line(l1.x, l1.y, l1.z, l2.x, l2.y, l2.z);
stroke(255, 0, 0);
line(l2.x, l2.y, l2.z, l3.x, l3.y, l3.z);
stroke(0, 255, 0);
line(l3.x, l3.y, l3.z, l4.x, l4.y, l4.z);
stroke(0, 0, 255);
line(l4.x, l4.y, l4.z, l1.x, l1.y, l1.z);

}

}

public void drawTriangles()
{

strokeWeight(4);

for (int i = 0; i < drawOrder.size(); i++)
{
   
  PVector v1 = this.getVertex(this.getFace(i));
  i++;
  PVector v2 = this.getVertex(this.getFace(i));
  i++;
  PVector v3 = this.getVertex(this.getFace(i));
   
  stroke(255, 0, 0);
  line(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z);
   
  stroke(0, 255, 0);
  line(v2.x, v2.y, v2.z, v3.x, v3.y, v3.z);
   
  stroke(0, 0, 255);
  line(v3.x, v3.y, v3.z, v1.x, v1.y, v1.z);
  
}

}

} />

[Hello I am kind of new to processing 3D, and mediocre at Java. This little snippet of code is suppose to take the standard icosehedron vectors and turn them into a sphere through subdivision after many painful weeks of converting each part of the common code I found on the internet Im getting the subdivisions and drawable geometry but it is most definitely not a sphere. the vectors of the icosehedron go as follows {
float g = (float)(1.0 + Math.sqrt(5.0)) / 2.0;
float v = 200.0;
float t = g * 200;

PVector x1 = new PVector(v,  t,  0);
PVector x2 = new PVector(v,  -t,  0);
PVector x3 = new PVector(-v,  -t,  0); 
PVector x4 = new PVector(-v,  t,  0); 
 
PVector y1 = new PVector(0,  v,  t);
PVector y2 = new PVector(0,  v,  -t);
PVector y3 = new PVector(0,  -v,  -t);
PVector y4 = new PVector(0,  -v,  t);

PVector z1 = new PVector(t,  0, -v); 
PVector z2 = new PVector(-t,  0, -v); 
PVector z3 = new PVector(-t,  0, v); 
PVector z4 = new PVector(t,  0, v);

faceI.add(0);  faceI.add(3);  faceI.add(4);  faceI.add(4);
faceI.add(11);  faceI.add(0);  faceI.add(0);  faceI.add(11);
faceI.add(8);  faceI.add(8);  faceI.add(5);  faceI.add(0);
faceI.add(0);  faceI.add(5);  faceI.add(3);  faceI.add(3);
faceI.add(9);  faceI.add(10);  faceI.add(3);  faceI.add(10);
faceI.add(4);  faceI.add(4);  faceI.add(10);  faceI.add(7);
faceI.add(7);  faceI.add(11);  faceI.add(4);  faceI.add(3);
faceI.add(5);  faceI.add(9);  faceI.add(5);  faceI.add(9);
faceI.add(6);  faceI.add(6);  faceI.add(5);  faceI.add(8);
faceI.add(6);  faceI.add(1);  faceI.add(8);  faceI.add(1);
faceI.add(8);  faceI.add(11);  faceI.add(11);  faceI.add(7);
faceI.add(1);  faceI.add(1);  faceI.add(2);  faceI.add(6);
faceI.add(6);  faceI.add(2);  faceI.add(9);  faceI.add(9);
faceI.add(2);  faceI.add(10);  faceI.add(10);  faceI.add(2);
faceI.add(7);  faceI.add(7);  faceI.add(2);  faceI.add(1);

}

I had to draw each line of the triangle by hand, which i sketched out on a notepad. and plug them in vertex for vertex which I then iterated thought two at a time which you can see in the draw triangles function. I tried using P3D's begin shape method but it would only draw the bottom after that it became a distorted mess. Like I said I'm new to processing and my java experience boils down to my experience with android studio and aide. which I'm fair at but there are still a lot of things to learn. I have advanced from beginner to intermediate. I know 10 thousand things I didn't know about code when I started but don't fully understand all of it just yet, mostly in syntax, math is pretty straight forward. But if anybody could help me figure out why I am not getting a sphere, and perhaps a bit of code that can shorten this mess up a little. I was trying to do it mostly in one class it started to get messy. I did try adjusting the value which i divide each value in the vector by in the method MiddlePoint. finding that anything below 1.5 returns some rather "interesting" results with anything greater than 2 being the inverse of that. I can get a single subdivision to return a mesh resembling a sphere with an increase of .1 for each sub division but once you go past 2 there is no return. as far as I can see this code should work like a charm minus the various work arounds I managed to make work. which does drastically effect performance as I can only get about 5 subdivisions before it crashes.]

I had to draw each line of the triangle by hand, which i sketched out on a notepad. and plug them in vertex for vertex which I then iterated thought two at a time which you can see in the draw triangles function. I tried using P3D’s begin shape method but it would only draw the bottom after that it became a distorted mess. Like I said I’m new to processing and my java experience boils down to my experience with android studio and aide. which I’m fair at but there are still a lot of things to learn. I have advanced from beginner to intermediate. I know 10 thousand things I didn’t know about code when I started but don’t fully understand all of it just yet, mostly in syntax, math is pretty straight forward. But if anybody could help me figure out why I am not getting a sphere, and perhaps a bit of code that can shorten this mess up a little. I was trying to do it mostly in one class it started to get messy. I did try adjusting the value which i divide each value in the vector by in the method MiddlePoint. finding that anything below 1.5 returns some rather “interesting” results with anything greater than 2 being the inverse of that. I can get a single subdivision to return a mesh resembling a sphere with an increase of .1 for each sub division but once you go past 2 there is no return. as far as I can see this code should work like a charm minus the various work arounds I managed to make work. which does drastically effect performance as I can only get about 5 subdivisions before it crashes.]

please go back to your initial post and edit it.

Please select the Code with the mouse and click the small </> sign in the small Command Bar.

Also, I couldn’t run your code because Icosehedron class was missing

and setup and draw, please post a runable version

This how the formatting should look like please:



class isoSphere
{

  Icosehedron ico = new Icosehedron();

  ArrayList vNew;
  ArrayList cul;
  ArrayList vecL = new ArrayList();
  ArrayList drawOrder = new ArrayList();

  public PVector MiddlePoint(PVector p1, PVector p2)
  {

    if (vecL.indexOf(p1) > vecL.indexOf(p2)) {
      return new PVector((p1.x + p2.x) / 2.0, (p1.y + p2.y) / 2.0, (p1.z + p2.z) / 2.0);
    } else {
      return new PVector((p2.x + p1.x) / 2.0, (p2.y + p1.y) / 2.0, (p2.z + p1.z) / 2.0);
    }
  }

  public isoSphere(int times)
  {

    vecL = ico.vec;
    drawOrder = ico.faceI;

    for (int t = 0; t < times; t++)
    {

      vNew = new ArrayList<PVector>();
      cul = new ArrayList<Integer>();

      for (int v = 0; v < drawOrder.size(); v++)
      {

        PVector a = vecL.get(drawOrder.get(v));
        v++;
        PVector b = vecL.get(drawOrder.get(v));
        v++;
        PVector c = vecL.get(drawOrder.get(v));

        PVector nav = this.MiddlePoint(a, b);
        PVector nbv = this.MiddlePoint(b, c);
        PVector ncv = this.MiddlePoint(c, a);

        boolean vecA = false;
        boolean vecB = false;
        boolean vecC = false;
        boolean vecAl = false;
        boolean vecnB = false;
        boolean vecCl = false;


        for (int bud = 0; bud < vNew.size(); bud++)
        {

          if ( a == vNew.get(bud))
          {
            vecA = true;
          } else if ( b == vNew.get(bud))
          {
            vecB = true;
          } else if ( c == vNew.get(bud))
          {
            vecC = true;
          } else if ( nav == vNew.get(bud))
          {
            vecAl = true;
          } else if ( nbv == vNew.get(bud))
          {
            vecnB = true;
          } else if ( ncv == vNew.get(bud))
          {
            vecCl = true;
          }
        }

        if (vecA != true)
        {
          vNew.add(a);
        }
        if (vecAl != true)
        {
          vNew.add(nav);
        }
        if (vecB != true)
        {
          vNew.add(b);
        }
        if (vecnB != true)
        {
          vNew.add(nbv);
        }
        if (vecC != true)
        {
          vNew.add(c);
        }
        if (vecCl != true)
        {
          vNew.add(ncv);
        }

        int aint = vNew.indexOf(a); 
        int bint = vNew.indexOf(b);
        int cint = vNew.indexOf(c); 
        int navint = vNew.indexOf(nav);
        int nbvint = vNew.indexOf(nbv); 
        int ncvint = vNew.indexOf(ncv);

        cul.add(aint); 
        cul.add(navint); 
        cul.add(ncvint);  
        cul.add(bint); 
        cul.add(nbvint); 
        cul.add(navint);  
        cul.add(cint); 
        cul.add(ncvint); 
        cul.add(nbvint);  
        cul.add(navint); 
        cul.add(nbvint); 
        cul.add(ncvint);
      }

      vecL = vNew;
      drawOrder =  cul;
    }
  }

  public PVector getVertex(int i) { 
    return vecL.get(i);
  }

  public int getFace(int i) {
    return drawOrder.get(i);
  }

  public void drawVertex()
  {

    for (int f = 0; f < vecL.size(); f++)
    {

      PVector v = this.getVertex(f);

      if (f <= 3)
      {

        stroke(255, 0, 0);
      } else if (f >= 8)
      {

        stroke(0, 0, 255);
      } else
      {

        stroke(0, 255, 0);
      }

      strokeWeight(20);

      point(v.x, v.y, v.z);
    }
  }

  public void drawLines()
  {

    strokeWeight(4);

    for (int i = 0; i < vecL.size(); i ++)
    {

      PVector l1 = this.getVertex(i);
      i++;
      PVector l2 = this.getVertex(i);
      i++;
      PVector l3 = this.getVertex(i);
      i++;
      PVector l4 = this.getVertex(i);

      stroke(255, 0, 255);
      line(l1.x, l1.y, l1.z, l2.x, l2.y, l2.z);
      stroke(255, 0, 0);
      line(l2.x, l2.y, l2.z, l3.x, l3.y, l3.z);
      stroke(0, 255, 0);
      line(l3.x, l3.y, l3.z, l4.x, l4.y, l4.z);
      stroke(0, 0, 255);
      line(l4.x, l4.y, l4.z, l1.x, l1.y, l1.z);
    }
  }

  public void drawTriangles()
  {

    strokeWeight(4);

    for (int i = 0; i < drawOrder.size(); i++)
    {

      PVector v1 = this.getVertex(this.getFace(i));
      i++;
      PVector v2 = this.getVertex(this.getFace(i));
      i++;
      PVector v3 = this.getVertex(this.getFace(i));

      stroke(255, 0, 0);
      line(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z);

      stroke(0, 255, 0);
      line(v2.x, v2.y, v2.z, v3.x, v3.y, v3.z);

      stroke(0, 0, 255);
      line(v3.x, v3.y, v3.z, v1.x, v1.y, v1.z);
    }
  }
} 
//