CS 100: Lecture L14
March 11
// First example with arrays.
import java.awt.*;
public class ShowMedians extends Frame
{
public void paint(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0,1000,1000);
// Create arrays for the vertex coordinates and the midpoint coordinates.
int[] h = new int[3];
int[] v = new int[3];
int[] hMid = new int[3];
int[] vMid = new int[3];
// Initialize the vertex coordinates.
h[0] = 100; h[1] = 700; h[2] = 300;
v[0] = 500; v[1] = 500; v[2] = 100;
// Draw the sides
g.setColor(Color.blue);
g.drawLine(h[0],v[0],h[1],v[1]);
g.drawLine(h[1],v[1],h[2],v[2]);
g.drawLine(h[2],v[2],h[0],v[0]);
// Compute the midpoint coordinates
hMid[0] = (h[0]+h[1])/2 ; vMid[0] = (v[0]+v[1])/2;
hMid[1] = (h[1]+h[2])/2 ; vMid[1] = (v[1]+v[2])/2;
hMid[2] = (h[2]+h[0])/2 ; vMid[2] = (v[2]+v[0])/2;
// Connect each vertex with the opposite midpoint
g.setColor(Color.red);
g.drawLine(h[0],v[0],hMid[1],vMid[1]);
g.drawLine(h[1],v[1],hMid[2],vMid[2]);
g.drawLine(h[2],v[2],hMid[0],vMid[0]);
// Display the Centroid.
int hBar = (h[0]+h[1]+h[2])/3;
int vBar = (v[0]+v[1]+v[2])/3;
g.fillOval(hBar-5,vBar-5,10,10);
}
}
public class L14A
{
public static void main(String args[])
{
ShowMedians d = new ShowMedians();
d.resize(1000,600);
d.move(0,75);
d.setTitle("The Medians of a Triangle Intersect at the Centroid");
d.show();
d.toFront();
}
}
// Arrays and loops.
import java.awt.*;
public class ShowCentroid extends Frame
{
final static int nMax = 200;
final static int m = 50;
final static int L = 800;
final static int W = 500;
public void paint(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0,1000,1000);
// Generate the random points
int[] h = new int[nMax];
int[] v = new int[nMax];
for(int i=0;i<nMax;i++)
{
h[i] = (int) (m + L*Math.random());
v[i] = (int) (m + W*Math.random());
}
// Compute the centroid (hBar,vBar) where hBar (vBar) is the
// average of the h-values (v-values).
int hSum = 0;
int vSum = 0;
for(int j=0;j<nMax;j++)
{
hSum = hSum + h[j];
vSum = vSum + v[j];
}
int hBar = hSum/nMax;
int vBar = vSum/nMax;
// Display the line segments that connect the points and the centroid.
for(int k=0;k<nMax;k++)
{
g.setColor(Color.pink);
g.drawLine(hBar,vBar,h[k],v[k]);
}
// Highlight points and the centroid.
for(int k=0;k<nMax;k++)
{
g.setColor(Color.red);
g.fillOval(h[k]-4,v[k]-4,8,8);
}
g.setColor(Color.blue);
g.fillOval(hBar-5,vBar-5,10,10);
// Identify and highlight the point furthest from the centroid.
double d; // distance from centroid to the ith point.
double dMax = 0; // Maximum distance from the centroid.
int iMax=0; // Index of the point furthest from the centroid.
for(int i=0;i<nMax;i++)
{
// See in (h[i],v[i]) is the new "furthest point" from the centroid.
d = Math.sqrt(Math.pow(hBar-h[i],2)+Math.pow(vBar-v[i],2));
if(d>dMax)
{
dMax = d;
iMax = i;
}
}
g.setColor(Color.green);
g.fillOval(h[iMax]-5,v[iMax]-5,10,10);
}
}
public class L14B
{
public static void main(String args[])
{
ShowCentroid d = new ShowCentroid();
d.resize(1000,600);
d.move(0,75);
d.setTitle("Centroid");
d.show();
d.toFront();
}
}