~ubuntu-branches/ubuntu/precise/ipe/precise

« back to all changes in this revision

Viewing changes to src/ipelets/lua/euclid.lua

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2009-12-11 21:22:35 UTC
  • mfrom: (4.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20091211212235-5iio4nzpra64snab
Tags: 7.0.10-1
* New upstream.  Closes: #551192.
  - New build-depends: libcairo2-dev, liblua5.1-0-dev, gsfonts
  - patches/config.diff: Remove.  Upstream build system replaced.
  - Runtime lib package changed to libipe7.0.10 from libipe1c2a
  - Devel package renamed to libipe-dev (from libipe1-dev)
  - Package ipe depends on lua5.1 due to ipe-update-master.

* rules: Re-write to use dh.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
----------------------------------------------------------------------
 
2
-- Euclid ipelet
 
3
----------------------------------------------------------------------
 
4
--[[
 
5
 
 
6
    This file is part of the extensible drawing editor Ipe.
 
7
    Copyright (C) 1993-2009  Otfried Cheong
 
8
 
 
9
    Ipe is free software; you can redistribute it and/or modify it
 
10
    under the terms of the GNU General Public License as published by
 
11
    the Free Software Foundation; either version 3 of the License, or
 
12
    (at your option) any later version.
 
13
 
 
14
    As a special exception, you have permission to link Ipe with the
 
15
    CGAL library and distribute executables, as long as you follow the
 
16
    requirements of the Gnu General Public License in regard to all of
 
17
    the software in the executable aside from CGAL.
 
18
 
 
19
    Ipe is distributed in the hope that it will be useful, but WITHOUT
 
20
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
21
    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 
22
    License for more details.
 
23
 
 
24
    You should have received a copy of the GNU General Public License
 
25
    along with Ipe; if not, you can find it at
 
26
    "http://www.gnu.org/copyleft/gpl.html", or write to the Free
 
27
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
28
 
 
29
--]]
 
30
 
 
31
label = "Euclidean Geometry"
 
32
 
 
33
about = [[
 
34
Create the incircle or the three excircles of a triangle.
 
35
 
 
36
Original Ipe 6 version by Jari Lappalainen, this ipelet is now part of
 
37
Ipe.
 
38
]]
 
39
 
 
40
function incorrect(model)
 
41
  model:warning("Primary selection is not a triangle")
 
42
end
 
43
 
 
44
function collect_vertices(model)
 
45
  local p = model:page()
 
46
  local prim = p:primarySelection()
 
47
  if not prim then model.ui:explain("no selection") return end
 
48
 
 
49
  local obj = p[prim]
 
50
  if obj:type() ~= "path" then incorrect(model) return end
 
51
 
 
52
  local shape = obj:shape()
 
53
  if (#shape ~= 1 or shape[1].type ~= "curve" or #shape[1] ~= 2
 
54
      or shape[1][1].type ~= "segment" or shape[1][2].type ~= "segment")
 
55
  then
 
56
    incorrect(model)
 
57
    return
 
58
  end
 
59
 
 
60
  local m = obj:matrix()
 
61
  local a = m * shape[1][1][1]
 
62
  local b = m * shape[1][1][2]
 
63
  local c = m * shape[1][2][2]
 
64
  return a, b, c
 
65
end
 
66
 
 
67
function angle_bisector(origin, dir1, dir2)
 
68
  assert(dir1:sqLen() > 0)
 
69
  assert(dir2:sqLen() > 0)
 
70
  local bisector = dir1:normalized() + dir2:normalized()
 
71
  if bisector:sqLen() == 0 then bisector = dir1:orthogonal() end
 
72
  return ipe.LineThrough(origin, origin + bisector)
 
73
end
 
74
 
 
75
function create_circle(model, center, radius)
 
76
  local shape =  { type="ellipse";
 
77
                   ipe.Matrix(radius, 0, 0, radius, center.x, center.y) }
 
78
  return ipe.Path(model.attributes, { shape } )
 
79
end
 
80
 
 
81
function incircle(model, a, b, c)
 
82
  local b1 = angle_bisector(a, b - a, c - a)
 
83
  local b2 = angle_bisector(b, c - b, a - b)
 
84
  local center = b1:intersects(b2)
 
85
  if (center) then
 
86
    local AB = ipe.LineThrough(a, b)
 
87
    local radius = AB:distance(center)
 
88
    return create_circle(model, center, radius)
 
89
  end
 
90
end
 
91
 
 
92
function excircle(model, a, b, c)
 
93
  local b1 = angle_bisector(a, b - a, c - a)
 
94
  local b2 = angle_bisector(b, c - b, a - b)
 
95
  local n1 = b1:normal()
 
96
  local n2 = b2:normal()
 
97
  local nl1 = ipe.LineThrough(a, a + n1)
 
98
  local nl2 = ipe.LineThrough(b, b + n2)
 
99
  local center = nl1:intersects(nl2)
 
100
  if center then
 
101
    local AB = ipe.LineThrough(a, b)
 
102
    local radius = AB:distance(center)
 
103
    return create_circle(model, center, radius)
 
104
  end
 
105
end
 
106
 
 
107
function create_incircle(model)
 
108
  local a, b, c = collect_vertices(model)
 
109
  if not a then return end
 
110
 
 
111
  local obj = incircle(model, a, b, c)
 
112
  if obj then
 
113
    model:creation("create incircle of triangle", obj)
 
114
  end
 
115
end
 
116
 
 
117
function create_excircles(model)
 
118
  local a, b, c = collect_vertices(model)
 
119
  if not a then return end
 
120
 
 
121
  local circles = {}
 
122
  local obj = excircle(model, a, b, c)
 
123
  if obj then circles[#circles + 1] = obj end
 
124
  obj = excircle(model, b, c, a)
 
125
  if obj then circles[#circles + 1] = obj end
 
126
  obj = excircle(model, c, a, b)
 
127
  if obj then circles[#circles + 1] = obj end
 
128
 
 
129
  local group = ipe.Group(circles)
 
130
  model:creation("create excircles of triangles", group)
 
131
end
 
132
 
 
133
methods = {
 
134
  { label="Incircle of triangle", run = create_incircle },
 
135
  { label="Excircles of triangle", run = create_excircles },
 
136
}
 
137
 
 
138
----------------------------------------------------------------------