Java Reference
In-Depth Information
P3 y=( int ) getSize () . getHeight ()
20;
setVisible( true ) ; setDefaultCloseOperation (JFrame.
EXIT ON CLOSE) ;
// Compute the midpoint
public Point getMiddle (Point p1 , Point p2) {
return new Point (( int ) (p1 . getX () + p2 . getX () ) /2 , ( int )(p1
. getY () + p2 . getY () ) /2) ;
} public void paint (Graphics g)
{
super .paint(g);
sierpinski draw( new Point(P1 x, P1 y) , new Point(P2 x,
P2 y) , new Point(P3 x,P3 y));
} public void sierpinski draw (Point p1 , Point p2 , Point p3)
{
//termination condition
if (p1. distance(p2) < THRESHOLD
&&
p1 . distance (p3) <
THRESHOLD &&
p2 . distance (p3) < THRESHOLD)
return ; // stop recursion
//draw the current triangle
Graphics g = getGraphics () ;
g . drawLine (( int )p1.getX() ,( int )p1.getY() ,( int )p2.getX() ,(
int )p2.getY()) ;
g . drawLine (( int )p2.getX() ,( int )p2.getY() ,( int )p3.getX() ,(
int )p3.getY()) ;
g . drawLine (( int )p3.getX() ,( int )p3.getY() ,( int )p1.getX() ,(
int )p1.getY()) ;
//recursively draw the 3 smaller corner triangles
Point m12 = getMiddle (p1 , p2) ;
Point m23 = getMiddle (p2 , p3) ;
Point m31 = getMiddle (p3 , p1) ;
// Recursive calls
sierpinski draw(p1, m12, m31) ;
sierpinski draw(p2, m23, m12) ;
sierpinski draw(p3, m31, m23) ;
} public static void main( String [ ]
args )
{
new Sierpinski () ;
}
}
3.8 Halting problem: An undecidable task
We have shown that recursion is a very powerful concept for writing compact
functions that calculates results using a recurrence relation with associated
terminal states. A major problem is to know whether all terminal states are
properly studied. Otherwise, the function might call itself forever, or at least
 
 
Search WWH ::




Custom Search