I'm having problem with the boolean divide(int depth)
method in the Triangle
class I created, the code is as follows. Other parts have passed the test, but because of the boolean divide(int depth)
, this programming throws a java.lang.NullPointerException
.
I've been working on it for a long time but I still don't know what's wrong with the recursion. Both boolean divide()
and boolean divide(int depth)
don't take any argument, this makes it so difficult for me to write the recursion. Any hint will be appreciated, thanks a lot.
boolean divide(int depth)
overwrites boolean divide()
, boolean divide()
divides the triangle into three sub triangles, it creates three Vertex2D
on each edge of a triangle and the sub-triangles are stored in the Triangle
class in an array as an attribute.
public class Triangle {
private static final int NUM_OF_SUB_TRIANGLES = 3;
private final Vertex2D[] vertices = new Vertex2D[3];
private final Triangle[] subTriangles = new
Triangle[NUM_OF_SUB_TRIANGLES];
public Triangle(Vertex2D v1, Vertex2D v2, Vertex2D v3){
vertices[0] = v1;
vertices[1] = v2;
vertices[2] = v3;
}
// Method returns a vertex of the triangle according to input index
public Vertex2D getVertex(int index) {
for (int i = 0; i < 3; i++) {
if (index == i) {
return vertices[i];
}
}
return null;
}
public boolean isEquilateral(){
double side1 = vertices[0].distance(vertices[1]);
double side2 = vertices[0].distance(vertices[2]);
double side3 = vertices[1].distance(vertices[2]);
if(Math.abs(side1-side2) < 0.001 && Math.abs(side1-side3) < 0.001 && Math.abs(side2-side3) < 0.001){
return true;
}else{
return false;
}
}
// Method outputs the coordinates of threes vertices of the triangle
// edit this one
public String toString(){
String s = "Triangle: vertices=" + vertices[0]
+ " [" + vertices[1].getX() + ", " + vertices[1].getY() + "] " + "[" + vertices[2].getX() + ", " + vertices[2].getY()
+ "]";
return s;
}
// Method checks if the three sub triangles are created
public boolean isDivided(){
for(int i = 0 ; i < NUM_OF_SUB_TRIANGLES; i ++){
if(subTriangles[i] == null){
return false;
}
}
return true;
}
// Method returns a sub triangle according to the input index
public Triangle getSubTriangle(int index){
for (int i = 0; i < 3; i++) {
if (index == i) {
return subTriangles[i];
}
}
return null;
}
// Method divides the triangle into three sub triangles
public boolean divide(){
if (isDivided()){
return false;
}else{
// Find vertices of the three sub triangles
Triangle t1 = new Triangle(vertices[0], vertices[0].createMiddle(vertices[1]), vertices[0].createMiddle(vertices[2]));
Triangle t2 = new Triangle(vertices[0].createMiddle(vertices[1]), vertices[1], vertices[1].createMiddle(vertices[2]));
Triangle t3 = new Triangle(vertices[0].createMiddle(vertices[2]), vertices[1].createMiddle(vertices[2]), vertices[2]);
subTriangles[0] = t1;
subTriangles[1] = t2;
subTriangles[2] = t3;
return true;
}
}
public boolean divide(int depth){
if(depth <= 0){
return false;
}else{
while(depth > 0) {
if (!isDivided()) {
divide();
} else {
depth -= 1;
for (int i = 0; i < 3; i++) {
getSubTriangle(i).divide(depth);
}
depth -= 1;
}
}
return true;
}
}
}