1
----------------------------------------------------------------------
3
----------------------------------------------------------------------
6
This file is part of the extensible drawing editor Ipe.
7
Copyright (C) 1993-2009 Otfried Cheong
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.
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.
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.
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.
31
label = "Euclidean Geometry"
34
Create the incircle or the three excircles of a triangle.
36
Original Ipe 6 version by Jari Lappalainen, this ipelet is now part of
40
function incorrect(model)
41
model:warning("Primary selection is not a triangle")
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
50
if obj:type() ~= "path" then incorrect(model) return end
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")
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]
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)
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 } )
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)
86
local AB = ipe.LineThrough(a, b)
87
local radius = AB:distance(center)
88
return create_circle(model, center, radius)
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)
101
local AB = ipe.LineThrough(a, b)
102
local radius = AB:distance(center)
103
return create_circle(model, center, radius)
107
function create_incircle(model)
108
local a, b, c = collect_vertices(model)
109
if not a then return end
111
local obj = incircle(model, a, b, c)
113
model:creation("create incircle of triangle", obj)
117
function create_excircles(model)
118
local a, b, c = collect_vertices(model)
119
if not a then return end
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
129
local group = ipe.Group(circles)
130
model:creation("create excircles of triangles", group)
134
{ label="Incircle of triangle", run = create_incircle },
135
{ label="Excircles of triangle", run = create_excircles },
138
----------------------------------------------------------------------