001 002 /** 003 * PointN is a subclass of Point extending Point to the general case 004 * of N dimensions.*/ 005 public class PointN extends Point { 006 007 /** 008 * Instance variables d storing the dimension of this PointN.*/ 009 private int d; 010 011 /** 012 * Instance double array x containing the coordinates of this PointN.*/ 013 private double[] x; 014 015 public PointN(double[] z) { 016 d = z.length; 017 x = new double[d]; 018 for (int i = 0; i < d; i++) x[i] = z[i]; 019 } 020 021 /** 022 * Overrides the Point.clone() method to enable a deep copy.*/ 023 public Object clone() { 024 PointN p = (PointN) super.clone(); 025 p.x = (double[]) x.clone(); 026 return (Object) p; 027 } 028 029 /** 030 * Instance method to return d, the dimension of this PointN.*/ 031 public int dimension() { return d; } 032 033 /** 034 * Instance method to return the ith coordinate of this PointN.*/ 035 public double coordinate(int i) { 036 try { return x[i]; } 037 catch (ArrayIndexOutOfBoundsException e) { 038 throw new IllegalArgumentException("Cannot get coordinate " + i + 039 " of a point in an " + d + 040 "dimensional space"); 041 } 042 } 043 044 /** 045 * Instance method to set the ith coordinate of this PointN.*/ 046 public void setCoordinate(int i, double value) { 047 try { x[i] = value; } 048 catch (ArrayIndexOutOfBoundsException e) { 049 throw new IllegalArgumentException("Cannot set coordinate " + i + 050 " of a point in an " + d + 051 "dimensional space"); 052 } 053 } 054 055 056 /** 057 * Instance method to add a PointN to this PointN.*/ 058 public void add(Point delta) { 059 PointN u; 060 try { u = (PointN) delta; } 061 catch (ClassCastException e) { 062 throw new IllegalArgumentException("Incompatible Points"); 063 } 064 if (u.dimension() != d) 065 throw new IllegalArgumentException("Incompatible Points"); 066 for (int i = 0; i < d; i++) { x[i] += u.coordinate(i); } 067 } 068 069 /** 070 * Instance method to multiply this PointN by a double.*/ 071 public void multiply(double s) { 072 for (int i = 0; i < d; i++) { x[i] *= s; } 073 } 074 075 /** 076 * Instance method to take the absolute value of this PointN.*/ 077 public void abs() { 078 for (int i=0; i<d; i++) { 079 x[i] = Math.abs(x[i]); 080 } 081 } 082 083 /** 084 * Instance method to return the maximum value of this PointN.*/ 085 public double max() { 086 double max = x[0]; 087 for (int i=1; i<x.length; i++) { 088 if (x[i]>x[i-1]) max = x[i]; 089 } 090 return max; 091 } 092 093 /** 094 * Instance method to return the minimum value of this PointN.*/ 095 public double min() { 096 double min = x[0]; 097 for (int i=1; i<x.length; i++) { 098 if (x[i]<x[i-1]) min = x[i]; 099 } 100 return min; 101 } 102 103 /** 104 * Returns the PointN in readable form.*/ 105 public String toString() { 106 String line = "("; 107 int j = 0; 108 for (int i=0; i<d-1; i++) { 109 if (j == 5){ 110 line += x[i]+", ,"; 111 j = 0; 112 } 113 line += x[i]+","; 114 } 115 line += x[d-1]+")"; 116 return line; 117 } 118 119 /** 120 * Instance method to truncate this PointN to 10^(n) accuracy.*/ 121 public void truncate(int n){ 122 int temp; 123 for (int i=0; i<x.length; i++) { 124 x[i] *= Math.pow(10,-n); 125 temp = (int)x[i]; 126 x[i] = temp/Math.pow(10,-n); 127 } 128 } 129 130 /** 131 * Instance method to round this PointN to 10^(n) accuracy.*/ 132 public void round(int n){ 133 int tempX; 134 double absX; 135 for (int i=0; i<x.length; i++) { 136 absX = Math.abs(x[i]); 137 absX *= Math.pow(10,-n); 138 absX += 0.50; 139 tempX = (int)absX; 140 absX = tempX; 141 absX /= Math.pow(10,-n); 142 if(x[i]!=0) x[i] = absX*x[i]/Math.abs(x[i]); 143 else x[i] = absX; 144 } 145 } 146 147 } 148 149