001 package com.croftsoft.core.math.geom;
002
003 import java.awt.Point;
004 import java.awt.geom.Point2D;
005 import java.io.*;
006
007 import com.croftsoft.core.io.SerializableLib;
008 import com.croftsoft.core.lang.Testable;
009
010 /*********************************************************************
011 * A Point2D.Double extension implementing accessor interface PointXY.
012 *
013 * @version
014 * 2003-04-13
015 *
016 * @since
017 * 2003-03-20
018 *
019 * @author
020 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
021 *********************************************************************/
022
023 public class Point2DD
024 extends Point2D.Double
025 implements PointXY, Serializable, Testable
026 //////////////////////////////////////////////////////////////////////
027 //////////////////////////////////////////////////////////////////////
028 {
029
030 private static final long serialVersionUID = 0L;
031
032 //////////////////////////////////////////////////////////////////////
033 // static unit test methods
034 //////////////////////////////////////////////////////////////////////
035
036 public static void main ( String [ ] args )
037 //////////////////////////////////////////////////////////////////////
038 {
039 System.out.println ( test ( args ) );
040 }
041
042 public static boolean test ( String [ ] args )
043 //////////////////////////////////////////////////////////////////////
044 {
045 try
046 {
047 Point2DD point2DD1 = new Point2DD ( 0, 1 );
048
049 byte [ ] bytes = SerializableLib.compress ( point2DD1 );
050
051 Point2DD point2DD2 = ( Point2DD ) SerializableLib.load (
052 new ByteArrayInputStream ( bytes ) );
053
054 System.out.println ( point2DD2 );
055
056 return point2DD2.equals ( point2DD1 );
057 }
058 catch ( Exception ex )
059 {
060 ex.printStackTrace ( );
061
062 return false;
063 }
064 }
065
066 //////////////////////////////////////////////////////////////////////
067 // constructor methods
068 //////////////////////////////////////////////////////////////////////
069
070 public Point2DD (
071 double x,
072 double y )
073 //////////////////////////////////////////////////////////////////////
074 {
075 this.x = x;
076
077 this.y = y;
078 }
079
080 public Point2DD ( PointXY pointXY )
081 //////////////////////////////////////////////////////////////////////
082 {
083 x = pointXY.getX ( );
084
085 y = pointXY.getY ( );
086 }
087
088 public Point2DD ( Point2D point2D )
089 //////////////////////////////////////////////////////////////////////
090 {
091 x = point2D.getX ( );
092
093 y = point2D.getY ( );
094 }
095
096 public Point2DD ( )
097 //////////////////////////////////////////////////////////////////////
098 {
099 }
100
101 //////////////////////////////////////////////////////////////////////
102 // mutator methods
103 //////////////////////////////////////////////////////////////////////
104
105 public void setX ( double x ) { this.x = x; }
106
107 public void setY ( double y ) { this.y = y; }
108
109 public void setXY ( PointXY pointXY )
110 //////////////////////////////////////////////////////////////////////
111 {
112 x = pointXY.getX ( );
113
114 y = pointXY.getY ( );
115 }
116
117 public void setXY (
118 double x,
119 double y )
120 //////////////////////////////////////////////////////////////////////
121 {
122 this.x = x;
123
124 this.y = y;
125 }
126
127 public void setXY ( Point point )
128 //////////////////////////////////////////////////////////////////////
129 {
130 x = point.x;
131
132 y = point.y;
133 }
134
135 //////////////////////////////////////////////////////////////////////
136 // math methods
137 //////////////////////////////////////////////////////////////////////
138
139 /*********************************************************************
140 * The angle, in radians, from this point to the other point.
141 * Note that the direction of 0 radians is along the positive x-axis
142 * and PI/2 radians is along the positive y-axis.
143 *********************************************************************/
144 public double angleTo ( PointXY otherPointXY )
145 //////////////////////////////////////////////////////////////////////
146 {
147 return Math.atan2 (
148 otherPointXY.getY ( ) - y,
149 otherPointXY.getX ( ) - x );
150 }
151
152 public double distanceXY ( PointXY otherPointXY )
153 //////////////////////////////////////////////////////////////////////
154 {
155 return distance ( otherPointXY.getX ( ), otherPointXY.getY ( ) );
156 }
157
158 //////////////////////////////////////////////////////////////////////
159 // overridden Object methods
160 //////////////////////////////////////////////////////////////////////
161
162 public String toString ( )
163 //////////////////////////////////////////////////////////////////////
164 {
165 return "(" + x + "," + y + ")";
166 }
167
168 //////////////////////////////////////////////////////////////////////
169 // private serialization methods
170 //////////////////////////////////////////////////////////////////////
171
172 private void writeObject ( ObjectOutputStream objectOutputStream )
173 throws IOException
174 //////////////////////////////////////////////////////////////////////
175 {
176 objectOutputStream.defaultWriteObject ( );
177
178 objectOutputStream.writeDouble ( x );
179
180 objectOutputStream.writeDouble ( y );
181 }
182
183 private void readObject ( ObjectInputStream objectInputStream )
184 throws IOException, ClassNotFoundException
185 //////////////////////////////////////////////////////////////////////
186 {
187 objectInputStream.defaultReadObject ( );
188
189 x = objectInputStream.readDouble ( );
190
191 y = objectInputStream.readDouble ( );
192 }
193
194 //////////////////////////////////////////////////////////////////////
195 //////////////////////////////////////////////////////////////////////
196 }