Question
In Dr Java! I need help ASAP
Triangles! Oh Fractals! Objective: Write a program in which draws (yes it actually makes a picture) a triangular fractal using recursion. This is best if done using a java applet Suggested Methodology The idea for it is this 1. First draw a filled equilateral triangle 2. Next draw another filled equilateral triangle of a different color thats upside down in the middle of that triangle 3. Using the other triangles formed repeat step 2 until a pixel limit of 4 is reached HINTS It may be a good idea to look at the examples I gave 02/12/2015 The method fillPolygon(intl] xPoints, intlI yPoint, numberOfPoints) as called by the graphics device is important The method setColor(Color aColor) is important for picking different colors to draw things Example Image of Results: id Applet View. Applet Applet started
0 0
Add a comment Improve this question Transcribed image text
Answer #1
/Declaration of the drawSierpinski function. The coordinates are the 3 outer corners of the Sierpinski Triangle.
void drawSierpinski(float x1, float y1, float x2, float y2, float x3, float y3);
//Declaration of the subTriangle function, the coordinates are the 3 corners, and n is the number of recursions.
void subTriangle(int n, float x1, float y1, float x2, float y2, float x3, float y3);

//depth is the number of recursive steps
int depth = 7;

//The main function sets up the screen and then calls the drawSierpinski function
int main(int argc, char *argv[])
{
    screen(640, 480, 0, "Sierpinski Triangle");
    cls(RGB_White); //Make the background white
    drawSierpinski(10, h - 10, w - 10, h - 10, w / 2, 10); //Call the sierpinski function (works with any corners inside the screen)
    //After drawing the whole thing, redraw the screen and wait until the any key is pressed
    redraw();
    sleep();
    return(0);   
}

//This function will draw only one triangle, the outer triangle (the only not upside down one), and then start the recursive function
void drawSierpinski(float x1, float y1, float x2, float y2, float x3, float y3)
{
        //Draw the 3 sides of the triangle as black lines
        drawLine(int(x1), int(y1), int(x2), int(y2), RGB_Black);
        drawLine(int(x1), int(y1), int(x3), int(y3), RGB_Black);
        drawLine(int(x2), int(y2), int(x3), int(y3), RGB_Black);
        
        //Call the recursive function that'll draw all the rest. The 3 corners of it are always the centers of sides, so they're averages
        subTriangle
        (
            1, //This represents the first recursion
            (x1 + x2) / 2, //x coordinate of first corner
            (y1 + y2) / 2, //y coordinate of first corner
            (x1 + x3) / 2, //x coordinate of second corner
            (y1 + y3) / 2, //y coordinate of second corner
            (x2 + x3) / 2, //x coordinate of third corner
            (y2 + y3) / 2  //y coordinate of third corner
        );            
}    

//The recursive function that'll draw all the upside down triangles
void subTriangle(int n, float x1, float y1, float x2, float y2, float x3, float y3)
{   
    //Draw the 3 sides as black lines
    drawLine(int(x1), int(y1), int(x2), int(y2), RGB_Black);
    drawLine(int(x1), int(y1), int(x3), int(y3), RGB_Black);
    drawLine(int(x2), int(y2), int(x3), int(y3), RGB_Black);        
    
    //Calls itself 3 times with new corners, but only if the current number of recursions is smaller than the maximum depth
    if(n < depth)
    {
        //Smaller triangle 1
        subTriangle
        (
            n+1, //Number of recursions for the next call increased with 1
            (x1 + x2) / 2 + (x2 - x3) / 2, //x coordinate of first corner
            (y1 + y2) / 2 + (y2 - y3) / 2, //y coordinate of first corner
            (x1 + x2) / 2 + (x1 - x3) / 2, //x coordinate of second corner
            (y1 + y2) / 2 + (y1 - y3) / 2, //y coordinate of second corner
            (x1 + x2) / 2, //x coordinate of third corner
            (y1 + y2) / 2  //y coordinate of third corner
        );
        //Smaller triangle 2
        subTriangle
        (
            n+1, //Number of recursions for the next call increased with 1
            (x3 + x2) / 2 + (x2 - x1) / 2, //x coordinate of first corner
            (y3 + y2) / 2 + (y2 - y1) / 2, //y coordinate of first corner
            (x3 + x2) / 2 + (x3 - x1) / 2, //x coordinate of second corner
            (y3 + y2) / 2 + (y3 - y1) / 2, //y coordinate of second corner
            (x3 + x2) / 2, //x coordinate of third corner
            (y3 + y2) / 2  //y coordinate of third corner
        );
        //Smaller triangle 3
        subTriangle
        (
            n+1, //Number of recursions for the next call increased with 1
            (x1 + x3) / 2 + (x3 - x2) / 2, //x coordinate of first corner
            (y1 + y3) / 2 + (y3 - y2) / 2, //y coordinate of first corner
            (x1 + x3) / 2 + (x1 - x2) / 2, //x coordinate of second corner
            (y1 + y3) / 2 + (y1 - y2) / 2, //y coordinate of second corner
            (x1 + x3) / 2, //x coordinate of third corner
            (y1 + y3) / 2  //y coordinate of third corner        
        );                
    }             
}

you see after running the program:

Sierpinski Triangle

Add a comment
Know the answer?
Add Answer to:
In Dr Java! I need help ASAP Triangles! Oh Fractals! Objective: Write a program in which...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment Λ You shall write a Java program that accepts 5 command-line arguments and generates an image of a Sierpinski triangle, as a 24- bit RGB PNG image file. Specifications The command-line arg...

    Assignment Λ You shall write a Java program that accepts 5 command-line arguments and generates an image of a Sierpinski triangle, as a 24- bit RGB PNG image file. Specifications The command-line arguments shall consist of the following 1. The width (in pixels) of the image, as a positive decimal integer 2. The height (in pixels) of the image, as a positive decimal integer 3. The minimum area (in pixels) that a triangle must have in order to be drawn,...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • hello there, i have to implement this on java processing. can someone please help me regarding...

    hello there, i have to implement this on java processing. can someone please help me regarding that? thanks War is the name of a popular children’s card game. There are many variants. After playing War with a friend for over an hour, they argue that this game must never end . However! You are convinced that it will end. As a budding computer scientist, you decide to build a simulator to find out for sure! You will implement the logic...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT