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 7495315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:01:48+00:00 2026-05-29T18:01:48+00:00

I’ve created a sierpinski triangle code. But i want to shade the triangles according

  • 0

I’ve created a sierpinski triangle code. But i want to shade the triangles according to how many triangles the code makes. In my case i only print triangles that are not shaded. I want something similar like this!
http://en.wikipedia.org/wiki/File:Sierpinski_triangle_evolution.svg

Group 2: Kulplex

Student:Henry Dang

What should i do?

import java.applet.*;
import java.awt.*;

public class Sierpinski extends Applet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    Graphics g;
    Point a1,b1,c1, a2,b2,c2, a3,b3,c3;

    int deep = 0;

    public void paint() {
        setBackground(new Color(255,255,255));
    }

    public boolean mouseDown(Event ev, int x, int y) {
        if (!ev.metaDown()) deep += 1;
        else if (deep>0) deep -= 1;
        repaint();
        return true;
    }


    public void paint(Graphics g) {
        // Create triangle
        int px[] = {20, 400, 210};
        int py[] = {400, 400, 20};
        g.drawPolygon(px, py, 3);

        paintTriangle(g, new Point(20,400),new Point(400,400),new Point(210,20), deep);
    }

    public void paintTriangle(Graphics g, Point a, Point b, Point c, int lvl) {


        if (lvl==0) return;

        lvl -= 1;

        // In the given triangle, amended to include an upside-down triangle
        int px[] = {c.x, (c.x+b.x)/2, (a.x+c.x)/2};
        int py[] = {b.y, (c.y+a.y)/2, (c.y+a.y)/2};

        g.drawPolygon(px, py, 3);

        // 3 new triangles 
        a1 = a;
        b1 = new Point(c.x, b.y);
        c1 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
        paintTriangle(g, a1, b1, c1, lvl);

        a2 = new Point(c.x, b.y);
        b2 = b;
        c2 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
        paintTriangle(g, a2, b2, c2, lvl);

        a3 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
        b3 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
        c3 = c;
        paintTriangle(g, a3, b3, c3, lvl);
    }
}
  • 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-05-29T18:01:50+00:00Added an answer on May 29, 2026 at 6:01 pm

    Shouldn’t you only use the call to g.drawPolygon(px, py, 3); when you are at the bottom of your recursive call?

    The way the code looks to me right now you will always draw the big triangle of the first level, which will obscure the drawing you do of the smaller layers.

    What I’m trying to say is that you should only do the call to g.drawPolygon() if the level is zero, for all lower levels just recurse to produce smaller triangles. This is if you’re trying to draw the black triangles, instead of drawing the white spaces. It’s a bit unclear what you want to accomplish.

    Edit: Based on your comment what you really want to is to FILL the polygons, am I right? In that case replace drawPolygon with fillPolygon and the triangles will be filled in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but

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.