So I was doing a program there's supposed to verify if when cut, a triangle can produce two new triangles, I did all the logic and the code works just as it is supposed to, however as I was doing I noticed that I had to apply the same logic for different attributes over and over again, which seemed highly unproductive. Here's the code:
public static class Triangle{
private int[] x = new int[3];
private int[] y = new int[3];
public int getX(int position) {
return x[position];
}
public int getY(int position) {
return y[position];
}
//Get the information regarding the points that make the triangle
public void setDomains(Scanner readVar){
readVar.nextLine();
this.x[0] = readVar.nextInt();
this.y[0] = readVar.nextInt();
this.x[1] = readVar.nextInt();
this.y[1] = readVar.nextInt();
this.x[2] = readVar.nextInt();
this.y[2] = readVar.nextInt();
}
//Verify if one of the other points is lower or higher than the present one
public boolean areDiffX(int i, int j, boolean isLower){
if(isLower){
if(this.getX(i) < this.getX(j)){
return true;
};
}else{
if(this.getX(i) > this.getX(j)){
return true;
};
}
return false;
}
//Verify if one of the other points is lower or higher than the present one
public boolean areDiffY(int i, int j, boolean isLower){
if(isLower){
if(this.getY(i) < this.getY(j)){
return true;
};
}else{
if(this.getY(i) > this.getY(j)){
return true;
};
}
return false;
}
//Verify if from the X axes two new triangles can ensurge
public boolean compareTriangleDomain(int i, int j, int k){
boolean hasDomain = false;
boolean hasCodomain = false;
hasDomain = areDiffX(i, j, true);
hasCodomain = areDiffX(i, k, false);
if(hasDomain == true && hasCodomain == true){
return true;
}
hasDomain = areDiffX(i, j, false);
hasCodomain = areDiffX(i, k, true);
if(hasDomain == true && hasCodomain == true){
return true;
}
return false;
}
//Verify if from the Y axes two new triangles can ensurge
public boolean compareTriangleCodomain(int i, int j, int k){
boolean hasDomain = false;
boolean hasCodomain = false;
hasDomain = areDiffY(i, j, true);
hasCodomain = areDiffY(i, k, false);
if(hasDomain == true && hasCodomain == true){
return true;
}
hasDomain = areDiffY(i, j, false);
hasCodomain = areDiffY(i, k, true);
if(hasDomain == true && hasCodomain == true){
return true;
}
return false;
}
//Verify if from a certain point of a triangle can be produced two new triangles
public boolean cutTriangle(int i, int j, int k){
boolean halveTriangleX = compareTriangleDomain(i, j, k);
boolean halveTriangleY = compareTriangleCodomain(i, j, k);
if(halveTriangleX || halveTriangleY){
return true;
}
return false;
}
//Verify if a triangle can produce two new triangles
public void checkCut(){
boolean canCut = false;
boolean hasntCut = true;
canCut = cutTriangle(0, 1, 2);
if(canCut == true && hasntCut == true){
System.out.println("YES");
hasntCut = false;
}
canCut = cutTriangle(1, 0, 2);
if(canCut == true && hasntCut == true){
System.out.println("YES");
hasntCut = false;
}
canCut = cutTriangle(2, 1, 0);
if(canCut == true && hasntCut == true){
System.out.println("YES");
}
else if(hasntCut == true){
System.out.println("NO");
}
}
}
I thought that maybe I could add a new parameter to the get function so to specify which axes I'm working with, but I don't know if this a dumb way to do it, any recommendations and advices are welcome I'm still a baby in java and coding in general.
There are a few ways to do this. I would go with simply writing a function which takes the values instead of the indices, and then two new functions which get the values:
and then:
and a similar
areDiffYfunction.You can rewrite
compareTriangleDomainto take values and useareDiffand dispense withcompareTriangleCodomainandareDiffX/Y.