0

I have a program for drawing n generations of a pattern of lines. Beginning with a single horizontal line in the 0th generation, each open edge serves as the mid point of another perpendicular line. The picture shows the result of running it with n=0, n=1, and n=2 respectively. https://i.sstatic.net/ZlyKa.png

I call my method once like this where x is initialized to the center of the screen, and steps is the number of steps.

paintComponent(g,x, Direction.HORIZONTAL, steps);

The method produces expected output and correctly draws the lines.

public void paintComponent(Graphics g,Point point,Direction direction, int n){

    if(n>=0){
        //length*=multiplier;
        double mid = length/2;
        Point e1;
        Point e2;
        if(direction==Direction.HORIZONTAL){ 
            e1 = new Point((int)(point.getX()-mid),(int)(point.getY()));
            e2 = new Point((int)(point.getX()+mid),(int)(point.getY()));
            g.drawLine((int)e1.getX(),(int)e1.getY(),(int)e2.getX(),(int)e2.getY());  
            direction = Direction.VERTICAL;
        }
        else{
            e1 = new Point((int)(point.getX()),(int)(point.getY()-mid));
            e2 = new Point((int)(point.getX()),(int)(point.getY()+mid));
            g.drawLine((int)e1.getX(),(int)e1.getY(),(int)e2.getX(),(int)e2.getY());
            direction = Direction.HORIZONTAL;
        }   
        n--;
        paintComponent(g,e1, direction, n);
        paintComponent(g,e2, direction, n); 
    }   
}

Now i'm trying to change the length of the line by a multiplier after each generation/step. The first line would have the initial length, then length would be updated to length*=multiplier for the 2 lines added in n=1, updated again for the 4 added in n=2 etc.

What i'm having a problem defining is where each step actually ends so I can update the length there. I can't update it where I have below because each step enters the loop multiple times. I've tried iterating a count variable but I can't find where to put that either.

1 Answer 1

1

If you want to change length of your line then you need to change points e1 and e2 in such a way that when your line is horizontal then increase /decrease your e1.x and e2. x according to the value by which you want to increase it or decrease it and when it's vertical change only e1.y and e2.y before calling g.drawline method. For changing point location you can use translate method of point class where you need to compute value of dx and dy according to the factor by which you want to manipulate actual points.

Hope this solve your query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.