-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry.rb
More file actions
1146 lines (943 loc) · 23.6 KB
/
Copy pathgeometry.rb
File metadata and controls
1146 lines (943 loc) · 23.6 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'forwardable'
EPS = 1e-10
COUNTER_CLOCKWISE = 1
CLOCKWISE = -1
ONLINE_BACK = 2
ONLINE_FRONT = -2
ON_SEGMENT = 0
POINTS_POSITION = { COUNTER_CLOCKWISE: 1, CLOCKWISE: -1, ONLINE_BACK: 2, ONLINE_FRONT: -2, ON_SEGMENT: 0 }.invert
Math::HALF_PI = Math::PI / 2
Math::QUATER_PI = Math::PI / 4
Math::TAU = Math::PI * 2
Math::TWO_PI = Math::PI * 2
# Numeric
class Numeric
def equals(other)
(other - self).abs < EPS
end
end
# Array
class Array
def to_point
Point.new(*self)
end
def to_points
each_slice(2).map{ |x, y| Point.new(x, y) }
end
def to_circle
cx, cy, r = self
Circle.new(Point.new(cx, cy), r)
end
end
# String
class String
def to_point
x, y = split.map{ |t| t.to_f }
Point.new(x, y)
end
def to_circle
cx, cy, r = split.map{ |t| t.to_f }
Circle.new(Point.new(cx, cy), r)
end
def to_line
sx, sy, tx, ty = split.map{ |t| t.to_f }
Line.new(Point(sx, sy), Point.new(tx, ty))
end
end
# Point
class Point
include Math
include Comparable
def initialize(x = 0, y = 0)
@x = x
@y = y
end
attr_accessor :x, :y
class << self
# def gets
# str = Kernel.gets or return nil
# x, y = str.split.map{ |t| t.to_f }
# Point.new(x, y)
# end
def getf
str = Kernel.gets or return nil
x, y = str.split.map{ |t| t.to_f }
Point.new(x, y)
end
def geti
str = Kernel.gets or return nil
x, y = str.split.map{ |t| t.to_i }
Point.new(x, y)
end
def origin
Point.new(0.0, 0.0)
end
def polar(r, θ)
Point.new(Math.cos(θ) * r, Math.sin(θ) * r)
end
end
def +(other)
Point.new(@x + other.x, @y + other.y)
end
def -(other)
Point.new(@x - other.x, @y - other.y)
end
def +@
Point.new(@x, @y)
end
def -@
Point.new(-@x, -@y)
end
def *(k)
Point.new(@x * k, @y * k)
end
def /(k)
Point.new(@x / k, @y / k)
end
def fdiv(k)
Point.new(@x.fdiv(k), @y.fdiv(k))
end
def quo(k)
Point.new(@x.quo(k), @y.quo(k))
end
def abs2
@x * @x + @y * @y
end
alias sum_of_squares abs2
# alias norm abs2
alias ss abs2
def abs
Math.sqrt(@x * @x + @y * @y)
end
# alias length abs
def dist(other = nil)
other.nil? ? hypot(@x, @y) : hypot(@x - other.x, @y - other.y)
end
def distance_to(other)
case other
when Point then distance_to_point(other)
when Segment then distance_to_segment(other)
when Line then distance_to_line(other)
when Circle then distance_to_circle(other)
else raise ArgumentError end
end
# alias getDistance distance_to
def distance_to_point(other)
(other - self).abs
end
def distance_to_line(l)
b = l.t - l.s
b.cross(self - l.s).abs / b.abs
end
def distance_to_segment(s)
(s.t - s.s).dot(v = (self - s.s)) < 0.0 and return v.abs
(s.s - s.t).dot(v = (self - s.t)) < 0.0 and return v.abs
distance_to_line(s)
end
def distance_to_circle(circle)
diff = (circle.center - self).abs - circle.radius
# diff < 0 ? 0 : diff
end
def <=>(other)
@x != other.x ? @x <=> other.x : @y <=> other.y
end
def ==(other)
(@x - other.x).abs < EPS && (@y - other.y).abs < EPS
end
alias eql? ==
def hash
end
def dot(other)
@x * other.x + @y * other.y
end
def cross(other)
@x * other.y - @y * other.x
end
def orthogonal?(other)
dot(other).abs < EPS
end
def parallel?(other)
cross(other).abs < EPS
end
alias parallel_to? parallel?
def project(l)
base = l.t - l.s
r = (self - l.s).dot(base) / base.abs2
l.s + base * r
end
def reflect(l)
self + (project(l) - self) * 2
end
def ccw(v1, v2)
a = v1 - self
b = v2 - self
return COUNTER_CLOCKWISE if a.cross(b) > EPS
return CLOCKWISE if a.cross(b) < -EPS
return ONLINE_BACK if a.dot(b) < -EPS
return ONLINE_FRONT if a.abs2 < b.abs2
return ON_SEGMENT
end
# 三点が作る三角形の面積
def area(v1, v2 = nil)
v2 = Point.new(0.0, 0.0) if v2.nil?
a = self - v2
b = v1 - v2
(a.x * b.y - a.y * b.x) / 2.0
end
def arg
Math.atan2(@y, @x)
end
def rot90
Point.new(-@y, @x)
end
def rot90!
@x, @y = -@y, @x
end
def rot(θ)
Point.new(@x * cos(θ) - @y * sin(θ), @x * sin(θ) + @y * cos(θ))
end
alias rotate rot
def rot!(θ)
@x, @y = @x * cos(θ) - @y * sin(θ), @x * sin(θ) + @y * cos(θ)
end
alias rotate! rot!
def polar
Complex(@x, @y).polar
end
def flip!
@x, @y = @y, @x
end
alias swap! flip!
alias reverse! flip!
def flip
Point.new(@y, @x)
end
alias swap flip
alias reverse flip
def to_line
Line.new(Point.origin, self)
end
alias to_segment to_line
def origin?
@x.abs < EPS && @y.abs < EPS
end
alias zero? origin?
# def size
# 2
# end
def to_a
[@x, @y]
end
alias to_ary to_a
# def to_c
# Complex(@x, @y)
# end
def [](i)
case i
when 0 then @x
when 1, -1 then @y
else
raise ArgumentError
end
end
def to_s
# "#{@x.round(12)} #{@y.round(12)}" % [@x, @y]
format("%f %f", @x, @y)
end
# def to_s; "#{x} #{y}" end
def inspect
format("(%f %f)", @x, @y)
end
def out
puts "#{@x} #{@y}"
end
# deprecate
end
# Line
class Line
def initialize(s, t)
@s = s
@t = t
end
attr_accessor :s, :t
class << self
# def gets
# s = Kernel.gets or return nil
# sx, sy, tx, ty = s.to_s.split.map{ |e| e.to_f }
# Line.new(Point.new(sx, sy), Point.new(tx, ty))
# end
def getf
s = Kernel.gets or return nil
sx, sy, tx, ty = s.to_s.split.map{ |e| e.to_f }
Line.new(Point.new(sx, sy), Point.new(tx, ty))
end
def geti
s = Kernel.gets or return nil
sx, sy, tx, ty = s.to_s.split.map{ |e| e.to_i }
Line.new(Point.new(sx, sy), Point.new(tx, ty))
end
end
def orthogonal?(l)
(@s - @t).dot(l.s - l.t).abs < EPS
end
def parallel?(l)
(@s - @t).cross(l.s - l.t).abs < EPS
end
alias parallel_to? parallel?
def project(v)
base = @t - @s
r = 1.0 * (v - @s).dot(base) / base.abs2
@s + base * r
end
def reflect(v)
v + (project(v) - v) * 2.0
end
def ccw(v)
a = @t - @s
b = v - @s
return COUNTER_CLOCKWISE if a.cross(b) > EPS
return CLOCKWISE if a.cross(b) < -EPS
return ONLINE_BACK if a.dot(b) < -EPS
return ONLINE_FRONT if a.abs2 < b.abs2
return ON_SEGMENT
end
def distance_to(other)
case other
when Point then distance_to_point(other)
when Segment then distance_to_segment(other)
when Line then distance_to_line(other)
when Circle then distance_to_circle(other)
end
end
alias dist distance_to
alias distance distance_to
def distance_to_point(v)
((@t - @s).cross(v - @s) / (@t - @s).abs).abs
end
alias getDistanceLP distance_to_point
def getDistanceSP(v)
return (v - @s).abs if (@t - @s).dot(v - @s) < 0.0
return (v - @t).abs if (@s - @t).dot(v - @t) < 0.0
getDistanceLP(v)
end
def intersect?(l)
ccw(l.s) * ccw(l.t) <= 0 && l.ccw(@s) * l.ccw(@t) <= 0
end
def intersect_to_circle?(c)
distance_to_circle(c) < EPS
end
def distance_to_line(l)
return 0.0 if intersect?(l)
[
getDistanceSP(l.s),
getDistanceSP(l.t),
l.getDistanceSP(@s),
l.getDistanceSP(@t),
].min
end
# alias getDistance distance_to_line
def distance_to_circle(c)
pl = c.center.project(self)
[(pl - c.c).abs - c.r, 0.0].min
end
def cross_point(l)
base = @t - @s
d1 = base.cross(l.t - l.s).abs
d2 = base.cross(@t - l.s).abs
return l.s if d1.abs < EPS && d2.abs < EPS # same line!!
return warn "!!!PRECONDITION NOT SATISFIED!!!" if d1.abs < EPS
l.s + (l.t - l.s) * 1.0 * d2 / d1
end
# alias getCrossPoint cross_point
def ==(other)
@s == other.s && @t == other.t
end
alias eql? ==
def length
Float::INFINITY
end
def slope
dy = @t.y - @s.y
dx = @t.x - @s.x
dy / dx
end
def reverse
self.class.new(@t, @s)
end
def reverse!
@s, @t = @t, @s
end
def to_point
Point.new(@t.x - @s.x, @t.y - @s.y)
end
def to_s
"#{@s} #{@t}"
end
def inspect
"[(#{@s.x}, #{@s.y}) -> (#{@t.x}, #{@t.y})]"
end
end
# Segment
class Segment < Line
def intersect?(other)
ccw(other.s) * ccw(other.t) <= 0 && l.ccw(@s) * l.ccw(@t) <= 0
end
def distance_to(other)
case other
when Point then distance_to_point(other)
when Segment then distance_to_segment(other)
when Line then distance_to_line(other)
when Circle then distance_to_circle(other)
end
end
# alias getDistance distance_to
def distance_to_point(v)
return (v - @s).abs if (@t - @s).dot(v - @s) < 0.0
return (v - @t).abs if (@s - @t).dot(v - @t) < 0.0
super
end
# alias getDistanceSP distance_to_point
def distance_to_segment(other)
return 0.0 if intersect?(other)
[
distance_to_point(other.s),
distance_to_point(other.t),
other.distance_to_point(@s),
other.distance_to_point(@t),
].min
end
def distance_to_line(l)
intersect?(l) ? 0.0 : [l.distance_to_point(@s), l.distance_to_point(@t)].min
end
def distance_to_circle(c)
pc = c.center.project(self)
t = [@s.dist(c.c), @t.dist(c.c)]
t << @pc.dist(c.c) if ccw(pc).odd?
[t.min - c.r, 0.0].max
end
def length
(@t - @s).abs
end
end
# Polygon
class Polygon
def initialize(arg = nil)
if block_given?
@points = []
arg.times{ @points << yield }
else
@points = arg || []
end
end
attr_accessor :points
extend Forwardable
def_delegators(:@points, :push, :pop, :append, :prepend, :shift, :unshift, :size, :sort, :sort!, :sort_by, :sort_by!, :to_a, :uniq, :uniq!, :max_by, :max_by, :max, :min, :map)
alias add_point push
def area
o = @points[-1]
(0...@points.size - 1).sum{ |i| o.area(@points[i - 1], @points[i]) }.abs
end
def length
(@points + @points[0]).each_cons(2).sum{ |x, y| (x - y).abs }
end
def convex?
n = @points.size
if n == 3
x = @points[0]
y = @points[1]
z = @points[2]
return x.ccw(y, z).abs == 1
end
n.times do |i|
x = @points[i]
y = @points[(i + 1) % n]
z = @points[(i + 2) % n]
return false if x.ccw(y, z) == CLOCKWISE || x.ccw(y, z) == ONLINE_BACK
end
true
end
def include?(v)
# { ON_EDGE: 1, IN_POLYGON: 2, OUT_OF_POLYGON:0 }
n = @points.size
f = false
n.times do |i|
a = @points[i] - v
b = @points[(i + 1) % n] - v
return 1 if a.cross(b).abs < EPS && a.dot(b) < EPS
a, b = b, a if a.y > b.y
f = !f if a.y < EPS && EPS < b.y && a.cross(b) > EPS
end
f ? 2 : 0
end
alias includes? include?
alias contain? include?
alias contain include?
def ==(other)
@points.sort == other.points.sort
end
# deprecate because this is slow
def closest_pair
# http://www.prefield.com/algorithm/geometry/closest_pair.html
# pair<P,P> closestPair(vector<P> p) {
# int n = p.size(), s = 0, t = 1, m = 2, S[n]; S[0] = 0, S[1] = 1;
# sort(ALL(p)); // "p < q" <=> "p.x < q.x"
# double d = norm(p[s]-p[t]);
# for (int i = 2; i < n; S[m++] = i++) REP(j, m) {
# if (norm(p[S[j]]-p[i])<d) d = norm(p[s = S[j]]-p[t = i]);
# if (real(p[S[j]]) < real(p[i]) - d) S[j--] = S[--m];
# }
# return make_pair( p[s], p[t] );
# }
n = @points.size
s = 0
t = 1
m = 2
ss = Array.new(n)
ss[0] = 0
ss[1] = 1
@points.sort!
d = (@points[s] - @points[t]).abs2
i = 2
while i < n
m.times do |j|
if d > (@points[ss[j]] - @points[i]).abs2
d = (@points[s = ss[j]] - @points[t = i]).abs2
end
if @points[ss[j]].x < @points[i].x - d
ss[j] = ss[m -= 1]
j -= 1
end
end
ss[m] = i
m += 1
i += 1
end
[@points[s], @points[t]]
end
def closest_pair(n = @points.size)
return Float::INFINITY if n <= 1
m = n / 2
x = a[m].x
d = [closest_pair(a, m), closest_pair(a + m, n - m)]
a.sort!
n.times do |i|
end
end
# private def implace_merge
# end
def andrew_scan
g = Polygon.new
g.points = @points.map(&:dup)
g.andrew_scan!
end
def andrew_scan!
u = Polygon.new
l = Polygon.new
return self if size < 3
@points.sort!{ |a, b| (a.y <=> b.y).nonzero? || a.x <=> b.x }
u.push(points[0], points[1])
l.push(points[-1], points[-2])
(2...size).each do |i|
(u.size).downto(2) do |n|
break if u.points[n - 2].ccw(u.points[n - 1], points[i]) != COUNTER_CLOCKWISE
u.pop
end
u.push(points[i])
end
(size - 3).downto(0) do |i|
(l.size).downto(2) do |n|
break if l.points[n - 2].ccw(l.points[n - 1], points[i]) != COUNTER_CLOCKWISE
l.pop
end
l.push(points[i])
end
l.points.reverse!
(u.size - 2).downto(1){ |i| l.push(u.points[i]) }
l
end
alias andrewScan andrew_scan
def diameter
n = size
is = js = 0
n.times do |i|
is = i if points[i].y > points[is].y
js = i if points[i].y < points[js].y
end
dmax = (points[is] - points[js]).dist
i = is
j = js
while true
if (points[(i + 1) % n] - points[i]).cross(points[(j + 1) % n] - points[j]) >= 0
j = (j + 1) % n
else
i = (i + 1) % n
end
d = (points[i] - points[j]).dist
dmax = d if dmax < d
break if i == is && j == js
end
dmax
end
def cut(ln)
n = size
q = Polygon.new
n.times do |i|
cur = points[i]
nex = points[(i + 1) % n]
q.push(cur) if ln.ccw(cur) != CLOCKWISE
q.push(ln.cross_point(line(cur, nex))) if ln.ccw(cur) * ln.ccw(nex) < 0
end
q
end
def center_of_gravity
# @points.sum(Point.new(0, 0)).fdiv(@points.size)
@points.sum(Point.new(0, 0)).quo(@points.size)
end
def rhombus?
size == 4 && @points.map{ |e| e.abs }.uniq.size == 1
end
def parallelogram?
size == 4 && @points[0].parallel?(@points[2]) && @points[1].parallel?(@points[3])
end
def square?
rhombus? && parallelogram?
end
def out
puts size
puts points
end
def pout
puts size
puts points.map(&:inspect)
end
end
# Triangle
class Triangle < Polygon
def initialize(points = [])
if block_given?
3.times{ points << yield }
@points = points
else
super
end
end
class << self
# def gets
# str = Kernel.gets or return nil
# a, b, c, d, e, f = str.split.map{ |e| e.to_f }
# Polygon.new([xy(a, b), xy(c, d), xy(e, f)])
# end
def getf
str = Kernel.gets or return nil
a, b, c, d, e, f = str.split.map{ |e| e.to_f }
Polygon.new([xy(a, b), xy(c, d), xy(e, f)])
end
def geti
str = Kernel.gets or return nil
a, b, c, d, e, f = str.split.map{ |e| e.to_i }
Polygon.new([xy(a, b), xy(c, d), xy(e, f)])
end
end
def inradius
@inradius or inscribed_circle and @inradius
end
def inner_center
@inner_center or inscribed_circle and @inner_center
end
def inscribed_circle
a, b, c = @points
x = (b - c).abs
y = (c - a).abs
z = (a - b).abs
@inner_center = (a * x + b * y + c * z) * 1.0 / (x + y + z)
@inradius = area * 2.0 / (x + y + z)
Circle.new(inner_center, inradius)
end
def circumradius
a = (@points[1] - @points[0]).abs
b = (@points[2] - @points[1]).abs
c = (@points[0] - @points[2]).abs
(a * b * c).fdiv(4 * area)
end
def circumcenter
a, b, c = @points
x = (b - c).abs2
y = (c - a).abs2
z = (a - b).abs2
t = x * (y + z - x)
u = y * (z + x - y)
w = z * (x + y - z)
(a * t + b * u + c * w).fdiv(t + u + w)
# r = circumradius
# ca = Circle.new(@points[0], r)
# cb = Circle.new(@points[1], r)
# cc = Circle.new(@points[2], r)
# p0, p1 = ca.cross_points_to_circle(cb)
# q0, q1 = ca.cross_points_to_circle(cc)
# pp [ca.cross_points_to_circle(cb), ca.cross_points_to_circle(cc), cb.cross_points_to_circle(cc)]
# return p0 if p0 == q0 || p0 == q1
# return p1 if p1 == q0 || p1 == q1
# raise "Triangle cannot find circumcenter"
end
def circumscribed_circle
Circle.new(circumcenter, circumradius)
end
end
# Circle
class Circle
def initialize(c = Point.new(0.0, 0.0), r = 0.0)
@c = c
@r = r
end
attr_accessor :c, :r
alias center c
alias radius r
class << self
def gets
str = Kernel.gets or return nil
cx, cy, r = str.to_s.split.map{ |e| e.to_f }
Circle.new(Point.new(cx, cy), r)
end
def getf
str = Kernel.gets or return nil
cx, cy, r = str.to_s.split.map{ |e| e.to_f }
Circle.new(Point.new(cx, cy), r)
end
def geti
str = Kernel.gets or return nil
cx, cy, r = str.to_s.split.map{ |e| e.to_f }
Circle.new(Point.new(cx, cy), r)
end
def unit_ciercle
Circle.new(Point.new(0.0, 0.0), 1.0)
end
end
def diameter
@r * 2
end
def length
@r * Math::TAU
end
alias circumference length
def area
@r * @r * Math::PI
end
include Math
def relation(other)
d = (@c - other.c).dist
r_sum = @r + other.r
return 4 if d > r_sum
return 3 if d == r_sum
r0, r1 = @r, other.r
r0, r1 = other.r, r if other.r < @r
r_diff = r1 - r0
return 2 if d > r_diff # && d < r_sum
warn "完全一致" if r_diff == 0 && d == 0
return 1 if d == r_diff
return 0
end
alias number_of_common_tangents relation
def intersection_to_line(l)
warn "円と線は交わらない(接していない含む)" if @r < @c.distance_to_line(l)
pr = l.project(@c)
e = (l.t - l.s) / (l.t - l.s).abs # unit vector
base = sqrt(@r * @r - (pr - c).abs2)
t = e * base
[pr - t, pr + t] # .sort{ |a, b| (a.x <=> b.x).nonzero? || a.y <=> b.y }
end
alias cross_points_to_line intersection_to_line
def cross_points_count(other)
dc = @c.dist(other.c) # distance between centers
lr, sr = @r, other.r
lr, sr = sr, lr if lr < sr
dr = lr - sr # diff between radiuses
sr = lr + sr # sum of radiuses
if dc < dr
warn "Large circle contains small circle"
0
elsif dc == dr
if dr == 0
warn "Both circles are a perfect match: @c=#{@c.inspect}, @r=#{@r}"
return Float::INFINITY
end
warn "One circle is inscribed inside another circle"
1
elsif dc > sr
warn "Both circles don't intercect. 交点・接点はない"
0
elsif dc == sr
1
else
2
end
end
def intersection_to_circle(other)
# 途中?、螺旋本p.397
d = @c.dist(other.c)
lr, sr = @r, other.r
lr, sr = sr, lr if lr < sr
dr = lr - sr
if d < dr
warn "Large circle contains small circle"
elsif d == dr
if dr == 0
warn "Both circles are a perfect match: @c=#{@c.inspect}, @r=#{@r}"
return dup
end
warn "One circle is inscribed inside another circle"
elsif d > @r + other.r
warn "Both circles don't intercect. 交点・接点はない"
return nil
end
a = Math.acos(Rational(@r * @r + d * d - other.r**2, 2.0 * @r * d))
t = (other.c - @c).arg
[Point.polar(r, t + a) + c, Point.polar(r, t - a) + c] # .sort{ |a, b| (a.x <=> b.x).nonzero? || a.y <=> b.y }
end
alias cross_points_to_circle intersection_to_circle
def cross_points(other)
case other
when Line then intersection_to_line(other)
when Circle then intersection_to_circle(other)
else raise ArgumentError end
end
# alias getCrossPoints cross_points
alias intersectiton cross_points
alias cross_points_to cross_points
def contact_points_with_point(point)
# https://ei1333.github.io/luzhiled/snippets/geometry/template.html
# pair< Point, Point > tangent(const Circle &c1, const Point &p2)
return cross_points_to_circle(Circle.new(point, Math.sqrt((@c - point).abs2 - @r * @r)))
# refer to CGL_7_F code written by drken-san
# http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=3140448#1
d = (@c - point).abs2
l = d - @r * @r
return Point.new(0, 0) if l < -EPS
l = 0.0 if l <= 0.0
cq = (point - @c) * (@r * @r / d)
qs = ((point - @c) * (@r * sqrt(l) / d)).rot90
[@c + cq + qs, c + cq - qs] # .sort # { |a, b| (a.x <=> b.x).nonzero? || a.y <=> b.y }
end