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
|
-- tkz_elements_intersections.lua
-- date 2024/07/16
-- version 3.00
-- Copyright 2024 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.
-------------------------------------------------------------------------
-- intersection of lines
-------------------------------------------------------------------------
function intersection_ll (la,lb)
return intersection_ll_ (la.pa,la.pb,lb.pa,lb.pb)
end
---------------------------------------------------------------------------
-- intersection of a line and a circle
---------------------------------------------------------------------------
function intersection_lc (D,C )
return intersection_lc_ ( D.pa,D.pb ,C.center,C.through )
end -- function
---------------------------------------------------------------------------
-- intersection of two circles
---------------------------------------------------------------------------
function intersection_cc (Ca , Cb )
return intersection_cc_(Ca.center,Ca.through,Cb.center,Cb.through)
end -- function
-- line ellipse
function intersection_le (L,E)
local a,b,c,d,t1,t2,z1,z2,A,B,Bx,By,Ax,Ay,Rx,Ry,sd
A = (L.pa - E.center)*(point(math.cos(E.slope),-math.sin(E.slope)))
B = (L.pb - E.center)*(point(math.cos(E.slope),-math.sin(E.slope)))
Rx = E.Rx
Ry = E.Ry
Ax = A.re
Ay = A.im
Bx = B.re
By = B.im
a = Rx^2 * (By-Ay)^2 +Ry^2 * (Bx-Ax)^2
b = 2 * Rx^2 * Ay * (By-Ay)+ 2 * Ry^2 * Ax * (Bx-Ax)
c = Rx^2 * Ay^2 + Ry^2 * Ax^2 - Rx^2 * Ry^2
d = b^2 - 4 * a * c
if d > 0 then
sd = math.sqrt(d)
t1 = (-(b)+sd)/(2*a)
t2 = (-(b)-sd)/(2*a)
z1 = point ( Ax + (Bx-Ax)*t1 , Ay + (By-Ay)*t1 )
z2 = point ( Ax + (Bx-Ax)*t2 , Ay + (By-Ay)*t2 )
if angle_normalize (point.arg(z1)) < angle_normalize (point.arg(z2))
then
return z1*(point(math.cos(E.slope),math.sin(E.slope))) + E.center,
z2*(point(math.cos(E.slope),math.sin(E.slope))) + E.center
else
return z2*(point(math.cos(E.slope),math.sin(E.slope))) + E.center,
z1*(point(math.cos(E.slope),math.sin(E.slope))) + E.center
end -- if
elseif math.abs(d) < tkz_epsilon
then
t1 = (-(b))/(2*a)
z1 = point ( Ax + (Bx-Ax)*t1 , Ay + (By-Ay)*t1 )
return z1*(point(math.cos(E.slope),math.sin(E.slope))) + E.center,
z1*(point(math.cos(E.slope),math.sin(E.slope))) + E.center
else
return false,false
end
end
function intersection_ll_ (a,b,c,d)
local x1,x2,x3,x4,y1,y2,y3,y4,DN,NX,NY
x1 = a.re
y1 = a.im
x2 = b.re
y2 = b.im
x3 = c.re
y3 = c.im
x4 = d.re
y4 = d.im
DN = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4)
if math.abs ( DN ) < tkz_epsilon then
return false
else
NX = (x1*y2-y1*x2)*(x3-x4) - (x1-x2)*(x3*y4-y3*x4)
NY = (x1*y2-y1*x2)*(y3-y4) - (y1-y2)*(x3*y4-y3*x4)
return point (NX/DN,NY/DN)
end
end
function intersection_lc_ (pa,pb,c,p)
local zh, dh, arg_ab, test, phi,c1,c2,r
r = point.mod (c-p)
zh = projection_ (pa,pb,c)
dh = point.abs (c - zh)
arg_ab = point.arg (pa - pb)
if dh < tkz_epsilon
then
return
c + polar_ (r , math.pi + arg_ab), -- through center
c + polar_ (r , arg_ab)
elseif math.abs (r - dh) < tkz_epsilon
then
return zh , zh -- tangent
elseif dh > r
then return false , false -- no intersection
else
phi = math.asin (dh / r)
-- phi = angle_normalize(phi)
test = (pa-pb) * point.conj (c-zh)
if test.im < 0
then phi = math.pi + phi
end
c1 = angle_normalize (arg_ab + phi )
c2 = angle_normalize (math.pi + arg_ab - phi )
if c2 < c1 then
return
c + polar_ (r, c2) ,
c + polar_ (r, c1)
else
return
c + polar_ (r, c1) ,
c + polar_ (r, c2)
end -- if
end -- if
end -- function
function intersection_cc_ (ca,pa,cb,pb )
local d, cosphi, phi,ra,rb,c1,c2,epsilon
epsilon = 12
d = point.abs (ca - cb)
ra = point.abs (ca - pa)
rb = point.abs (cb - pb)
cosphi = tkzround(((ra * ra + d * d - rb * rb)
/( 2 * ra * d )) , epsilon)
phi = tkzround (math.acos(cosphi),epsilon)
if not phi then
return false , false
elseif phi == 0 then
return ca + polar_ (ra, phi + point.arg (cb - ca)) ,
ca + polar_ (ra, phi + point.arg (cb - ca))
else
c1 = angle_normalize ( phi + point.arg(cb - ca))
c2 = angle_normalize (-phi + point.arg(cb - ca))
if c1 < c2 then
return
ca + polar_(ra, c1),
ca + polar_(ra, c2)
else
return
ca + polar_(ra, c2),
ca + polar_(ra, c1)
end -- if
end -- if
end -- function
function intersection ( X , Y )
local i,z1,z2
local t = {}
if X.type == 'circle'
then
if Y.type == 'circle'
then
z1,z2 = intersection_cc ( X , Y )
table.insert (t , z1 )
table.insert (t , z2 )
else -- Y[i] est une droite
z1,z2 = intersection_lc ( Y , X )
table.insert (t , z1 )
table.insert (t , z2 )
end -- if
else
if X.type == 'line' then
if Y.type == 'circle'
then
z1,z2 = intersection_lc ( X , Y )
table.insert ( t , z1 )
table.insert ( t , z2 )
else
if Y.type == 'line' then
z1 = intersection_ll ( X , Y )
table.insert (t , z1 )
else -- ellipse
z1,z2 = intersection_le ( X , Y )
table.insert ( t , z1 )
table.insert ( t , z2 )
end
end -- if
else
if X.type == 'ellipse' then
z1,z2 = intersection_le ( Y,X)
table.insert ( t , z1 )
table.insert ( t , z2 )
end
end
end -- if
return table.unpack ( t )
end -- function
|