comparison flys-client/src/main/java/org/dive4elements/river/client/shared/Transform2D.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/shared/Transform2D.java@ef745bc6bed9
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.shared;
2
3 import java.io.Serializable;
4 import java.util.Date;
5
6 import com.google.gwt.core.client.GWT;
7 import com.google.gwt.i18n.client.DateTimeFormat;
8 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
9 import com.google.gwt.i18n.client.NumberFormat;
10
11
12 /**
13 * This object supports a linear transformation to transform xy coordinates into
14 * an other coordinate system based on scale and translation values.
15 *
16 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
17 */
18 public class Transform2D implements Serializable {
19
20 protected String xType;
21 protected String yType;
22
23 protected double sx;
24 protected double sy;
25
26 protected double tx;
27 protected double ty;
28
29
30 public Transform2D() {
31 }
32
33
34 /**
35 * Creates a new transformation with scale and translation factors.
36 *
37 * @param sx The scale factor for the x axis.
38 * @param sy The scale factor for the y axis.
39 * @param tx The translation factor for the x axis.
40 * @param ty The translation factor for the y axis.
41 */
42 public Transform2D(double sx, double sy, double tx, double ty) {
43 this(sx, sy, tx, ty, "number", "number");
44 }
45
46
47 public Transform2D(
48 double sx, double sy,
49 double tx, double ty,
50 String xType,
51 String yType
52 ) {
53 this.xType = xType;
54 this.yType = yType;
55
56 this.sx = sx;
57 this.sy = sy;
58 this.tx = tx;
59 this.ty = ty;
60 }
61
62
63 /**
64 * Transforms the pixel x and y into a new coordinate system based on the
65 * scale and translation values specified in the constructor.
66 */
67 public double[] transform(double x, double y) {
68 double resX = sx * x + tx;
69 double resY = sy * y + ty;
70
71 return new double[] { resX, resY };
72 }
73
74
75 public String[] format(Number[] xy) {
76 String x = null;
77 String y = null;
78
79 if (xType.equals("date")) {
80 x = formatDate(xy[0].longValue());
81 }
82 else {
83 x = formatNumber(xy[0].doubleValue());
84 }
85
86 if (yType.equals("date")) {
87 y = formatDate(xy[1].longValue());
88 }
89 else {
90 y = formatNumber(xy[1].doubleValue());
91 }
92
93 return new String[] { x, y };
94 }
95
96
97 protected String formatDate(long time) {
98 Date date = new Date(time);
99 DateTimeFormat df = getDateTimeFormat();
100
101 return df.format(date);
102 }
103
104
105 protected String formatNumber(double number) {
106 NumberFormat nf = getNumberFormat();
107
108 return nf.format(number);
109 }
110
111
112 public DateTimeFormat getDateTimeFormat() {
113 return DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT);
114 }
115
116
117 public NumberFormat getNumberFormat() {
118 return NumberFormat.getDecimalFormat();
119 }
120
121
122 public void dumpGWT() {
123 GWT.log("SX = " + sx);
124 GWT.log("SY = " + sy);
125 GWT.log("TX = " + tx);
126 GWT.log("TY = " + ty);
127 }
128 }
129 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org