So I'm trying to create a program that makes a fractal tree. This is my code for the branch (and trunk):
this is my world class:
I'm using danpost's coordinate translation classes from this post
When it creates the branch, it only creates the trunk that's created in the Background. I tried experimenting around to see if there was something wrong with the values of where it was trying to put the new branches by just drawing a string of them but they didn't show up. I tried just drawing the value of thisLength and nothing showed up. I don't think the loop is running. Can someone help I don't know why it isn't working.
EDIT:
I think the cause is where is checks if getWorld() != null. I tried just making it write a string instead of branching to test and it didn't write, to i changed it to == and it wrote the string. however, when its set to ==, it won't branch because "Actor not in world". How else do I test if an actor is in the world?
public class Branch extends TransActor { public Branch(int angleL, int angleR, float scaleFactor) { if (getWorld() != null) { while (getImage().getHeight()>2 && getImage().getWidth()>2){ int thisLength = getImage().getHeight(); int nextLength = Math.round(scaleFactor * thisLength); int endX = (int)Math.round(getX()+((thisLength/2)*Math.cos(getRotation()))); int endY = (int)Math.round(getY()+((thisLength/2)*Math.sin(getRotation()))); int nextXR = (int)Math.round(endX+((nextLength/2)*Math.cos(getRotation()+angleR))); int nextYR = (int)Math.round(endY+((nextLength/2)*Math.cos(getRotation()+angleR))); int nextXL = (int)Math.round(endX+((nextLength/2)*Math.cos(getRotation()-angleL))); int nextYL = (int)Math.round(endY+((nextLength/2)*Math.cos(getRotation()-angleL))); GreenfootImage img = getImage(); //img.scale((int)Math.round(img.getWidth() * scaleFactor), (int)Math.round(img.getHeight() * scaleFactor)); img.setColor(Color.BLACK); img.drawString(""+thisLength, 10, 10); setImage(img); getWorld().addObject(new Branch(angleL, angleR, scaleFactor),nextXR,nextYR); getWorld().addObject(new Branch(angleL, angleR, scaleFactor),nextXL,nextYL); } } } }
public class Background extends TransWorld { int angleL = 30; int angleR = 30; public Background() { super(1200, 900, 1); setTranslation(getWidth()/2, 1, getHeight(), -1); prepare(); } private void prepare() { addObject(new Branch(angleL, angleR, 0.6666667f),0,200); } }