id
stringlengths
36
36
text
stringlengths
1
1.25M
a79ac37f-7adf-4640-833f-8d640ac27c97
public void keyReleased(KeyEvent e) { // si se suelta spacebar, barra se mueve hacia al izquierda if(e.getKeyCode() == KeyEvent.VK_RIGHT) { dirBarra = 0; } else if(e.getKeyCode() == KeyEvent.VK_LEFT) { dirBarra = 0; } }
01c8d54a-a0c8-4c21-920a-8b6ffbedd2e9
public void keyPressed(KeyEvent e) { // si se presiona spacebar, barra se mueve hacia la derecha if (e.getKeyCode() == KeyEvent.VK_RIGHT) { dirBarra = 1; } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { dirBarra = 2; } //si se acaba el juego, se reinicia al teclear 'n' else if(e.getKeyCode() == KeyEvent.VK_N && encBloques.size() == 0){ init(); } else if(e.getKeyCode() == KeyEvent.VK_P) { bPausa = !bPausa; } }
fe017e3b-f429-42db-b5c7-0df7fe822d0f
public void paint (Graphics graGrafico){ // Inicializan el DoubleBuffer if (imaImagenApplet == null){ imaImagenApplet = createImage (this.getSize().width, this.getSize().height); graGraficaApplet = imaImagenApplet.getGraphics (); } //crea imagen para el background URL urlImagenFondo; Image imaImagenFondo; if(encBloques.size() > 0){ urlImagenFondo = this.getClass().getResource("fondo.png"); } else { urlImagenFondo = this.getClass().getResource("game_over.jpg"); } imaImagenFondo = Toolkit.getDefaultToolkit().getImage(urlImagenFondo); //despliega la imagen graGraficaApplet.drawImage(imaImagenFondo, 0, 0, getWidth(), getHeight(), this); // Actualiza el Foreground. graGraficaApplet.setColor (getForeground()); paintAux(graGraficaApplet); // Dibuja la imagen actualizada graGrafico.drawImage (imaImagenApplet, 0, 0, this); }
565f9773-565b-4600-95b5-fa959b136884
public void paintAux(Graphics g) { //se despliegan el score en la esquina superior izq g.setColor(Color.RED); g.drawString("Score: " + iScore, 20, 35); if (entBarra != null && entBloque != null && entProyectil != null && encBloques.size() > 0) { //Dibuja la imagen de la barra en la posicion actualizada g.drawImage(entBarra.getImagen(), entBarra.getX(), entBarra.getY(), this); g.drawImage(entProyectil.getImagen(), entProyectil.getX(), entProyectil.getY(), this); //se corre la animacion si es verdadero el boolean if(iContador < 3) { iContador++; g.drawImage(Toolkit.getDefaultToolkit().getImage(aniBloqueDestruido.getImagen()), aniPosX, aniPosY, this); } //Dibuja los bloques for (Object encBloque : encBloques) { Entidad bloque = (Entidad)encBloque; g.drawImage(bloque.getImagen(), bloque.getX(), bloque.getY(), this); } } }
a884fe92-584b-490a-8418-34d9d38ad085
@Override public void keyTyped(KeyEvent e) { }
3c6acf30-454d-490c-a9ab-17a61f4da3f3
public Entidad(int iX, int iY ,Image imaImagen) { this.iX = iX; this.iY = iY; imiIcono = new ImageIcon(imaImagen); this.iVelocidad = 1; // default 1 en velocidad }
692b401e-d2f7-4e5a-993f-43c24155e740
public Entidad(int iX, int iY ,ImageIcon icoImagen) { this.iX = iX; this.iY = iY; imiIcono = icoImagen; this.iVelocidad = 1; // default 1 en velocidad }
8d06f8bd-d0e7-4574-82c5-407988eb3d93
public void setX(int iX) { this.iX = iX; }
a3b12c47-ae6e-4486-94eb-b0f8a187f5cb
public int getX() { return iX; }
661d14c0-c9b8-4407-a7fe-c4d66f62c4a6
public void setY(int iY) { this.iY = iY; }
2847b2d1-e41c-4845-b1f7-06e80797d37d
public int getY() { return iY; }
964efd15-831e-4230-be36-d48e6fadfc39
public void setImageIcon(ImageIcon imiIcono) { this.imiIcono = imiIcono; }
596cf3a4-c342-4b9e-ad77-3cd38de53d26
public ImageIcon getImageIcon() { return imiIcono; }
341ac41a-0526-492f-a68e-4ebd7775dace
public void setImagen(Image imaImagen) { this.imiIcono = new ImageIcon(imaImagen); }
ea7d7a54-3a02-474b-938a-dc20411a5a29
public Image getImagen() { return imiIcono.getImage(); }
abe3feb2-6eb0-4c48-90c0-6c1169c514e6
public void setVelocidad(int iVelocidad) { this.iVelocidad = iVelocidad; }
8e91dac8-3a1d-4c0c-a95a-5c38954f381a
public int getVelocidad() { return iVelocidad; }
9bc92590-b1f3-438d-964b-68c6c6ad483d
public int getAncho() { return imiIcono.getIconWidth(); }
325b25f9-a6ce-4802-be90-98fb2a57062c
public int getAlto() { return imiIcono.getIconHeight(); }
c916af72-c551-43c9-a5c6-e0dd7422122f
public void arriba() { this.setY(this.getY() - iVelocidad); }
95a82872-20b3-42b0-b3f2-e52404dc47f8
public void abajo() { this.setY(this.getY() + iVelocidad); }
3c07340b-438d-4ac0-85de-4e6f2e0af928
public void derecha() { this.setX(this.getX() + iVelocidad); }
6a3f700e-a12b-4d6b-a4e9-39d76abc5a4b
public void izquierda() { this.setX(this.getX() - iVelocidad); }
4d9f03df-a762-4813-8873-c7affd6b4b19
public boolean colisiona(Entidad aniParametro) { // creo un objeto rectangulo a partir de este objeto Entidad Rectangle recObjeto = new Rectangle(this.getX(),this.getY(), this.getAncho(), this.getAlto()); // creo un objeto rectangulo a partir del objeto Entidad parametro Rectangle recParametro = new Rectangle(aniParametro.getX(), aniParametro.getY(), aniParametro.getAncho(), aniParametro.getAlto()); // si se colisionan regreso verdadero, sino regreso falso return recObjeto.intersects(recParametro); }
b9d49392-3a99-4403-9e83-3eecb52209d6
public boolean colisiona(int iX, int iY) { // creo un objeto rectangulo a partir de este objeto Personaje Rectangle recObjeto = new Rectangle(this.getX(),this.getY(), this.getAncho(), this.getAlto()); // si se colisionan regreso verdadero, sino regreso falso return recObjeto.contains(iX, iY); }
ab0db59b-0c96-43c6-9c1d-6073ce9b7a0e
public VennDiagram(double[][] centers, double[] diameters, double[] areas, double[] residuals, String[] circleLabels, String[] residualLabels, double[] colors, double stress, double stress01, double stress05) { this.centers = centers; this.diameters = diameters; this.areas = areas; this.residuals = residuals; this.colors = colors; this.circleLabels = circleLabels; this.residualLabels = residualLabels; this.stress = stress; this.stress01 = stress01; this.stress05 = stress05; }
e6404bf5-98e9-4ba2-a3a9-4ff1f388630d
public VennAnalytic() { stepsize = .01; minStress = .000001; isEqualArea = false; }
3ec386c3-85f7-437f-9aea-43b1629b0e5f
public VennDiagram compute(VennData vd) { String[][] data = vd.data; double[] areas = vd.areas; boolean isAreas = vd.isAreas; if (isAreas) processAreaData(data, areas); else processElementData(data); computeInitialConfiguration(); scaleDiameters(); scaleConfiguration(); minimizeGlobal(); minimizeLocal(); return collectResults(); }
e8d5059e-bb66-4792-9908-6157f00c90f5
private VennDiagram collectResults() { double[] colors = new double[nCircles]; for (int j = 0; j < nCircles; j++) colors[j] = (double) (j + 1) / (nCircles + 1); double stress01 = 0; double stress05 = 0; if (nCircles > 2) { stress01 = Math.exp(.909 * (nCircles - 6.105)) / (1 + Math.exp(.909 * (nCircles - 6.105))); stress05 = Math.exp(.900 * (nCircles - 5.129)) / (1 + Math.exp(.900 * (nCircles - 5.129))); } double[] residuals = new double[nPolygons - 1]; String[] residualLabels = new String[nPolygons - 1]; double area = 0; int nonZero = 0; for (int i = 1; i < nPolygons; i++) { residuals[i - 1] = polyAreas[i] - polyHats[i]; char[] c = encode(i); String s = ""; for (int j = 0; j < c.length; j++) { if (c[j] == '1') s += (circleLabels[j] + "&"); } area += polyAreas[i]; if (residuals[i - 1] != 0) nonZero++; s = s.substring(0, s.length() - 1); residualLabels[i - 1] = s; // System.out.println(residualLabels[i - 1] + " " + // residuals[i-1]+" "+polyAreas[i] + " " + polyData[i]); } // double cut = area / nonZero; // for (int i = 0; i < residuals.length; i++) { // if (Math.abs(residuals[i]) > cut) // System.out.println("OUTLIER---> "+residualLabels[i]+" "+residuals[i]); // } //System.out.println("stress = " + stress + ", stress01 = " + stress01 + ", stress05 = " + stress05); return new VennDiagram(centers, diameters, polyAreas, residuals, circleLabels, residualLabels, colors, stress, stress01, stress05); }
4d101eaa-6e83-4001-b851-646db38bd6e3
private void processAreaData(String[][] data, double[] areas) { HashMap<String, Double> sets = new HashMap<String, Double>(); for (int i = 0; i < data.length; i++) { String[] s = data[i][0].split("&"); for (int j = 0; j < s.length; j++) { if (!sets.containsKey(s[j])) { Double cat = new Double(sets.size()); sets.put(s[j], cat); } } } circleLabels = new String[sets.size()]; Set keys = sets.keySet(); Iterator it = keys.iterator(); while (it.hasNext()) { String key = (String) it.next(); int j = sets.get(key).intValue(); circleLabels[j] = key; } nRows = data.length; nCircles = sets.size(); nPolygons = (int) Math.pow(2, nCircles); polyData = new double[nPolygons]; polyAreas = new double[nPolygons]; polyHats = new double[nPolygons]; circleData = new double[nCircles]; centers = new double[nCircles][2]; double totalArea = 0.0; for (int i = 0; i < nRows; i++) totalArea += areas[i]; for (int i = 0; i < nRows; i++) { int[] subsets = new int[nCircles]; String[] s = data[i][0].split("&"); for (int j = 0; j < s.length; j++) { int jj = sets.get(s[j]).intValue(); subsets[jj] = 1; } int k = decode(subsets); polyData[k] = areas[i]; for (int j = 0; j < nCircles; j++) { if (subsets[j] > 0) circleData[j] += areas[i]; } } for (int i = 0; i < polyData.length; i++) polyData[i] = polyData[i] / totalArea; for (int j = 0; j < nCircles; j++) { circleData[j] = circleData[j] / totalArea; if (isEqualArea) circleData[j] = 1; } }
a91aa676-8788-49e7-97a5-e4559a4c463d
private void processElementData(String[][] data) { HashMap<String, Double>[] categories = new HashMap[2]; categories[0] = new HashMap<String, Double>(); categories[1] = new HashMap<String, Double>(); for (int i = 0; i < data.length; i++) { for (int j = 0; j < 2; j++) { if (!categories[j].containsKey(data[i][j])) { Double cat = new Double(categories[j].size()); categories[j].put(data[i][j], cat); } } } circleLabels = new String[categories[1].size()]; Set<String> keys = categories[1].keySet(); Iterator it = keys.iterator(); while (it.hasNext()) { String key = (String) it.next(); int j = categories[1].get(key).intValue(); circleLabels[j] = key; } nRows = data.length; nCircles = categories[1].size(); nPolygons = (int) Math.pow(2, nCircles); polyData = new double[nPolygons]; polyAreas = new double[nPolygons]; polyHats = new double[nPolygons]; circleData = new double[nCircles]; centers = new double[nCircles][2]; int[][] subsets = new int[categories[0].size()][nCircles]; for (int i = 0; i < nRows; i++) { int i1 = categories[0].get(data[i][0]).intValue(); int j1 = categories[1].get(data[i][1]).intValue(); subsets[i1][j1]++; } for (int i = 0; i < subsets.length; i++) updateCounts(subsets[i]); for (int i = 0; i < polyData.length; i++) polyData[i] = polyData[i] / nTot; for (int j = 0; j < nCircles; j++) { circleData[j] = circleData[j] / nTot; if (isEqualArea) circleData[j] = 1; } }
4c0da4f9-88bf-4fb0-b2d1-c970b4472df8
protected void updateCounts(int[] counts) { int index = decode(counts); polyData[index]++; for (int j = 0; j < counts.length; j++) { if (counts[j] > 0) circleData[j]++; } nTot++; }
30592836-2ebb-4ca7-9072-2c4fdc594bb7
private char[] encode(int index) { String s = Integer.toBinaryString(index); char[] c = s.toCharArray(); int offset = nCircles - c.length; char[] result = new char[nCircles]; for (int i = 0; i < offset; i++) result[i] = '0'; System.arraycopy(c, 0, result, offset, c.length); return result; }
0cd1b23b-e738-4477-ac70-8566d0b2f275
private int decode(int[] subsets) { String b = ""; for (int j = 0; j < subsets.length; j++) { if (subsets[j] > 0) b += '1'; else b += '0'; } return Integer.parseInt(b, 2); }
8a0f5a9b-f2b7-48a7-8490-7969a431494b
private void calculateAreas() { totalCount = 0; int size = 200; byte[][][] bis = new byte[nCircles][size][size]; double mins = Double.POSITIVE_INFINITY; double maxs = Double.NEGATIVE_INFINITY; for (int i = 0; i < nCircles; i++) { double radius = diameters[i] / 2; mins = Math.min(centers[i][0] - radius, mins); mins = Math.min(centers[i][1] - radius, mins); maxs = Math.max(centers[i][0] + radius, maxs); maxs = Math.max(centers[i][1] + radius, maxs); } for (int i = 0; i < nCircles; i++) { double xi = (centers[i][0] - mins) / (maxs - mins); double yi = (centers[i][1] - mins) / (maxs - mins); double di = diameters[i] / (maxs - mins); int r = (int) (di * size / 2.); int r2 = r * r; int cx = (int) (xi * size); int cy = (int) (size - yi * size); for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { if ((x - cx) * (x - cx) + (y - cy) * (y - cy) < r2) bis[i][x][y] = 1; } } } for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { int[] counts = new int[nCircles]; int count = 0; for (int j = 0; j < nCircles; j++) { if (bis[j][x][y] == 1) { counts[j]++; count++; } } if (count > 0) updatePixels(counts); } } if (totalCount == 0) return; for (int i = 0; i < nPolygons; i++) polyAreas[i] = 100 * polyAreas[i] / totalCount; }
afe9f064-281d-4ec4-932a-9f38e3dff6d7
private void updatePixels(int[] counts) { int index = decode(counts); polyAreas[index]++; totalCount++; }
975c0358-3347-41bc-b34e-b501be91ff07
public void computeInitialConfiguration() { double[][] s = computeDistanceMatrix(); if (s == null) { fixedStart(); return; } double[] q = new double[nCircles]; computeScalarProducts(nCircles, s, q); Eigen.eigenSymmetric(s, s, q); double rms = Math.sqrt(q[0]) + Math.sqrt(q[1]); if (Double.isNaN(rms) || rms < .1) { fixedStart(); return; } for (int i = 0; i < nCircles; i++) { centers[i][0] = .5 + .25 * s[i][0] * Math.sqrt(q[0]); centers[i][1] = .5 + .25 * s[i][1] * Math.sqrt(q[1]); } }
56813e0b-d52f-4a56-bf20-151012432106
private void fixedStart() { //System.out.println("Singularity: fixed start based on circles."); double theta = Math.PI / 2; double delta = 2 * Math.PI / nCircles; for (int i = 0; i < nCircles; i++) { centers[i][0] = .5 + Math.cos(theta); centers[i][1] = .5 + Math.sin(theta); theta -= delta; } }
5dda78f3-c0fa-4c85-8470-74c1e3e62f11
private double[][] computeDistanceMatrix() { int nIntersections = 0; double[][] s = new double[nCircles][nCircles]; for (int i = 0; i < nPolygons; i++) { char[] c = encode(i); for (int j = 0; j < c.length; j++) { if (c[j] == '0') continue; for (int k = j + 1; k < c.length; k++) { if (c[k] == '0') continue; s[j][k] += polyData[i]; s[k][j] = s[j][k]; nIntersections++; } } } for (int j = 0; j < nCircles; j++) { s[j][j] = 0; for (int k = 0; k < j; k++) { s[j][k] = 1 - s[j][k] / (circleData[j] + circleData[k]); s[k][j] = s[j][k]; } } if (nIntersections < 1) return null; else return s; }
da9bb426-0751-4a33-be32-b3631e42c18a
private void computeScalarProducts(int nPoints, double[][] s, double[] q) { double rms = 0.; for (int i = 1; i < nPoints; i++) { for (int j = 0; j < i; j++) { double dij = s[i][j] * s[i][j]; rms += dij + dij; q[i] += dij; q[j] += dij; } } rms = rms / (nPoints * nPoints); double dsm; for (int i = 0; i < nPoints; i++) { for (int j = 0; j <= i; j++) { if (i == j) dsm = 0.; else dsm = s[i][j] * s[i][j]; s[i][j] = ((q[i] + q[j]) / nPoints - rms - dsm) / 2.; s[j][i] = s[i][j]; } } }
e94f43a9-f375-4ae7-a0db-2c0343de1625
private void scaleDiameters() { diameters = new double[nCircles]; for (int j = 0; j < nCircles; j++) diameters[j] = 2 * Math.sqrt(circleData[j] / Math.PI / nCircles); }
8c8fa4cc-662f-454a-b57e-8be68589426a
private void rescaleDiameters(double[] realDiameters, int iteration) { if (iteration > 5) { return; } else if (iteration == 0) { double averageDiameter = 0; for (int j = 0; j < nCircles; j++) averageDiameter += realDiameters[j]; averageDiameter /= nCircles; for (int j = 0; j < nCircles; j++) diameters[j] = averageDiameter; } else if (iteration < 5) { for (int j = 0; j < nCircles; j++) diameters[j] = diameters[j] - (iteration / 5.0) * (diameters[j] - realDiameters[j]); } else { for (int j = 0; j < nCircles; j++) diameters[j] = realDiameters[j]; } }
a2cd1de2-db00-4b78-8f7b-6c6d54a26979
private void scaleConfiguration() { double vc = 0; for (int j = 0; j < 2; j++) { double mc = 0; for (int k = 0; k < nCircles; k++) mc += centers[k][j]; mc /= nCircles; for (int k = 0; k < nCircles; k++) { centers[k][j] -= mc; vc += centers[k][j] * centers[k][j]; } } vc = 10. * Math.sqrt(vc / (2 * nCircles)); if (vc > 0) { for (int j = 0; j < 2; j++) { for (int k = 0; k < nCircles; k++) centers[k][j] /= vc; } } }
b79bd486-bae5-45f8-ad98-1b85b52ea3ab
private double computeStress() { /* regression through origin */ calculateAreas(); if (totalCount == 0) { scaleConfiguration(); calculateAreas(); } double xx = 0; double xy = 0; int n = polyData.length; double sst = 0; for (int i = 1; i < n; i++) { double x = polyData[i]; double y = polyAreas[i]; xy += x * y; xx += x * x; sst += y * y; } double slope = xy / xx; double sse = 0; for (int i = 1; i < n; i++) { double x = polyData[i]; double y = polyAreas[i]; double yhat = x * slope; polyHats[i] = yhat; sse += (y - yhat) * (y - yhat); } return sse / sst; }
098f8db6-085d-4316-8d2d-f00a8827569e
private void minimizeGlobal() { double[] initialDiameters = new double[nCircles]; System.arraycopy(diameters, 0, initialDiameters, 0, nCircles); double[][] previousCenters = new double[nCircles][2]; copyCircles(centers, previousCenters); double lastStress = 1; for (int iter = 0; iter < 50; iter++) { rescaleDiameters(initialDiameters, iter); recenter(); stress = computeStress(); if (stress > lastStress) copyCircles(previousCenters, centers); else copyCircles(centers, previousCenters); //System.out.println("global iteration, loss " + iter + " " + stress); if (iter > 10 && (stress < minStress || lastStress - stress < minStress)) break; moveGlobal(); lastStress = stress; } rescaleDiameters(initialDiameters, 50); recenter(); stress = computeStress(); }
d1c460bb-bc1b-4644-962c-bc545812525e
private void minimizeLocal() { double[] initialDiameters = new double[nCircles]; System.arraycopy(diameters, 0, initialDiameters, 0, nCircles); double[][] initialCenters = new double[nCircles][2]; copyCircles(centers, initialCenters); double[][] previousCenters = new double[nCircles][2]; copyCircles(centers, previousCenters); double previousStress = stress; double lastStress = 1; for (int iter = 0; iter < 50; iter++) { rescaleDiameters(initialDiameters, iter); recenter(); stress = computeStress(); //System.out.println("local iteration, loss " + iter + " " + stress); if (stress > lastStress) copyCircles(previousCenters, centers); else copyCircles(centers, previousCenters); if (iter > 10 && (stress < minStress || lastStress - stress < minStress)) break; moveLocal(); lastStress = stress; } rescaleDiameters(initialDiameters, 50); if (previousStress < stress) copyCircles(initialCenters, centers); recenter(); stress = computeStress(); }
9637893d-58ae-4ae5-ba6c-93f80d612026
private void moveGlobal() { double[][] gradients = new double[nCircles][2]; for (int i = 0; i < nPolygons; i++) { String s = Integer.toBinaryString(i); char[] c = s.toCharArray(); int offset = nCircles - c.length; for (int j = 0; j < c.length; j++) { if (c[j] == '0') continue; int jo = j + offset; for (int k = j + 1; k < c.length; k++) { if (c[k] == '0') continue; int ko = k + offset; double resid = polyAreas[i] - polyHats[i]; double dx = resid * stepsize * (centers[jo][0] - centers[ko][0]); double dy = resid * stepsize * (centers[jo][1] - centers[ko][1]); gradients[jo][0] += dx; gradients[jo][1] += dy; gradients[ko][0] -= dx; gradients[ko][1] -= dy; } } } for (int i = 0; i < nCircles; i++) { centers[i][0] += gradients[i][0]; centers[i][1] += gradients[i][1]; } }
17416419-1c51-48bc-b6db-98d10a1802c9
private void moveLocal() { double[][] gradients = new double[nCircles][2]; for (int i = 0; i < nCircles; i++) { centers[i][0] += stepsize; double xPlus = computeStress(); centers[i][0] -= 2 * stepsize; double xMinus = computeStress(); centers[i][0] += stepsize; if (xPlus < xMinus) gradients[i][0] = stepsize; else gradients[i][0] = -stepsize; centers[i][1] += stepsize; double yPlus = computeStress(); centers[i][1] -= 2 * stepsize; double yMinus = computeStress(); centers[i][1] += stepsize; if (yPlus < yMinus) gradients[i][1] = stepsize; else gradients[i][1] = -stepsize; } for (int i = 0; i < nCircles; i++) { centers[i][0] += gradients[i][0]; centers[i][1] += gradients[i][1]; } }
1e9f6a31-bec6-4634-90eb-5bcc023f1109
private void recenter() { double cx = 0; double cy = 0; for (int i = 0; i < nCircles; i++) { cx += centers[i][0]; cy += centers[i][1]; } cx = cx / nCircles; cy = cy / nCircles; for (int i = 0; i < nCircles; i++) { centers[i][0] = .5 + centers[i][0] - cx; centers[i][1] = .5 + centers[i][1] - cy; } }
0fa22c6c-3d4b-4af4-8b2b-84803abefe93
private void copyCircles(double[][] a, double[][] b) { for (int i = 0; i < nCircles; i++) System.arraycopy(a[i], 0, b[i], 0, 2); }
ca8a8e17-8ea5-4756-b94d-5670d22f1582
private Eigen() {}
5f070764-7e10-44c1-8e28-20f7999f7288
public static void eigenSymmetric(double[][] a, double[][] eigenvectors, double[] eigenvalues) { // eigenvalues and eigenvectors for dense system (A-lambda*I)x = 0 // returns eigenvectors in columns double[] e = new double[eigenvalues.length]; Eigen.tred2(a, eigenvectors, eigenvalues, e); Eigen.imtql2(eigenvectors, eigenvalues, e); Eigen.sort(eigenvalues, eigenvectors); }
54389451-5621-4593-a2ea-c383aa8706cc
private static void tred2(double[][] a, double[][] z, double[] d, double[] e) { /* Tridiagonalization of symmetric matrix. * This generateMethod is a translation of the Algol procedure tred2(), * Wilkinson and Reinsch, Handbook for Auto. Comp., Vol II-Linear Algebra, (1971). * Converted to Java by Leland Wilkinson. */ int i, j, jp1, k, l, n; double f, g, h, hh, scale; n = d.length; for (i = 0; i < n; i++) { for (j = 0; j <= i; j++) z[i][j] = a[i][j]; } if (n > 1) { for (i = n - 1; i > 0; i--) { l = i - 1; h = 0.0; scale = 0.0; if (l > 0) { for (k = 0; k <= l; k++) scale += Math.abs(z[i][k]); } if (scale == 0.0) e[i] = z[i][l]; else { for (k = 0; k <= l; k++) { z[i][k] /= scale; h += z[i][k] * z[i][k]; } f = z[i][l]; g = f < 0.0 ? Math.sqrt(h) : -Math.sqrt(h); e[i] = scale * g; h -= f * g; z[i][l] = f - g; f = 0.0; for (j = 0; j <= l; j++) { z[j][i] = z[i][j] / (scale * h); g = 0.0; for (k = 0; k <= j; k++) g += z[j][k] * z[i][k]; jp1 = j + 1; if (l >= jp1) { for (k = jp1; k <= l; k++) g += z[k][j] * z[i][k]; } e[j] = g / h; f += e[j] * z[i][j]; } hh = f / (h + h); for (j = 0; j <= l; j++) { f = z[i][j]; g = e[j] - hh * f; e[j] = g; for (k = 0; k <= j; k++) z[j][k] = z[j][k] - f * e[k] - g * z[i][k]; } for (k = 0; k <= l; k++) z[i][k] *= scale; } d[i] = h; } } d[0] = 0.0; e[0] = 0.0; for (i = 0; i < n; i++) { l = i - 1; if (d[i] != 0.0) { for (j = 0; j <= l; j++) { g = 0.0; for (k = 0; k <= l; k++) g += z[i][k] * z[k][j]; for (k = 0; k <= l; k++) z[k][j] -= g * z[k][i]; } } d[i] = z[i][i]; z[i][i] = 1.0; if (l >= 0) { for (j = 0; j <= l; j++) { z[i][j] = 0.0; z[j][i] = 0.0; } } } }
685be558-e505-49fb-9799-ad42ba9620c7
private static int imtql2(double[][] z, double[] d, double[] e) { /* Implicit QL iterations on tridiagonalized matrix. * This generateMethod is a translation of the Algol procedure imtql2(), * Wilkinson and Reinsch, Handbook for Auto. Comp., Vol II-Linear Algebra, (1971). * Converted to Java by Leland Wilkinson. */ int i, ii, ip1, j, k, l, lp1, m, mm, mml, n; double b, c, f, g, p, r, s; double EPSILON = 1.0e-15; n = d.length; if (n == 1) return 0; for (i = 1; i < n; i++) e[i - 1] = e[i]; e[n - 1] = 0.0; mm = 0; for (l = 0; l < n; l++) { lp1 = l + 1; j = 0; for (; ;) { for (m = l; m < n; m++) { mm = m; if (m == n - 1) break; if (Math.abs(e[m]) <= EPSILON * (Math.abs(d[m]) + Math.abs(d[m + 1]))) break; } m = mm; p = d[l]; if (m == l) break; if (j == 30) return l; j++; g = (d[lp1] - p) / (2.0 * e[l]); r = Math.sqrt(g * g + 1.0); g = d[m] - p + e[l] / (g + (g < 0.0 ? -r : r)); s = 1.0; c = 1.0; p = 0.0; mml = m - l; for (ii = 1; ii <= mml; ii++) { i = m - ii; ip1 = i + 1; f = s * e[i]; b = c * e[i]; if (Math.abs(f) >= Math.abs(g)) { c = g / f; r = Math.sqrt(c * c + 1.0); e[ip1] = f * r; s = 1.0 / r; c *= s; } else { s = f / g; r = Math.sqrt(s * s + 1.0); e[ip1] = g * r; c = 1.0 / r; s *= c; } g = d[ip1] - p; r = (d[i] - g) * s + 2.0 * c * b; p = s * r; d[ip1] = g + p; g = c * r - b; for (k = 0; k < n; k++) { f = z[k][ip1]; z[k][ip1] = s * z[k][i] + c * f; z[k][i] = c * z[k][i] - s * f; } } d[l] -= p; e[l] = g; e[m] = 0.0; } } return 0; }
917ef62f-9537-400c-a307-828925365740
private static void sort(double[] d, double[][] z) { int l; int k; int j; int i; int ip1; double p; int ii; int n = d.length; /* Sort by eigenvalues (descending) */ for (l = 1; l <= n; l = 3 * l + 1) ; while (l > 2) { l = l / 3; k = n - l; for (j = 0; j < k; j++) { i = j; while (i >= 0) { ip1 = i + l; if (d[i] < d[ip1] || Double.isNaN(d[i])) { p = d[i]; d[i] = d[ip1]; d[ip1] = p; for (ii = 0; ii < n; ii++) { p = z[ii][i]; z[ii][i] = z[ii][ip1]; z[ii][ip1] = p; } i = i - l; } else break; } } } }
0d979c41-de42-4b0a-96bf-fdf22ceeba54
private Venn() {}
dcc18ab9-c382-457f-8054-ac67ff022415
public static void main(String[] argv) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { process(); } }); }
ba72ccf5-a870-4a1d-b35a-897750697fb8
public void run() { process(); }
424950df-bf21-4e35-8711-1cad06eb8943
private static void process() { String fileName = FilePicker.loadFile(); File file = new File(fileName); VennData dv = FileReader.getData(file); VennAnalytic va = new VennAnalytic(); VennDiagram vd = va.compute(dv); new VennFrame(vd); }
a1d524ac-d234-4c0a-b424-4da07e4bd1b6
public VennFrame(VennDiagram vd) { Container con = this.getContentPane(); con.setBackground(Color.white); VennCanvas vc = new VennCanvas(vd); con.add(vc); setTitle("Venn/Euler Diagram"); setBounds(0, 0, WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setVisible(true); }
5828c16b-e3f0-485a-9aff-3abcd1ae0ed4
public VennCanvas(VennDiagram venn) { size = 700; bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); frc = bi.createGraphics().getFontRenderContext(); size *= .8; centers = venn.centers; diameters = venn.diameters; colors = venn.colors; labels = venn.circleLabels; mins = Double.POSITIVE_INFINITY; maxs = Double.NEGATIVE_INFINITY; for (int i = 0; i < centers.length; i++) { double margin = diameters[i] / 2; mins = Math.min(centers[i][0] - margin, mins); mins = Math.min(centers[i][1] - margin, mins); maxs = Math.max(centers[i][0] + margin, maxs); maxs = Math.max(centers[i][1] + margin, maxs); } }
a15a60c1-4a04-4488-a8af-554ae1d9d978
public void paintComponent(Graphics g) { Graphics2D g2D = (Graphics2D) bi.getGraphics(); g2D.setColor(Color.WHITE); g2D.fillRect(0, 0, bi.getHeight(), bi.getWidth()); for (int i = 0; i < centers.length; i++) { double xi = (centers[i][0] - mins) / (maxs - mins); double yi = (centers[i][1] - mins) / (maxs - mins); double pi = diameters[i] / (maxs - mins); int pointSize = (int) (pi * size); int x = 50 + (int) (xi * size); int y = 50 + (int) (size - yi * size); Color color = rainbow(colors[i], .4f); g2D.setColor(color); g2D.fillOval(x - pointSize / 2, y - pointSize / 2, pointSize, pointSize); g2D.setColor(color.BLACK); double[] wh = getWidthAndHeight(labels[i], g2D); g2D.drawString(labels[i], x - (int) wh[0] / 2, y + (int) wh[1] / 2); } Graphics2D gg = (Graphics2D) g; gg.drawImage(bi, 2, 2, this); }
c042b61c-69f7-485e-9eda-d97820b3adb7
private static Color rainbow(double value, float transparency) { /* blue to red, approximately by wavelength */ float v = (float) value * 255.f; float vmin = 0; float vmax = 255; float range = vmax - vmin; if (v < vmin + 0.25f * range) return new Color(0.f, 4.f * (v - vmin) / range, 1.f, transparency); else if (v < vmin + 0.5 * range) return new Color(0.f, 1.f, 1.f + 4.f * (vmin + 0.25f * range - v) / range, transparency); else if (v < vmin + 0.75 * range) return new Color(4.f * (v - vmin - 0.5f * range) / range, 1.f, 0, transparency); else return new Color(1.f, 1.f + 4.f * (vmin + 0.75f * range - v) / range, 0, transparency); }
1095a789-1d05-4c94-a5b3-0cd2fe458af5
public double[] getWidthAndHeight(String s, Graphics2D g2D) { Font font = g2D.getFont(); Rectangle2D bounds = font.getStringBounds(s, frc); double[] wh = new double[2]; wh[0] = bounds.getWidth(); wh[1] = .7 * bounds.getHeight(); return wh; }
4d0da0e3-bea7-48c0-97ee-573b6f15bdae
public static VennData getData(File fname) { String[][] data; double[] areas; String record; java.io.BufferedReader fin; identifySeparator(fname); nRows = 0; try { fin = new java.io.BufferedReader(new java.io.FileReader(fname)); while (fin.readLine() != null) nRows++; fin.close(); nRows--; data = new String[nRows][nCols]; areas = new double[nRows]; fin = new java.io.BufferedReader(new java.io.FileReader(fname)); record = fin.readLine(); //header record = compressBlanks(record); String[] row = split(record); labels = new String[nCols]; System.arraycopy(row, 0, labels, 0, nCols); for (int i = 0; i < nRows; i++) { record = fin.readLine(); if (record == null) break; record = compressBlanks(record); row = split(record); if (i == 0) { if (row.length == 2) isAreas = isDouble(row[1]); else if (row.length > 2) isBinaryMatrix = true; } if (isAreas) { data[i][0] = row[0]; areas[i] = Double.parseDouble(row[1]); } else if (isBinaryMatrix) { System.arraycopy(row, 0, data[i], 0, nCols); } else { System.arraycopy(row, 0, data[i], 0, 2); } } fin.close(); if (isBinaryMatrix) data = reformData(data); return new VennData(data, areas, isAreas); } catch (java.io.IOException ie) { System.err.println("I/O exception in getData"); return null; } }
35257de0-1f9a-4531-9186-30df216685bb
private static String[][] reformData(String[][] data) { ArrayList d = new ArrayList(nRows); for (int i = 0; i < nRows; i++) { for (int j = 0; j < nCols - 1; j++) { data[i][j] = data[i][j].trim(); if (data[i][j].equals("1")) { String[] record = new String[2]; record[0] = labels[j]; record[1] = data[i][nCols - 1]; d.add(record); } } } String[][] result = new String[d.size()][2]; for (int i = 0; i < d.size(); i++) { Object[] o = (Object[]) d.get(i); result[i][0] = (String) o[0]; result[i][1] = (String) o[1]; } return result; }
af0fa4bc-6936-4424-a31e-73c284ee6542
private static boolean isDouble(String s) { try { Double.parseDouble(s); } catch (NumberFormatException ex) { return false; } return true; }
4e9b4d47-db7b-4b14-9e98-fa682e07944d
private static void identifySeparator(File fname) { separators.put(",", new int[2]); separators.put(";", new int[2]); separators.put(":", new int[2]); separators.put("|", new int[2]); separators.put("\t", new int[2]); separators.put(" ", new int[2]); java.io.BufferedReader fin; try { fin = new java.io.BufferedReader(new java.io.FileReader(fname)); String record; int n = 0; while ((record = fin.readLine()) != null) { record = compressBlanks(record); if (record.length() == 0) continue; countSeparators(record); deleteInvalidSeparators(n); n++; if (n > 100 || separators.size() == 0) break; } if (separators.size() == 0) { if (n == 1) { separator = ""; nCols = 1; } } else { Object[] keys = separators.keySet().toArray(); separator = (String) keys[0]; nCols = 1 + ((int[]) separators.get(separator))[1]; } fin.close(); } catch (java.io.IOException ie) { System.err.println("I/O exception in computeVariableTypes"); } }
253b25ec-38bb-488e-8efa-b27ce5e94758
private static void countSeparators(String record) { boolean isUnquoted = true; for (int i = 0; i < record.length(); i++) { if (record.charAt(i) == '"') isUnquoted = !isUnquoted; if (isUnquoted) { Object[] keys = separators.keySet().toArray(); for (int j = 0; j < keys.length; j++) { String key = (String) keys[j]; if (record.substring(i, i + 1).equals(key)) { int[] counts = (int[]) separators.get(key); counts[0]++; break; } } } } }
58fcf57d-39d1-4999-83d0-1121fb11742d
private static void deleteInvalidSeparators(int n) { Object[] keys = separators.keySet().toArray(); for (int j = 0; j < keys.length; j++) { String key = (String) keys[j]; int[] counts = (int[]) separators.get(key); if (counts[0] == 0 || n > 0 && counts[0] != counts[1]) { separators.remove(key); } else { counts[1] = counts[0]; counts[0] = 0; separators.put(key, counts); } } }
b9ae699f-8a3c-46aa-b2e3-dc2e75822842
private static String[] split(String record) { String[] row = new String[nCols]; boolean isUnquoted = true; int i0 = 0; int col = 0; for (int i = 0; i < record.length(); i++) { if (record.charAt(i) == '"') isUnquoted = !isUnquoted; if (isUnquoted) { if (record.substring(i, i + 1).equals(separator)) { row[col] = record.substring(i0, i); row[col] = trimSpaces(row[col]); row[col] = trimQuotes(row[col]); col++; i0 = i + 1; } else if (i == record.length() - 1) { row[col] = record.substring(i0, i + 1); row[col] = trimSpaces(row[col]); row[col] = trimQuotes(row[col]); } } } if (row[nCols - 1] == null) row[nCols - 1] = ""; return row; }
751184b9-9479-48f5-ac3e-7d2ac4907afd
private static String trimSpaces(String s) { return s.trim(); }
66301cfd-f88d-4e71-badf-5da1fda7598a
private static String trimQuotes(String s) { if (s.startsWith("\"")) s = s.substring(1, s.length()); if (s.endsWith("\"")) s = s.substring(0, s.length() - 1); return s; }
efab051e-4b31-47fb-9722-1c76289d64e7
private static String compressBlanks(String record) { return record.replaceAll(" {2,}", " ").trim(); }
81469964-91e0-44b4-b835-b28a14b40d3c
public static String loadFile() { FileDialog fd = new FileDialog(new Frame(), "Open", FileDialog.LOAD); fd.setLocation(50, 50); fd.setVisible(true); return fd.getDirectory() + fd.getFile(); }
f97ed7e2-9e6f-4bc6-bc7e-0048f5ba0dc0
public static String saveFile() { FileDialog fd = new FileDialog(new Frame(), "Save", FileDialog.SAVE); fd.setLocation(50, 50); fd.setVisible(true); return fd.getDirectory() + fd.getFile(); }
c811107e-557c-4434-b9bc-69d36b9ae90e
public VennData(String[][] data, double[] areas, boolean isAreas) { this.data = data; this.areas = areas; this.isAreas = isAreas; }
b4798969-a770-4315-a775-62eafd2ef7aa
public VennData(String[] data, double[] areas) { this.data = new String[data.length][2]; for (int i = 0; i < data.length; i++) this.data[i][0] = data[i]; this.areas = areas; this.isAreas = true; }
772bb278-c108-45d8-b581-1f299537d296
public VennData(String[] pair1, String[] pair2) { this.data = new String[pair1.length][2]; for (int i = 0; i < data.length; i++) { this.data[i][0] = pair1[i]; this.data[i][1] = pair2[i]; } this.isAreas = false; }
6be53f19-d767-4b89-9adf-3412a7e164e4
protected Connection getConnection() { try { connection = DriverManager.getConnection(DB_URL, "sa", ""); return connection; } catch (SQLException e) { throw new RuntimeException(e); } }
3aa75dc8-0860-4239-82e5-38e4ae6b756b
protected void closeResources() { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(pst); DbUtils.closeQuietly(st); DbUtils.closeQuietly(connection); }
5667764f-0d06-42de-a2e6-c773a2949a5c
protected void executeQuery(String queryString) { try { st = getConnection().createStatement(); st.executeUpdate(queryString); } catch (Exception e) { throw new RuntimeException(e); } finally { closeResources(); } }
f3d8182b-68ec-4bda-86b9-97b7d967d5bd
public void createSchema(String file) { executeSqlFromFile(getClassPathFile(file)); }
6b61f970-eba1-42ac-861d-4fa0981477a8
private String getClassPathFile(String fileName) { return getClass().getClassLoader().getResource(fileName).getFile(); }
e9cf7771-e72c-4961-a8de-2be1fc765592
private void executeSqlFromFile(String sqlFilePath) { Project project = new Project(); project.init(); SQLExec e = new SQLExec(); e.setProject(project); e.setTaskType("sql"); e.setTaskName("sql"); e.setSrc(new File(sqlFilePath)); e.setDriver("org.hsqldb.jdbcDriver"); e.setUserid("sa"); e.setPassword(""); e.setUrl(DB_URL); e.execute(); }
c2b40f66-8cfc-413c-8ab8-bef80a06743c
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("do"); if("insert_data".equals(action)){ try { new SetupDao().createSchema("example_data.sql"); } catch (Exception e) { e.printStackTrace(); } response.setHeader("Location", "Search"); response.setStatus(HttpServletResponse.SC_FOUND); } else if("clear_data".equals(action)){ try { new Dao().clearData(); } catch (SQLException e) { e.printStackTrace(); } response.setHeader("Location", "Search"); response.setStatus(HttpServletResponse.SC_FOUND); } }
6a3638f4-f4c9-40bf-a4c1-009d79bb8d15
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
4359906c-feee-4ea8-9dd5-6f8e41058846
public int getId() { return id; }
eac5168c-ab8b-4b8d-ac93-bb6fbad58e8e
public void setId(int id) { this.id = id; }
f6db1a60-cd8d-4bb5-9da8-fcf18eecb51c
public String getNimi() { return nimi; }
54f68864-1688-4f56-90c1-e350e332b167
public void setNimi(String nimi){ this.nimi = nimi; }
8b49fa88-0fe8-4a9a-9cb3-3d183093b503
public String getKood() { return kood; }
a97716e7-28eb-4bd1-8b4e-dd71906106e5
public void setKood(String kood) { this.kood = kood; }
22ce81d8-55af-4ccb-af0c-02b69476b5fb
public List<Object> findAllObjects() throws SQLException { List<Object> objects = new ArrayList<Object>(); try { st = getConnection().createStatement(); rs = st.executeQuery("SELECT * FROM unit"); while (rs.next()) { Object object = new Object(); object.setNimi(rs.getString(2)); object.setKood(rs.getString(3)); object.setId(rs.getInt(1)); objects.add(object); } } finally { closeResources(); } return objects; }
bda2139f-7872-4607-8926-4c820d97d4e6
public void deleteRow(String id) throws SQLException { int row = Integer.parseInt(id); st = getConnection().createStatement(); rs = st.executeQuery("DELETE FROM unit WHERE id=" + row); closeResources(); }
c9cbba57-4ad0-4d91-9536-2a19fb89dbbe
public void clearData() throws SQLException { st = getConnection().createStatement(); rs = st.executeQuery("TRUNCATE TABLE unit"); closeResources(); }
7728620f-df83-474e-9ecf-12855f41ebd3
public List<Object> search(String parameter) throws SQLException { List<Object> objects = new ArrayList<Object>(); try { st = getConnection().createStatement(); rs = st.executeQuery("SELECT * FROM unit WHERE UPPER(name) LIKE UPPER('%" + parameter + "%')"); while (rs.next()) { Object object = new Object(); object.setNimi(rs.getString(2)); object.setKood(rs.getString(3)); object.setId(rs.getInt(1)); objects.add(object); } } finally { closeResources(); } return objects; }
a656a049-9e66-4f59-9c11-39e7e00d6141
public void addRow(String name, String code) throws SQLException { st = getConnection().createStatement(); rs = st.executeQuery("INSERT INTO unit VALUES (NEXT VALUE FOR seq1,'" + name + "','" + code + "')"); closeResources(); }
dbb52e98-7594-4a13-b7cd-a6692dcd8e8a
public void contextInitialized(ServletContextEvent arg0) { try { new SetupDao().createSchema("schema.sql"); new SetupDao().createSchema("example.sql"); } catch (Exception e) { System.out.println(e); } }
fc6c2ac7-227c-4ad4-b251-d42550169cbd
public void contextDestroyed(ServletContextEvent arg0) { }