summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/tkz/tkz-elements/latex/tkz_elements_functions_circles.lua
blob: cde3c027ac0acd714b39f96212dfc65c539ed720 (plain)
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
-- tkz_elements_functions_circles.lua
-- date 23/12/24
-- version 1.72c
-- Copyright 2023  Alain Matthes
-- This work may be distributed and/or modified under the
-- conditions of the LaTeX Project Public License, either version 1.3
-- of this license or (at your option) any later version.
-- The latest version of this license is in
--   http://www.latex-project.org/lppl.txt
-- and version 1.3 or later is part of all distributions of LaTeX
-- version 2005/12/01 or later.
-- This work has the LPPL maintenance status “maintained”.
-- The Current Maintainer of this work is Alain Matthes.

--  define a circle by the center and a radius

-- function circle_cr ( c, r )
--     return c +  point(r,0)
-- end

function midarc_ (o,a,b) -- a -> b
   local phi = 0.5 * get_angle_ ( a,o,b )
   return rotation_ (o,phi,b)
end

function tangent_from_ (c,p,pt)
  local o
   o  = midpoint_ ( c,pt )
 return  intersection_cc_ (o,c,c,p)
end

function tangent_at_ (a,p)
   return rotation_ (p,math.pi/2,a),rotation_ (p,-math.pi/2,a)
end
 
function orthogonal_from_ (a,b,p)
    return tangent_from_ (a,b,p)
end

function orthogonal_through_ (a,b,x,y)
    local d,z
   d =  1/point.mod(x-a)
   z = a +(b-a)*d
   return circum_center_ (x,y,z)
end

 
function inversion_ (c,p,pt)
  local ry = point.abs(c-p)
  local d  = point.abs(c-pt)
  local r  = (ry*ry)/d
  return c+polar_ (r,point.arg(pt-c))
end
  
function radical_axis_ (c1,p1,c2,p2)
   local d,m,m1,m2,epsilon,ci,cj,ck,cm,cn,r1,r2,t
   r1 = point.abs(c1-p1)
   r2 = point.abs(c2-p2)
   epsilon = 10^(-12)
   d = point.abs (c2 - c1)
   m = d - (r1+r2)
   m1 = math.abs (d - (r1+r2))
   m2 = math.abs (d - math.abs(r1-(r2)))
   if m2 < epsilon then
      ci = rotation_ (p1,math.pi/2,c2)
      cj = rotation_ (p1,-math.pi/2,c2)
      return ci,cj
   elseif m1 < epsilon then -- circles tangent
      -- z.A = point : new ( x , y ) contact
      t,t = intersection_cc_ (c1,p1,c2,p2)
   ci = rotation_ (t,math.pi/2,c2)
   cj = rotation_ (t,-math.pi/2,c2)
   return ci,cj
    -- circles disjoints
     elseif   m > 0 then
         ck = rotation_ (c1,math.pi/3,c2)
         ci,cj = intersection_cc_ (c1,p1,ck,c1)
         cm,cn = intersection_cc_ (c2,p2,ck,c1)
         ci = intersection_ll_ (ci,cj,cm,cn)
         cj = symmetry_axial_ (c1,c2,ci) -- projection_ (c1,c2,ci)
         return ci,cj
     else         
     ci,cj = intersection_cc_ (c1,p1,c2,p2)
     return ci,cj
   end
end

function south_pole_ (c,p)
   local r
   r = point.abs (c-p)
   return c - point (0,r)
 end

function north_pole_ (c,p)
   local r
   r = point.abs (c-p)
   return c + point (0,r)
end

function antipode_ (c,pt)
   return 2 * c - pt
end

function internal_similitude_ (c1,r1,c2,r2)
  return barycenter_ ({c2,r1},{c1,r2})
end

function  external_similitude_ (c1,r1,c2,r2)
  return  barycenter_ ({c2,r1},{c1,-r2})
end

function circlepoint_ (c,t,k)
   local phi = 2*k* math.pi
   return rotation_ (c,phi,t) 
end
 
-- version 1.60 new
function midcircle_(C1,C2)
   local state,r,s,t1,t2,T1,T2,p,a,b,c,d,Cx,Cy,i,j
   state = circles_position(C1,C2)
   i = barycenter_ ({C2.center,C1.radius},{C1.center,-C2.radius})
   j = barycenter_ ({C2.center,C1.radius},{C1.center,C2.radius})
   t1,t2 = tangent_from_ (C1.center,C1.through,i)
   T1,T2 = tangent_from_ (C2.center,C2.through,i)
      
   if (state == 'outside') or (state == 'outside tangent')then
         p = math.sqrt(point.mod(i-t1)*point.mod(i-T1))
   return circle  : radius (i,p)
   elseif state == 'intersect' then
        r,s = intersection (C1,C2)
   return circle  : radius (i,point.mod(r-i)) , circle  : radius (j,point.mod(r-j))
   elseif (state == 'inside') or (state == 'inside tangent') then
      a,b = intersection_lc_ (C1.center,C2.center,C1.center,C1.through)
      c,d = intersection_lc_ (C1.center,C2.center,C2.center,C2.through)
       
      if C1.radius < C2.radius then 
         z.u, z.v, z.r, z.s = a, b, c, d
      else
         z.u, z.v, z.r, z.s = c, d, a, b
      end
      
      if  (in_segment_ (z.s,z.v,z.u) == true) 
      then 
      Cx = circle  :  diameter (z.r,z.v)
      Cy = circle  :  diameter (z.u,z.s)
      else
      Cx = circle  :  diameter (z.s,z.v)
      Cy = circle  :  diameter (z.u,z.r)
      end
      if (Cx.radius) < (Cy.radius) then
         return Cy : orthogonal_from (j) 
      else
         return Cx : orthogonal_from (j)
 end
 end
end