001: package de.anomic.ymage;
002:
003: import java.util.ArrayList;
004: import java.util.HashSet;
005:
006: public class ymageToolCircle {
007:
008: private static int[][] circles = new int[0][];
009:
010: private static int[] getCircleCoords(int radius) {
011: if ((radius - 1) < circles.length)
012: return circles[radius - 1];
013:
014: // read some lines from known circles
015: HashSet<String> crds = new HashSet<String>();
016: crds.add("0|0");
017: String co;
018: for (int i = Math.max(0, circles.length - 5); i < circles.length; i++) {
019: for (int j = 0; j < circles[i].length; j = j + 2) {
020: co = circles[i][j] + "|" + circles[i][j + 1];
021: if (!(crds.contains(co)))
022: crds.add(co);
023: }
024: }
025:
026: // copy old circles into new array
027: int[][] newCircles = new int[radius + 30][];
028: System.arraycopy(circles, 0, newCircles, 0, circles.length);
029:
030: // compute more lines in new circles
031: int x, y;
032: ArrayList<int[]> crc;
033: for (int r = circles.length; r < newCircles.length; r++) {
034: crc = new ArrayList<int[]>();
035: for (int a = 0; a <= 2 * (r + 1); a++) {
036: x = (int) ((r + 1) * Math.cos(Math.PI * a
037: / (4 * (r + 1))));
038: y = (int) ((r + 1) * Math.sin(Math.PI * a
039: / (4 * (r + 1))));
040: co = x + "|" + y;
041: if (!(crds.contains(co))) {
042: crc.add(new int[] { x, y });
043: crds.add(co);
044: }
045: x = (int) ((r + 0.5) * Math.cos(Math.PI * a
046: / (4 * (r + 1))));
047: y = (int) ((r + 0.5) * Math.sin(Math.PI * a
048: / (4 * (r + 1))));
049: co = x + "|" + y;
050: if (!(crds.contains(co))) {
051: crc.add(new int[] { x, y });
052: crds.add(co);
053: }
054: }
055: // put coordinates into array
056: //System.out.print("Radius " + r + " => " + crc.size() + " points: ");
057: newCircles[r] = new int[2 * (crc.size() - 1)];
058: int[] coords;
059: for (int i = 0; i < crc.size() - 1; i++) {
060: coords = crc.get(i);
061: newCircles[r][2 * i] = coords[0];
062: newCircles[r][2 * i + 1] = coords[1];
063: //System.out.print(circles[r][i][0] + "," +circles[r][i][1] + "; ");
064: }
065: //System.out.println();
066: }
067: crc = null;
068: crds = null;
069:
070: // move newCircles to circles array
071: circles = newCircles;
072: newCircles = null;
073:
074: // finally return wanted slice
075: return circles[radius - 1];
076: }
077:
078: public static void circle(ymageMatrix matrix, int xc, int yc,
079: int radius) {
080: if (radius == 0) {
081: matrix.plot(xc, yc);
082: } else {
083: int[] c = getCircleCoords(radius);
084: int x, y;
085: for (int i = (c.length / 2) - 1; i >= 0; i--) {
086: x = c[2 * i];
087: y = c[2 * i + 1];
088: matrix.plot(xc + x, yc - y - 1); // quadrant 1
089: matrix.plot(xc - x + 1, yc - y - 1); // quadrant 2
090: matrix.plot(xc + x, yc + y); // quadrant 4
091: matrix.plot(xc - x + 1, yc + y); // quadrant 3
092: }
093: }
094: }
095:
096: public static void circle(ymageMatrix matrix, int xc, int yc,
097: int radius, int fromArc, int toArc) {
098: // draws only a part of a circle
099: // arc is given in degree
100: if (radius == 0) {
101: matrix.plot(xc, yc);
102: } else {
103: int[] c = getCircleCoords(radius);
104: int q = c.length / 2;
105: int[][] c4 = new int[q * 4][];
106: for (int i = 0; i < q; i++) {
107: c4[i] = new int[] { c[2 * (i)], -c[2 * (i) + 1] - 1 }; // quadrant 1
108: c4[i + q] = new int[] { 1 - c[2 * (q - 1 - i)],
109: -c[2 * (q - 1 - i) + 1] - 1 }; // quadrant 2
110: c4[i + 2 * q] = new int[] { 1 - c[2 * (i)],
111: c[2 * (i) + 1] }; // quadrant 3
112: c4[i + 3 * q] = new int[] { c[2 * (q - 1 - i)],
113: c[2 * (q - 1 - i) + 1] }; // quadrant 4
114: }
115: for (int i = q * 4 * fromArc / 360; i < q * 4 * toArc / 360; i++) {
116: matrix.plot(xc + c4[i][0], yc + c4[i][1]);
117: }
118: }
119: }
120: }
|