The Java Program: D_angles/equalangles.java
1
2
3 import java.awt.geom.*;
4 import java.awt.*;
5 import java.io.*;
6 import java.util.*;
7
8 public class equalangles
9 {
10 public Scanner sc;
11 public PrintStream ps;
12
13 public static final double epsilon = 0.00000001;
14
15
16
17
18
19
20
21
22
23
24 public double distance( double x1, double y1, double x2, double y2 )
25 {
26 double dx = x2-x1;
27 double dy = y2-y1;
28
29 return Math.sqrt( dx*dx + dy*dy );
30 }
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public double angle( double a, double b, double c )
53 {
54 return Math.acos( ( a*a + b*b - c*c ) / ( 2.0*a*b ) );
55 }
56
57
58
59
60
61
62
63
64
65
66
67
68 public double side( double a, double b, double theta )
69 {
70 return Math.sqrt( a*a + b*b - 2.0*a*b*Math.cos(theta) );
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public double sines( double t1, double a1, double t2 )
91 {
92 return Math.sin( t2 ) * a1 / Math.sin( t1 );
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106 public Point2D.Double anglepoint( double ax, double ay, double bx, double by, double cx, double cy )
107 {
108
109 double ab = distance( ax, ay, bx, by );
110 double bc = distance( bx, by, cx, cy );
111 double ca = distance( cx, cy, ax, ay );
112
113
114 double a = angle( ca, ab, bc );
115 double b = angle( ab, bc, ca );
116 double c = angle( bc, ca, ab );
117
118
119
120
121
122
123
124
125 double lo = 0.0;
126 double hi = Math.min( Math.min( a, b ), c );
127 double theta = 0.0;
128 double pa = 0.0, pb = 0.0, pc = 0.0;
129 while( hi-lo > epsilon )
130 {
131
132 double mid = (hi+lo)/2.0;
133
134
135
136
137
138 pa = sines( Math.PI-b, ab, b-mid );
139
140
141 pb = sines( Math.PI-b, ab, mid );
142
143
144 pc = side( pb, bc, mid );
145
146
147 theta = angle( ca, pc, pa );
148
149
150 if( mid-theta<0.0 )
151 {
152 lo = mid;
153 }
154 else
155 {
156 hi = mid;
157 }
158 }
159
160
161
162
163
164 double a2b = Math.atan2( by-ay, bx-ax );
165
166
167
168
169
170 double alpha = a2b + theta;
171
172
173
174 double testlen = Math.max( ab, ca ) + 10.0;
175 double tx = ax + testlen*Math.cos( alpha );
176 double ty = ay + testlen*Math.sin( alpha );
177
178
179 if( !Line2D.Double.linesIntersect( ax, ay, tx, ty, bx, by, cx, cy ) )
180 {
181
182 alpha = a2b - theta;
183 }
184 double px = ax + pa*Math.cos( alpha );
185 double py = ay + pa*Math.sin( alpha );
186
187 return new Point2D.Double( px, py );
188 }
189
190
191
192
193
194
195 public void doit() throws Exception
196 {
197 sc = new Scanner( new File( "equalangles.judge" ) );
198 ps = new PrintStream( new FileOutputStream( "equalangles.out" ) );
199
200 for(;;)
201 {
202 double ax = sc.nextDouble();
203 double ay = sc.nextDouble();
204 double bx = sc.nextDouble();
205 double by = sc.nextDouble();
206 double cx = sc.nextDouble();
207 double cy = sc.nextDouble();
208
209
210
211 if( distance( ax, ay, bx, by ) < epsilon ) break;
212
213 Point2D.Double p = anglepoint( ax, ay, bx, by, cx, cy );
214 Point2D.Double q = anglepoint( ax, ay, cx, cy, bx, by );
215
216 ps.printf( "%.2f %.2f %.2f %.2f", p.x, p.y, q.x, q.y );
217 ps.println();
218 System.out.printf( "%.2f %.2f %.2f %.2f", p.x, p.y, q.x, q.y );
219 System.out.println();
220 }
221 }
222
223 public boolean eq( int x1, int y1, int x2, int y2 )
224 {
225 return x1==x2 && y1==y2;
226 }
227
228 public void div10() throws Exception
229 {
230 sc = new Scanner( new File( "equalangles.in" ) );
231 ps = new PrintStream( new FileOutputStream( "equalangles.out" ) );
232
233 for(;;)
234 {
235 int ax = sc.nextInt()/10;
236 int ay = sc.nextInt()/10;
237 int bx = sc.nextInt()/10;
238 int by = sc.nextInt()/10;
239 int cx = sc.nextInt()/10;
240 int cy = sc.nextInt()/10;
241
242
243
244 if( eq(ax,ay,bx,by) || eq(ax,ay,cx,cy) || eq(bx,by,cx,cy) )
245 {
246 System.out.println( "ACK! " + ax + " " + ay + " " + bx + " " + by + " " + cx + " " + cy);
247 }
248 else
249 {
250 System.out.println( ax + " " + ay + " " + bx + " " + by + " " + cx + " " + cy );
251 }
252
253 }
254 }
255
256 public static void printcase( Point a, Point b, Point c )
257 {
258 if( !a.equals( b ) && !a.equals( b ) && !b.equals( c ) )
259 {
260 System.out.println( a.x + " " + a.y + " " + b.x + " " + b.y + " " + c.x + " " + c.y );
261 }
262 }
263
264 public static void printallcases( int ax, int ay, int bx, int by, int cx, int cy )
265 {
266 Point a = new Point( ax, ay );
267 Point b = new Point( bx, by );
268 Point c = new Point( cx, cy );
269 printcase( a, b, c );
270 printcase( b, c, a );
271 printcase( c, a, b );
272 printcase( a, c, b );
273 printcase( b, a, c );
274 printcase( c, b, a );
275 }
276
277 public static Random random = new Random();
278
279 public static int r()
280 {
281 return random.nextInt( 2001 ) - 1000;
282 }
283
284
285
286
287
288
289
290 public static void main( String[] args ) throws Exception
291 {
292 new equalangles().doit();
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 }
308
309 }