ImageCoords.java
5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* ESO Archive
*
* $Id: ImageCoords.java,v 1.2 2009/02/20 23:10:11 abrighto Exp $
*
* who when what
* -------------- ---------- ----------------------------------------
* Allan Brighton 1999/05/19 Created, based on C++ version
*/
package jsky.coords;
import java.io.Serializable;
/**
* Class representing world coordinates (right-ascension, ylination,
* stored as J2000 internally).
*/
public class ImageCoords implements Coordinates, Serializable {
/** The X coordinate */
protected double x;
/** The Y coordinate */
protected double y;
/** Default constructor: initialize null coordinates. */
public ImageCoords() {
}
/**
* Initialize from x and y.
*
* @param x The X coordinate
* @param y The Y coordinate
*/
public ImageCoords(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Initialize from x and y.
*
* @param x The X coordinate
* @param y The Y coordinate
*/
public ImageCoords(Double x, Double y) {
this.x = x;
this.y = y;
}
/*
* Parse X and Y in string format.
*
* @param x The X coordinate in string form
*
* @param y The Y coordinate in string form
*/
public ImageCoords(String x, String y) {
this.x = Double.parseDouble(x);
this.y = Double.parseDouble(y);
}
/** return the X value */
public double getX() {
return x;
}
/** return the Y value */
public double getY() {
return y;
}
/**
* Return the coordinates as a string in h:m:s [+-]d:m:s format
*/
public String toString() {
return Double.toString(x) + " " + Double.toString(y);
}
/**
* Return the distance between the two points.
* @param x0 The X coordinate of the first point
* @param y0 The Y coordinate of the first point
* @param x1 The X coordinate of the second point
* @param y1 The Y coordinate of the second point
* @return The distance between the two points.
*/
public static double dist(double x0, double y0, double x1, double y1) {
double x = Math.abs(x1 - x0);
double y = Math.abs(y1 - y0);
return Math.sqrt(x * x + y * y);
}
/**
* Return the distance between this position and the given one.
* @param pos The other point.
* @return The distance to the given point.
*/
public double dist(ImageCoords pos) {
return dist(x, y, pos.x, pos.y);
}
/**
* Return the distance between this position and the given one.
* @param pos The other point.
* @return The distance to the given point.
*/
public double dist(Coordinates pos) {
return dist((ImageCoords) pos);
}
/**
* Given a radius, return an array {pos1, pos2} with the 2 endpoints
* that form a box with center at "this" position.
*
* @param radius The radius.
*
* @return Array of 2 ImageCoords objects that are the endpoints of a box
* with the given radius and centered at "this" position.
*/
public ImageCoords[] box(double radius) {
ImageCoords[] ar = new ImageCoords[2]; // return array
double w = Math.sqrt((radius * radius) / 2.);
double x0 = x - w;
double y0 = y - w;
double x1 = x + w;
double y1 = y + w;
// set the result array
ar[0] = new ImageCoords(x0, y0);
ar[1] = new ImageCoords(x1, y1);
return ar;
}
/**
* Given the endpoints of a box (pos1, pos2), return an array containing the
* center pos, as well as the width, height, and radius of the box.
*
* @param pos1 The first endpoint of the box.
* @param pos2 The second endpoint of the box.
*
* @return An array of 5 doubles: {x, y, width, height, radius}
* where (x, y) gives the center position,
* width and height are the size of the box,
* and radius is the distance from the center to a corner.
*/
public double[] center(ImageCoords pos1, ImageCoords pos2) {
double ar[] = new double[5]; // result
// get center pos
double x1 = pos1.x, y1 = pos1.y;
double x2 = pos2.x, y2 = pos2.y;
ar[0] = (x1 + x2) / 2.0;
ar[1] = (y1 + y2) / 2.0;
// get width and height of box
ar[2] = dist(x1, y1, x2, y1);
ar[3] = dist(x1, y1, x1, y2);
// radius is half the distance from pos1 to pos2
ar[4] = dist(x1, y1, x2, y2) / 2.;
return ar;
}
/** Returns the name of the coordinate system as a string. */
public String getCoordinateSystemName() {
return "image";
}
/**
* Test cases
*/
public static void main(String[] args) {
ImageCoords c1 = new ImageCoords(100., 200.);
ImageCoords c2 = new ImageCoords("100.", "200.");
System.out.println("these coords should each be the same:");
System.out.println(c1);
System.out.println(c2);
// test the "box" method (get 2 points given a radius)
ImageCoords c3 = new ImageCoords(100, 100), c4, c5;
ImageCoords[] ar1 = c3.box(10);
c4 = ar1[0];
c5 = ar1[1];
System.out.println("box of radius 10 with center at (100, 100):");
System.out.println(c4);
System.out.println(c5);
}
}