Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Java Help

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-18-2007, 12:42 AM
dalearyous dalearyous is offline
Newbie
 
Join Date: May 2007
Posts: 9
Rep Power: 0
dalearyous is on a distinguished road
Default need help drawing Sierpinski triangles using recursion

ok i understand the overall idea but i am having trouble finding the vertices (midpoints of each line) of a triangle. basically i am going to have the program draw a triangle then draw another triangle inside that one with the three midpoints (only upside down). once that one is created ill have 3 triangles around it that are also sierpinski triangles. if i can find each of the 3 triangles vertices then i can just repeat process because each time i draw a new triangle i have 3 more sierpinski triangles. if you post suggestions keep in mind im kinda noob with java.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 09-18-2007, 01:23 PM
dalearyous dalearyous is offline
Newbie
 
Join Date: May 2007
Posts: 9
Rep Power: 0
dalearyous is on a distinguished road
Default

Code:
// this program draws Sierpinski triangles every time you click the screen


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

public class Sierpinski extends Applet
{

    Graphics g;
    Point a1,b1,c1, a2,b2,c2, a3,b3,c3;

    int depth = 0;

    public void init()
    {
        setBackground(new Color(255,255,255));
        setBackground( Color.black );
    }

// this handles the mouse operations, every time you left click it steps up one depth
// every time you right click it steps down one depth
    public boolean mouseDown(Event click, int x, int y)
    {
        if (!click.metaDown()) depth += 1;
        else if (depth>0) depth -= 1;
        repaint();
        return true;
    }

// draws first triangle
    public void paint(Graphics tri)
    {
        tri.setColor( Color.YELLOW );
        // tri.setColor(new Color(-16777216));
        int xCoords[] = {10, 390, 200};
        int yCoords[] = {390, 390, 10};
        tri.drawPolygon(xCoords, yCoords, 3);

        drawTriangle(tri, new Point(10,390),new Point(390,390),new Point(200,10), depth);
    }

    public void fillPolygon()
    {

    }

// i know the coordinates were supposed to be passed in as an array but here is the draw method
    public void drawTriangle(Graphics g, Point a, Point b, Point c, int depth)
    {
        if (depth==0) return;

        depth -= 1;

// did the array here because i couldn't get it to work if i initialize it at beginning
        int xCoords[] = {c.x, (c.x+b.x)/2, (a.x+c.x)/2};
        int yCoords[] = {b.y, (c.y+a.y)/2, (c.y+a.y)/2};

        g.drawPolygon(xCoords, yCoords, 3);

        a1 = a;
        b1 = new Point(c.x, b.y);
        c1 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
        drawTriangle(g, a1, b1, c1, depth);

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

        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;
        drawTriangle(g, a3, b3, c3, depth);
    }
}
ok i finished it. that does exactly what i want except, i want each triangle to be a random color. any help?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-18-2007, 01:54 PM
dalearyous dalearyous is offline
Newbie
 
Join Date: May 2007
Posts: 9
Rep Power: 0
dalearyous is on a distinguished road
Default

nevermind i got it
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-18-2007, 01:55 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,470
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default

Can you post your final code?
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
! Need urgent help ! Drawing program using cygwin siren C and C++ 0 05-26-2007 11:51 PM


All times are GMT -5. The time now is 11:55 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads