Seirpinski Triangle through Chaos Game

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
sciwizeh sciwizeh is online now Online Jan 24th, 2009, 12:31 am |
0
This is a simple implementation of the Chaos Game to produce a Seirpinski Triangle.
Quick reply to this message  
Java Syntax
  1. import java.awt.*;
  2. import javax.swing.*;
  3. /**
  4. * Main Frame of the Sierpinski Triangle frame.
  5. * @author SciWizEH
  6. * @version 1.0
  7. */
  8. public class Sierpinski extends JFrame
  9. {
  10. /**make a new Sierpinski*/
  11. public Sierpinski()
  12. {
  13. //add STPane to frame
  14. this.add(new STPane());
  15. //pack
  16. this.pack();
  17. //set visible
  18. this.setVisible(true);
  19. }
  20. /**main*/
  21. public static void main(String[] args){
  22. //make a new frame
  23. new Sierpinski();
  24. }
  25. }
  26. /**
  27.  * Point class holds a 2 dimentional integer point.
  28.  */
  29. class Point {
  30. //two portions of the point
  31. int x,y;
  32. //default constructor does nothing
  33. public Point(){}
  34. //constructor that takes two arguments
  35. public Point(int nx,int ny){
  36. x=nx;
  37. y=ny;
  38. }
  39. }
  40. /**
  41.  * class that computes and displays the Sierpinski Triangle
  42.  */
  43. class STPane extends JComponent {
  44. //holds the initial triangle for the sierpenski
  45. Point[] triangle = new Point[3];
  46. //first point
  47. Point fp;
  48. //current point
  49. Point cp=new Point();
  50. //default constructor simply sets the original size
  51. public STPane(){
  52. this.setPreferredSize(new Dimension(500,500));
  53. }
  54. //simple method to ge a 0-2 range random number to select a new point
  55. public int rand0to2(){
  56. return (int)(Math.random()*100)%3;
  57. }
  58. //paint where the magic of the chaos happens
  59. public void paint(Graphics g){
  60. //get the current size of the component
  61. Dimension d = super.getSize();
  62.  
  63. //make an isocilese triangle from the size of the
  64. //component for the original points
  65. triangle[0] = new Point((int)d.getWidth()/2,0);
  66. triangle[1] = new Point(0,(int)d.getHeight());
  67. triangle[2] = new Point((int)d.getWidth(),(int)d.getHeight());
  68.  
  69. //get an arbitrary point in the middle of the screen somewhere
  70. fp=new Point((int)(Math.random()*d.getWidth()),(int)(Math.random()*d.getHeight()/2));
  71.  
  72. //get a random triangle point
  73. Point p = triangle[rand0to2()];
  74. //get the first current point by going half way from the first
  75. //point to the chosen triangle point
  76. cp.x=(p.x+fp.x)/2;
  77. cp.y=(p.y+fp.y)/2;
  78.  
  79. //draw the first point
  80. g.drawRect(cp.x,cp.y,0,0);
  81.  
  82. //compute 40000 points in the same fashion but use the current
  83. //point rather than the very original point
  84. for(int i = 0; i<40000;i++){
  85. p = triangle[rand0to2()];
  86. cp.x=(p.x+cp.x)/2;
  87. cp.y=(p.y+cp.y)/2;
  88. g.drawRect(cp.x,cp.y,0,0);
  89. }
  90. }
  91. }
0
subtercosm subtercosm is offline Offline | Feb 7th, 2009
There's no need to write (int)(Math.random()*100)%3 . Just write (int)(Math.random() * 3) .
 
0
sciwizeh sciwizeh is online now Online | Feb 7th, 2009
I know, but, i've tested out some random stuff, and my way makes a better spread.
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC