Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:10:48+00:00 2026-06-12T14:10:48+00:00

import gpdraw.*; public class Y2K { // Attributes SketchPad pad; DrawingTool pen; // Constructor

  • 0
import gpdraw.*;

public class Y2K {

// Attributes
SketchPad pad;
DrawingTool pen;

// Constructor
public Y2K() {

    pad = new SketchPad(600, 600, 50);
    pen = new DrawingTool(pad);

    // Back the pen up so the Y is drawn in the middle of the screen
    pen.up();
    pen.setDirection(270);
    pen.forward(150);
    pen.down();
    pen.setDirection(90);
}

public void drawY(int level, double length) {

    // Base case:  Draw an Y
    if (level == 0) {

        //pen.setDirection(90);
        pen.forward(length);
        pen.turnRight(60);
        pen.forward(length);
        pen.backward(length);
        pen.turnLeft(120);
        pen.forward(length);
        pen.backward(length);
    }


    // Recursive case:  Draw an L at each midpoint
    // of the current L's segments
    else {


            //Drawing the bottom "leg" of our Y shape
            pen.forward(length / 2);
            double xpos1 = pen.getXPos();
            double ypos1 = pen.getYPos();
            double direction1 = pen.getDirection();


            pen.turnRight(90);
            drawY(level - 1, length / 2.0);

            pen.up();
            pen.move(xpos1, ypos1);
            pen.setDirection(direction1);
            pen.down();
            pen.forward(length / 2);

            double xpos2 = pen.getXPos();
            double ypos2 = pen.getYPos();
            double direction2 = pen.getDirection();

            //Drawing upper Right Leg
            pen.turnRight(60);
            pen.forward(length / 2); //going to the midpoint
            double xpos3 = pen.getXPos();
            double ypos3 = pen.getYPos();
            double direction3 = pen.getDirection();
            pen.turnLeft(90);
            drawY(level - 1, length / 2.0);

            pen.up();
            pen.move(xpos3, ypos3);
            pen.setDirection(direction3);
            pen.down();
            pen.forward(length / 2);

            //drawing upper left leg
            pen.up();
            pen.move(xpos1, ypos1);
            pen.setDirection(direction1);
            pen.down();
            pen.forward(length / 2);

            pen.turnLeft(60);
            pen.forward(length / 2);
            double xpos4 = pen.getXPos();
            double ypos4 = pen.getYPos();
            double direction4 = pen.getDirection();

            pen.turnLeft(90);
            drawY(level - 1, length / 2.0);

            pen.up();
            pen.move(xpos4, ypos4);
            pen.setDirection(direction4);
            pen.down();
            pen.forward(length / 2);
            pen.forward(length / 2);
        }

}

public static void main(String[] args) {

    Y2K fractal = new Y2K();

    // Draw Y with given level and side length
    fractal.drawY(8, 200);
}   


}

output:

one certain leg of the triangle is too long, and that makes the output slightly off. maybe its because the code went (length/2) too far? lets debug this.

otherwise it is completely fine, the recursion is great, and its exactly what i wanted to do
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:10:49+00:00Added an answer on June 12, 2026 at 2:10 pm

    As you’re constantly drawing Y’s, I’d recommend you create a method that draws a Y given certain parameters (e.g. length, angle of separation between the two branches of the Y, rotation, etc.). This will make your code much more readable and easier to understand.

    As for moving to the center, just think of the Y on a coordinate plane. Based upon the rotation of the Y, and its starting point you can calculate the center point.

    Diagram

    Just break it up into its x and y components.

    Broken up into components

    Given this information, we can solve for a and for b.

    a = length * sin(θ)
    b = length * cos(θ)
    

    Then add this to your x and y to calculate the center point of the Y.

    As for keeping the constant length, you know the level. At the first level, level == 1. But the length of this next level should be length * (2^level). In this case, length/2 (as length would be -1).

    In pseudo code terms:

    public void drawY(int level, double length)
    {
        //Drawing the bottom "leg" of our Y shape
        Move Forward length/2
        Save our position 
        Save our direction
    
        Turn to the right 90 degrees
        Recursion (call drawY())
    
        revert to original location
        revert to original direction
        move forward length/2 (to go to center point of Y)
    
        save our new position
        save our new direction 
    
        //Drawing upper Right Leg
        Turn 60 to the right
        Move Forward length/2 //going to the midpoint
        save our new position (don't forget the center point)
        save our new direction (don't forget the center point direction)
        Turn 90 to the left
        Recursion (call drawY())
    
        return to our saved position (not center one)
        return to our saved direction (not center one)
    
        move forward length/2
    
        //drawing upper left leg
        return to center point
        return to center direction
    
        turn left 60 
        move forward length/2
        save position (you can overwrite the center one now
        save direction (you can overwrite)
    
        turn left 90
        Recursion (call drawY())
    
        return to position
        return to direction
        move forward length/2
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import gpdraw.*; import javax.swing.*; import java.lang.Math; public class Koch2 extends JFrame { SketchPad paper;
import java.util.Scanner; public class smth { Scanner input = new Scanner(System.in); int array[]={}; }
import java.util.ArrayList; import java.lang.Math; public class War { ArrayList deck = new ArrayList(0); ArrayList
import org.joda.time.LocalDate; public class Test { public static void main(String[] args) { long time=System.currentTimeMillis();
import java.awt.*; import javax.swing.*; import javax.swing.GroupLayout; /** * @author Ene Ion */ public class
import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class ExplicitChannelRead { /** * @param args
import java.util.Scanner; public class CourseSplitter { public static void main(String args[]){ Scanner keyboard =
import java.lang.*; public class GrammerStack extends GrammerStructure implements StringStack { private String structName; private
import javax.swing.JOptionPane; public class RotateArrayCircularLL { private Node head=null; public void init() { int
import java.util.*; import java.io.*; import java.util.regex.*; class ZiggyTest2 extends Thread{ String sa; public ZiggyTest2(String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.