|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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.
|
| Sponsored Links |
|
|
|
|||
|
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);
}
}
|
|
|||||
|
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 |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ! Need urgent help ! Drawing program using cygwin | siren | C and C++ | 0 | 05-26-2007 10:51 PM |
| John | ........ | 223.00000 |
| dargueta | ........ | 168.00000 |
| Xav | ........ | 164.00000 |
| gaylo565 | ........ | 18.00000 |
| WingedPanther | ........ | 15.00000 |
| |pH| | ........ | 15.00000 |
| Johnnyboy | ........ | 3.00000 |
| navghost | ........ | 1.00000 |
Goal: 100,000 Posts
Complete: 66%