summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/tkz/tkz-elements/latex/tkz_elements_line.lua
blob: a8d8da22d3a212c36d14f7efaf774c7ea641900e (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
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
-- tkz_elements_lines.lua
-- date 23/12/03
-- version 1.40c
-- 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.

-- -------------------------------------------------------------------------
--                           Lines
-- -------------------------------------------------------------------------
line = {}
function line: new(za, zb)
    local type             = 'line'
    local mid              = (za+zb)/2
    local north_pa         = rotation_ (za,math.pi/2,zb)
    local south_pa         = rotation_ (za,-math.pi/2,zb)
    local north_pb         = rotation_ (zb,-math.pi/2,za)
    local south_pb         = rotation_ (zb,math.pi/2,za)
    local west             = rotation_ (za,math.pi/2,north_pa)
    local east             = rotation_ (zb,math.pi/2,south_pb)
    local slope            = point.arg(zb-za)
    local length           = point.mod(zb-za)
    local o = {pa          = za, 
               pb          = zb,
               north_pa    = north_pa, 
               south_pa    = south_pa,
               west        = west, 
               east        = east,
               north_pb    = north_pb, 
               south_pb    = south_pb,
               slope       = slope,
               mid         = mid, 
               type        = type, 
               length      = length}
    setmetatable(o, self)
    self.__index = self
    return o
end

-------------------
-- Result -> real
-------------------
function line: distance (pt)   
    return point.mod(projection(self,pt)-pt)
end

function line: slope ()
   return slope_(self.pa,self.pb)
end
-------------------
-- Result -> boolean
-------------------
function line: in_out (pt)
    local sc,epsilon
    epsilon = 10^(-12)
    sc = math.abs ((pt-self.pa)^(pt-self.pb))
    if sc <= epsilon
     then
       return true
    else
       return false
    end
end

-------------------
-- Result -> point
-------------------
function line: barycenter (ka,kb)
    return barycenter_({self.pa,ka},{self.pb,kb})
end

function line: point (t) --  t=o A  t=1 B  t = AM / AB 
    return barycenter_({self.pa,1-t},{self.pb,(t)})
end

function line: midpoint ()
    return (self.pa+self.pb)/2
end

function line: harmonic_int (pt)
    return div_harmonic_int_(self.pa,self.pb,pt)
end

function line: harmonic_ext (pt)
    return div_harmonic_ext_(self.pa,self.pb,pt)
end

function line: harmonic_both (k)
    return div_harmonic_both_(self.pa,self.pb,k)
end

function line: gold_ratio()
   return self.pa + (self.pb-self.pa)*tkzinvphi
end

function line: normalize ()
   return  self.pa+(self.pb-self.pa)/point.mod(self.pb-self.pa)
end

function line: normalize_inv ()
   return normalize_ (self.pb,self.pa)
end
-------------- transformations -------------
function line: translation_pt ( pt )
    return translation_ ( self.pb-self.pa,pt )
end

function line: translation_C ( obj )
   local pa,pb,x,y
   pa = obj.center
   pb = obj.through
   x,y = set_translation_ ( self.pb-self.pa,pa,pb )
   return circle : new  (x,y)
end

function line: translation_T ( obj )
   local pa,pb,pc,x,y,z
   pa    = obj.pa
   pb    = obj.pb
   pc    = obj.pc
   x,y,z = set_translation_ ( self.pb-self.pa,pa,pb,pc )
   return triangle : new  (x,y,z)
end

function line: translation_L ( obj )
   local pa,pb,x,y
   pa = obj.pa
   pb = obj.pb
   x,y = set_translation_ ( self.pb-self.pa,pa,pb )
   return line : new  (x,y)
end

function line: translation (...)
   local obj,nb,t
   local tp = table.pack(...)
   obj = tp[1]
   nb = tp.n
    if nb == 1 then
       if obj.type == "point" then
          return translation_ ( self.pb-self.pa,obj )
       elseif  obj.type == "line" then
          return self: translation_L (obj)
       elseif obj.type == "triangle" then
          return self: translation_T (obj)
       else
          return self: translation_C (obj)
       end
    else
      t = {}
       for i=1,tp.n do
      table.insert(t , translation_ ( self.pb-self.pa , tp[i])) 
         end
      return table.unpack ( t )      
    end
end

function line: set_translation ( ...)
    return set_translation_ ( self.pb-self.pa,... )
end

function line: projection (...)
   local obj,nb,t
   local tp = table.pack(...)
   obj = tp[1]
   nb = tp.n
    if nb == 1 then
          return projection_ ( self.pa, self.pb, obj )
    else
        t = {}
        for i=1,tp.n do
            table.insert( t , projection_ (self.pa, self.pb, tp[i])  ) 
         end
      return table.unpack ( t )      
    end
end

function line: set_projection (...)
	local tp = table.pack(...)
	local i
    local t = {}
	for i=1,tp.n do
        table.insert( t , projection_ (self.pa,self.pb , tp[i])  ) 
	end
  return table.unpack ( t )
end

function line: symmetry_axial_L ( obj )
   local pa,pb,x,y
   pa = obj.pa
   pb = obj.pb
   x,y = self:set_reflection ( pa,pb )
   return line : new  (x,y)
end
function line: symmetry_axial_T ( obj )
   local pa,pb,pc,x,y,z
   pa    = obj.pa
   pb    = obj.pb
   pc    = obj.pc
   x,y,z = self:set_reflection ( pa,pb,pc )
   return triangle : new  (x,y,z)
end

function line: symmetry_axial_C ( obj )
   local pa,pb,x,y
   pa = obj.center
   pb = obj.through
   x,y = self:set_reflection ( pa,pb )
   return circle : new  (x,y)
end

function line: reflection (...)
   local obj,nb,t
   local tp = table.pack(...)
   obj = tp[1]
   nb = tp.n
    if nb == 1 then
       if obj.type == "point" then
          return symmetry_axial_ ( self.pa,self.pb,obj )
       elseif  obj.type == "line" then
          return self: symmetry_axial_L (obj)
       elseif obj.type == "triangle" then
          return self: symmetry_axial_T (obj)
       else
         return self: symmetry_axial_C (obj)
       end
    else
        t = {}
        for i=1,tp.n do
            table.insert( t , symmetry_axial_ ( self.pa,self.pb , tp[i])  ) 
         end
      return table.unpack ( t )      
    end
end

function line: set_reflection (...)
    return set_symmetry_axial_ ( self.pb,self.pa,... )
end

-------------------
-- Result -> line
-------------------
function line: ll_from ( pt )
	return line : new (pt,pt+self.pb-self.pa) 
end

function line: ortho_from ( pt )
	return  line : new (pt,pt+(self.pb-self.pa)*point(0,1))
end

function line: mediator () 
   local m
   m = midpoint_ (self.pa,self.pb)
  return line : new (m,rotation_ (m,math.pi/2,self.pb)) 
end
-------------------
-- Result -> circle
-------------------
function line: circle ()   
    return circle : new (self.pa,self.pb)
end

function line: circle_swap ()   
    return circle : new (self.pb,self.pa)
end

----------------------
-- Result -> triangle
----------------------
function line: equilateral (swap)
    if swap == nil then
        swap = false
    end
   if swap  then 
        return triangle : new (self.pa,self.pb,rotation_ (self.pa,-math.pi/3,self.pb))
    else
          return triangle : new (self.pa,self.pb,rotation_ (self.pa,math.pi/3,self.pb))
  
end
end


function line: isosceles (phi)
    local pta,ptb
    pta = rotation_ (self.pa,phi,self.pb)
    ptb = rotation_ (self.pb,-phi,self.pa)
  return triangle : new (self.pa,self.pb, intersection_ll_ (self.pa,pta,self.pb,ptb ))
  
end

function line: gold ()
    local pt
    pt = rotation_ (self.pa,math.pi/2,self.pb)
     return triangle : new (self.pa,self.pb, self.pa + (pt-self.pa) * tkzinvphi)
end

function line: euclide ()
     return triangle : new (self.pa,self.pb, rotation_ (self.pa,math.pi/5,self.pb))
  end

function line: golden ()
    local pt
    pt = rotation_ (self.pa,2*math.pi/5,self.pb)
    return triangle : new (self.pa,self.pb, self.pa + (pt-self.pa) * tkzphi)
end
------------------------------
-- Result -> couple of points
------------------------------
function line: square ()
   return square : side (self.pa,self.pb)
end

return line