adim2
adim2

Reputation: 21

Sierpinski triangle

The kth Sierpinski triangle is a triangle whose interior is sub-divided as follows:

  1. Take the three mid-points of the sides of the triangle. These points form the vertices of an inscribed triangle, which is colored black.
  2. The remaining 3 inscribed triangles are (k-1)th Sierpinski triangles. complete the code of the drawTriangle method in the SierpinskiTriangle class and a screenshot of the output of your Sierpinski program for k = 4.

I am really struggling here, I wrote this code but its not giving me what I need, any help and a step by step explanation will be very helpful. And my triangle don't stay, it disappears after a while. thanks in advance

import java.awt.*;
import javax.swing.*;

public class Sierpinski_Triangle extends JPanel {
    private static int numberLevelsOfRecursion;

    public Sierpinski_Triangle(int numLevels) {
        numberLevelsOfRecursion = numLevels;
    }

    public void paintComponent(Graphics computerScreen) {
        super.paintComponent(computerScreen);
        Point top = new Point(250, 50);
        Point left = new Point(50, 450);
        Point right = new Point(450, 450);
        drawTriangle(computerScreen, numberLevelsOfRecursion, top, left, right);
    }

    /**
     * Draw a Sierpinski triangle
     * 
     * @param screen
     *            the surface on which to draw the Sierpinski image
     * @param levels
     *            number of levels of triangles-within-triangles
     * @param top
     *            coordinates of the top point of the triangle
     * @param left
     *            coordinates of the lower-left point of the triangle
     * @param right
     *            coordinates of the lower-right point of the triangle
     */
    public static void drawTriangle(Graphics g, int levels, Point top, Point left, Point right) {
        /**
         * You must COMPLETER THE CODE HERE to draw the Sierpinski Triangle
         * recursive code needed to draw the Sierpinski Triangle
         */
        Point p1 = top;
        Point p2 = left;
        Point p3 = right;
        if (levels == 2) {
            // base case: simple triangle
            Polygon tri = new Polygon();
            tri.addPoint(250, 50);
            tri.addPoint(50, 450);
            tri.addPoint(450, 450);
            g.setColor(Color.RED);
            g.fillPolygon(tri);
        } else {
            // Get the midpoint on each edge in the triangle
            Point p12 = midpoint(p1, p2);
            Point p23 = midpoint(p2, p3);
            Point p31 = midpoint(p3, p1);
            // recurse on 3 triangular areas
            drawTriangle(g, levels - 1, p1, p12, p31);
            drawTriangle(g, levels - 1, p12, p2, p23);
            drawTriangle(g, levels - 1, p31, p23, p3);
        }
    }

    private static Point midpoint(Point p1, Point p2) {
        return new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("SierpinskiTriangle");
        Sierpinski_Triangle applet = new Sierpinski_Triangle(1);
        frame.add(applet);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(450, 450);
        frame.setVisible(true);
    }
}

Upvotes: 1

Views: 4410

Answers (2)

karatedog
karatedog

Reputation: 2618

Get rid of that fixed size triangle in the base case. You have to change this:

        tri.addPoint(250, 50);
        tri.addPoint(50, 450);
        tri.addPoint(450, 450);

to

        tri.addPoint(p1.x, p1.y);
        tri.addPoint(p2.x, p2.y);
        tri.addPoint(p3.x, p3.y);

And as a guard against stack overflow, you should change this:

    if (levels == 2) {

to this:

    if (levels <= 2) {

And add more recursion level by setting the initial argument to higher than 1 (or you will only see that big red triangle):

    Sierpinski_Triangle applet = new Sierpinski_Triangle(5);

Sierpinski Triangle, 9 level recursion

Upvotes: 3

Jiri Kremser
Jiri Kremser

Reputation: 12847

You are setting the numberLevelsOfRecursion to 1 in the main method:

new Sierpinski_Triangle(1);

But you base case for the recursion is if (levels == 2), so your program basically hangs, because the level goes to "-infinity".

Also the code in the base case looks odd. It basically draws a big red triangle over everything drawn before. If your intention was to draw this at the beginning of the recursive process as some kind of borders for the picture, you shouldn't decrease the levels variable.

Upvotes: 1

Related Questions