001: // ymageGraph.java
002: // (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
003: // first published 22.05.2007 on http://yacy.net
004: //
005: // This is a part of YaCy, a peer-to-peer based web search engine
006: //
007: // $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
008: // $LastChangedRevision: 1986 $
009: // $LastChangedBy: orbiter $
010: //
011: // LICENSE
012: //
013: // This program is free software; you can redistribute it and/or modify
014: // it under the terms of the GNU General Public License as published by
015: // the Free Software Foundation; either version 2 of the License, or
016: // (at your option) any later version.
017: //
018: // This program is distributed in the hope that it will be useful,
019: // but WITHOUT ANY WARRANTY; without even the implied warranty of
020: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: // GNU General Public License for more details.
022: //
023: // You should have received a copy of the GNU General Public License
024: // along with this program; if not, write to the Free Software
025: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
026:
027: package de.anomic.ymage;
028:
029: import java.util.HashMap;
030: import java.util.HashSet;
031: import java.util.Iterator;
032: import java.util.Map;
033:
034: /* this class is a container for graph coordinates and it can draw such coordinates into a graph
035: * all coordinates are given in a artificial coordinate system, in the range from
036: * -1 to +1. The lower left point of the graph has the coordinate -1, -1 and the upper
037: * right is 1,1
038: * 0,0 is the center of the graph
039: */
040:
041: public class ymageGraph {
042:
043: // a ymageGraph is a set of points and borders between the points
044: // to reference the points, they must all have a nickname
045:
046: HashMap<String, coordinate> points;
047: HashSet<String> borders;
048: double leftmost, rightmost, topmost, bottommost;
049:
050: public ymageGraph() {
051: points = new HashMap<String, coordinate>();
052: borders = new HashSet<String>();
053: leftmost = 1.0;
054: rightmost = -1.0;
055: topmost = -1.0;
056: bottommost = 1.0;
057: }
058:
059: public coordinate getPoint(String name) {
060: return points.get(name);
061: }
062:
063: public coordinate[] getBorder(String name) {
064: int p = name.indexOf("$");
065: if (p < 0)
066: return null;
067: coordinate from = getPoint(name.substring(0, p));
068: coordinate to = getPoint(name.substring(p + 1));
069: if ((from == null) || (to == null))
070: return null;
071: return new coordinate[] { from, to };
072: }
073:
074: public coordinate addPoint(String name, double x, double y,
075: int layer) {
076: coordinate newc = new coordinate(x, y, layer);
077: coordinate oldc = points.put(name, newc);
078: assert oldc == null; // all add shall be unique
079: if (x > rightmost)
080: rightmost = x;
081: if (x < leftmost)
082: leftmost = x;
083: if (y > topmost)
084: topmost = y;
085: if (y < bottommost)
086: bottommost = y;
087: return newc;
088: }
089:
090: public boolean hasBorder(String fromPoint, String toPoint) {
091: return borders.contains(fromPoint + "-" + toPoint);
092: }
093:
094: public void setBorder(String fromPoint, String toPoint) {
095: coordinate from = points.get(fromPoint);
096: coordinate to = points.get(toPoint);
097: assert from != null;
098: assert to != null;
099: borders.add(fromPoint + "$" + toPoint);
100: }
101:
102: public class coordinate {
103: public double x, y;
104: public int layer;
105:
106: public coordinate(double x, double y, int layer) {
107: assert x >= -1;
108: assert x <= 1;
109: assert y >= -1;
110: assert y <= 1;
111: this .x = x;
112: this .y = y;
113: this .layer = layer;
114: }
115: }
116:
117: public void print() {
118: // for debug purpose: print out all coordinates
119: Iterator<Map.Entry<String, coordinate>> i = points.entrySet()
120: .iterator();
121: Map.Entry<String, coordinate> entry;
122: String name;
123: coordinate c;
124: while (i.hasNext()) {
125: entry = i.next();
126: name = entry.getKey();
127: c = entry.getValue();
128: System.out.println("point(" + c.x + ", " + c.y + ", "
129: + c.layer + ") [" + name + "]");
130: }
131: Iterator<String> j = borders.iterator();
132: while (j.hasNext()) {
133: System.out.println("border(" + j.next() + ")");
134: }
135: }
136:
137: public static final long color_back = 0xFFFFFF;
138: public static final long color_text = 0xAAAAAA;
139: private static final long color_dot = 0x11CC11;
140: private static final long color_line = 0x333333;
141: private static final long color_lineend = 0x666666;
142:
143: public ymageMatrix draw(int width, int height, int leftborder,
144: int rightborder, int topborder, int bottomborder) {
145: ymageMatrix image = new ymageMatrix(width, height,
146: ymageMatrix.MODE_SUB, color_back);
147: double xfactor = ((rightmost - leftmost) == 0.0) ? 0.0 : (width
148: - leftborder - rightborder)
149: / (rightmost - leftmost);
150: double yfactor = ((topmost - bottommost) == 0.0) ? 0.0
151: : (height - topborder - bottomborder)
152: / (topmost - bottommost);
153:
154: // draw dots and names
155: Iterator<Map.Entry<String, coordinate>> i = points.entrySet()
156: .iterator();
157: Map.Entry<String, coordinate> entry;
158: String name;
159: coordinate c;
160: int x, y;
161: while (i.hasNext()) {
162: entry = i.next();
163: name = entry.getKey();
164: c = entry.getValue();
165: x = (xfactor == 0.0) ? width / 2
166: : (int) (leftborder + (c.x - leftmost) * xfactor);
167: y = (yfactor == 0.0) ? height / 2 : (int) (height
168: - bottomborder - (c.y - bottommost) * yfactor);
169: image.setColor(color_dot);
170: image.dot(x, y, 6, true);
171: image.setColor(color_text);
172: ymageToolPrint.print(image, x, y + 10, 0, name
173: .toUpperCase(), 0);
174: }
175:
176: // draw lines
177: Iterator<String> j = borders.iterator();
178: coordinate[] border;
179: image.setColor(color_line);
180: int x0, x1, y0, y1;
181: while (j.hasNext()) {
182: border = getBorder(j.next());
183: if (border == null)
184: continue;
185: if (xfactor == 0.0) {
186: x0 = width / 2;
187: x1 = width / 2;
188: } else {
189: x0 = (int) (leftborder + (border[0].x - leftmost)
190: * xfactor);
191: x1 = (int) (leftborder + (border[1].x - leftmost)
192: * xfactor);
193: }
194: if (yfactor == 0.0) {
195: y0 = height / 2;
196: y1 = height / 2;
197: } else {
198: y0 = (int) (height - bottomborder - (border[0].y - bottommost)
199: * yfactor);
200: y1 = (int) (height - bottomborder - (border[1].y - bottommost)
201: * yfactor);
202: }
203: // draw the line, with the dot at the beginning of the line
204: image.lineDot(x1, y1, x0, y0, 3, 4, color_line,
205: color_lineend);
206: }
207: return image;
208: }
209:
210: }
|