CS 100: Lecture L16
March 18
public class ArrayOps
{
// Yields a reference to an array whose values are obtained by
// performing a perfect shuffle of a. Assumes that a has even length.
public static int[] Shuffle(int[] a)
{
int n = a.length;
int m = n/2;
int[] b = new int[n];
for(int i=0;i<m;i++)
{
b[2*i] = a[i];
b[2*i+1] = a[i+m];
}
return b;
}
// Perfect shuffles the entries in a. Assumes that a has even length.
public static void ShuffleAlt(int[] a)
{
int n = a.length;
int m = n/2;
int[] b = new int[n];
for(int i=0;i<m;i++)
{
b[2*i] = a[i];
b[2*i+1] = a[i+m];
}
for(int i=0;i<n;i++)
a[i] = b[i];
}
// Prints on a single line the values in the (presumably short) array a.
public static void println(int[] a)
{
int n= a.length;
for(int k=0;k<n;k++)
Format.print(System.out," %2d",a[k]);
System.out.println(" ");
}
}
// Repeated Perfect Shuffles on a length 8 array.
import java.io.*;
public class L16A
{
public static void main(String args[])
{
TokenReader in = new TokenReader(System.in);
int[] f0 = {10,20,30,40,50,60,70,80};
int[] f = new int[8];
int[] g = new int[8];
f = f0;
ArrayOps.println(f);
for (int k=1;k<=3;k++)
{
g = ArrayOps.Shuffle(f);
f = g;
ArrayOps.println(f);
}
System.out.println(" ");
f = f0;
ArrayOps.println(f);
for (int k=1;k<=3;k++)
{
f = ArrayOps.Shuffle(f);
ArrayOps.println(f);
}
System.out.println(" ");
f = f0;
ArrayOps.println(f);
for (int k=1;k<=3;k++)
{
ArrayOps.ShuffleAlt(f);
ArrayOps.println(f);
}
in.waitUntilEnter();
}
}
/* Output:
10 20 30 40 50 60 70 80
10 50 20 60 30 70 40 80
10 30 50 70 20 40 60 80
10 20 30 40 50 60 70 80
10 20 30 40 50 60 70 80
10 50 20 60 30 70 40 80
10 30 50 70 20 40 60 80
10 20 30 40 50 60 70 80
10 20 30 40 50 60 70 80
10 50 20 60 30 70 40 80
10 30 50 70 20 40 60 80
10 20 30 40 50 60 70 80
*/
// Illustrates the method fillPolygon.
import java.io.*;
import java.awt.*;
public class ShowPolys extends Frame
{
// Yields a reference to an array whose values are midway between
// the values in x (with wraparound).
public static int[] midPoint(int[] x)
{
int n = x.length;
int[] xmid = new int[n];
for(int k=0;k<n-1;k++)
xmid[k] = (x[k] + x[(k+1)])/2;
xmid[n-1] = (x[n-1]+x[0])/2;
return xmid;
}
public void paint(Graphics g)
{
// The vertices of the initial triangle.
int[] h = {100,800,600};
int[] v = {600,500,100};
for(int i=1;i<=7;i++)
{
// Draw a triangle and color it red or blue
if (i%2==0)
g.setColor(Color.red);
else
g.setColor(Color.blue);
g.fillPolygon(h,v,3);
// The next triangle will be obtained by connecting the midpoints of the
// current triangle.
h = midPoint(h);
v = midPoint(v);
}
}
}
public class L16B
{
public static void main(String args[])
{
ShowPolys d = new ShowPolys();
d.resize(1000,800); // Modify as appropriate.
d.move(0,75);
d.setTitle("Triangles"); // Modify as appropriate.
d.show();
d.toFront();
}
}
// First recursion example.
import java.io.*;
import java.awt.*;
public class ShowTriangles extends Frame
{
// Yields a reference to an array whose values are midway between
// the values in x (with wraparound).
public static int[] midPoint(int[] x)
{
int n = x.length;
int[] xmid = new int[n];
for(int k=0;k<n-1;k++)
xmid[k] = (x[k] + x[(k+1)])/2;
xmid[n-1] = (x[n-1]+x[0])/2;
return xmid;
}
// Yields the perimeter of the triangle whose vertices are prescribed by the
// length-3 arrays h and v.
public static double perimeter(int[] h, int[] v)
{
double d01 = Math.sqrt((h[0]-h[1])*(h[0]-h[1]) + (v[0]-v[1])*(v[0]-v[1]));
double d12 = Math.sqrt((h[1]-h[2])*(h[1]-h[2]) + (v[1]-v[2])*(v[1]-v[2]));
double d20 = Math.sqrt((h[2]-h[0])*(h[2]-h[0]) + (v[2]-v[0])*(v[2]-v[0]));
return (int)(d01+d12+d20);
}
// h and v are length-3 arrays that define the vertices of a triangle.
// If the perimeter of the triangle is <=10, nothing is done. Otherwise,
// the triangle is drawn together with the linesegments that connect the midpoints
// of its sides. The midpoint connectors break up the triangle into 4 smaller triangles.
// The process is repeated on three of these smaller triangles, namely, the ones
// that share a vertex with the original triangle.
public static void triangle(Graphics g, int[] h, int[] v)
{
g.drawPolygon(h,v,3);
if (perimeter(h,v) <=10)
return;
else
{
int[] h1 = midPoint(h);
int[] v1 = midPoint(v);
int[] a = new int[3];
int[] b = new int[3];
// Set up the vertices of the three smaller triangles and repeat the process.
a[0]=h[0]; a[1]=h1[0]; a[2]=h1[2]; b[0]=v[0]; b[1]=v1[0]; b[2]=v1[2];
triangle(g,a,b);
a[1]=h[1]; a[2]=h1[1]; a[0]=h1[0]; b[1]=v[1]; b[2]=v1[1]; b[0]=v1[0];
triangle(g,a,b);
a[2]=h[2]; a[0]=h1[2]; a[1]=h1[1]; b[2]=v[2]; b[0]=v1[2]; b[1]=v1[1];
triangle(g,a,b);
return;
}
}
public void paint(Graphics g)
{
int[] h = {100,800,600};
int[] v = {600,500,100};
triangle(g,h,v);
}
}
public class L16C
{
public static void main(String args[])
{
ShowTriangles d = new ShowTriangles();
d.resize(1000,800);
d.move(0,75);
d.setTitle("Recursion");
d.show();
d.toFront();
}
}