d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With A Sheep Counting Method > Java And Java Fx
Add Reply New Topic New Poll
Member
Posts: 4,095
Joined: Apr 24 2011
Gold: 0.25
Apr 21 2018 05:28pm
Hi, I need help with an method in my little project. I've tried to create a method that counts white sheep from an image and displays the number I trued using union. But something is not working right, does anyone see if I'm doing something wrong?


Code
public void countSheep() {
ArrayList<Integer> sheepCounter = new ArrayList<>();
for (int i = 0; i <imageArray.length; i++) {
//Union find
if (imageArray[i] > 0 && imageArray[i+1] > 0 || imageArray[i] > 0 && imageArray[i + pic.getWidth()] > 0) {
if (imageArray[i] > 0 && imageArray[i + 1] > 0) {
union(imageArray, imageArray[i], i + 1);
}
System.out.println("The root of "+ i +" is " + find(imageArray, i));
if (imageArray[i] > 0 && imageArray[i + pic.getWidth()] > 0) {
union(imageArray, imageArray[i], i + pic.getWidth());
}
}
if(imageArray[i] > 0)
System.out.println("The root of " + i + " is " + find(imageArray, i));
}

int rootNode;
for (int nodes : imageArray) {
rootNode = find(imageArray, nodes);
if((!sheepCounter.contains(rootNode)) && (rootNode != 0)) {
sheepCounter.add(rootNode);
}
}
System.out.println(sheepCounter.size());
sheepEstimate.setText("Estimate of sheep = " + Integer.toString(sheepCounter.size()));
sheepEstimate.setVisible(true);
}


Regards,

Marcel
Member
Posts: 4,095
Joined: Apr 24 2011
Gold: 0.25
Apr 21 2018 06:21pm
nvm, topic can be closed.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll