This site requires JavaScript, please enable it in your browser!
Greenfoot back
TheGoldenProof
TheGoldenProof wrote ...

2018/8/14

My tree fractaler isn't fractaling

TheGoldenProof TheGoldenProof

2018/8/14

#
So I'm trying to create a program that makes a fractal tree. This is my code for the branch (and trunk):
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);
            }
        }
    }
}
this is my world class:
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);
    }
}
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?
TheGoldenProof TheGoldenProof

2018/8/14

#
I changed my code to this:
public class Branch extends TransActor
{
    int angleL, angleR;
    float scaleFactor;
    int o = 0;
    public Branch(int angleLc, int angleRc, float scaleFactorc, int width, int height) {
        angleL = angleLc;
        angleR = angleRc;
        scaleFactor = scaleFactorc;
        GreenfootImage img = getImage();
        img.scale(width, height);
        setImage(img);
    }
    public void act() {
        if (o == 0) {
            o++;
            while (getImage().getHeight()>20 && getImage().getWidth()>20){
                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,getImage().getWidth(), getImage().getHeight()),nextXR,nextYR);
                getWorld().addObject(new Branch(angleL, angleR, scaleFactor,getImage().getWidth(), getImage().getHeight()),nextXL,nextYL);
            }
        }
    }
}
creating the trunk:
private void prepare() {
        addObject(new Branch(angleL, angleR, 0.6666667f, 75, 600),0,200);
}
It acts and tries to fractal. It fails miserably. I don't even know how to describe what it's doing. before I click act: after I click act: What's actually under there:
Super_Hippo Super_Hippo

2018/8/14

#
When you have a line of code like this:
addObject(new Branch(angleL, angleR, 0.6666667f),0,200);
Then the code will always run from inside to outside. So the constructor of the Branch class will run with the given parameters and is then added to the world. The Branch isn't in any world when the constructor is executed. If you need a reference to the world, you can either pass the world object as a parameter or use the 'addedToWorld' method which will be executed automatically once right after it was added to a world.
TheGoldenProof TheGoldenProof

2018/8/15

#
I got it all fixed. I ended up using vectors and drawLine() to make it instead of scaling and rotating rectangles. Basically restarted from scratch. I did use the addedToWorld() method to draw the trunk.
You need to login to post a reply.