]> git.donarmstrong.com Git - lilypond.git/blob - lily/box.cc
Make Box::is_empty more selective and create an axis-specific variant
[lilypond.git] / lily / box.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "box.hh"
21
22 void
23 Box::translate (Offset o)
24 {
25   for (Axis i = X_AXIS; i < NO_AXES; incr (i))
26     interval_a_[i] += o[i];
27 }
28
29 void
30 Box::unite (Box b)
31 {
32   for (Axis i = X_AXIS; i < NO_AXES; incr (i))
33     interval_a_[i].unite (b[i]);
34 }
35
36 Real
37 Box::area () const
38 {
39   return interval_a_[X_AXIS].length ()
40          * interval_a_[Y_AXIS].length ();
41 }
42
43 Box::Box ()
44 {
45 }
46
47 void
48 Box::set_empty ()
49 {
50   interval_a_[X_AXIS].set_empty ();
51   interval_a_[Y_AXIS].set_empty ();
52 }
53
54 bool
55 Box::is_empty () const
56 {
57   return is_empty (X_AXIS) && is_empty (Y_AXIS);
58 }
59
60 bool
61 Box::is_empty (Axis a) const
62 {
63   Interval empty;
64   empty.set_empty ();
65   return interval_a_[a][LEFT] == empty[LEFT]
66     && interval_a_[a][RIGHT] == empty[RIGHT];
67 }
68
69 Box::Box (Interval ix, Interval iy)
70 {
71   x () = ix;
72   y () = iy;
73 }
74
75 Interval &
76 Box::operator [] (Axis a)
77 {
78   return interval_a_[a];
79 }
80
81 Interval
82 Box::operator [] (Axis a) const
83 {
84   return interval_a_[a];
85 }
86
87 void
88 Box::scale (Real s)
89 {
90   interval_a_[X_AXIS] *= s;
91   interval_a_[Y_AXIS] *= s;
92 }
93
94 void
95 Box::add_point (Offset o)
96 {
97   interval_a_[X_AXIS].add_point (o[X_AXIS]);
98   interval_a_[Y_AXIS].add_point (o[Y_AXIS]);
99 }
100
101 Offset
102 Box::center () const
103 {
104   return Offset (interval_a_[X_AXIS].center (),
105                  interval_a_[Y_AXIS].center ());
106 }
107
108 void
109 Box::widen (Real x, Real y)
110 {
111   interval_a_[X_AXIS].widen (x);
112   interval_a_[Y_AXIS].widen (y);
113 }
114
115 void
116 Box::intersect (Box b)
117 {
118   interval_a_[X_AXIS].intersect (b[X_AXIS]);
119   interval_a_[Y_AXIS].intersect (b[Y_AXIS]);
120 }
121
122 // for debugging
123
124 void
125 Box::print ()
126 {
127   printf ("X left %4.4f right %4.4f Y down %4.4f up %4.4f\n",
128           interval_a_[X_AXIS][LEFT], interval_a_[X_AXIS][RIGHT],
129           interval_a_[Y_AXIS][DOWN], interval_a_[Y_AXIS][UP]);
130 }
131
132 /****************************************************************/
133
134 #include "ly-smobs.icc"
135
136 IMPLEMENT_SIMPLE_SMOBS (Box);
137 IMPLEMENT_TYPE_P (Box, "ly:box?");
138 IMPLEMENT_DEFAULT_EQUAL_P (Box);
139
140 SCM
141 Box::mark_smob (SCM /* x */)
142 {
143   return SCM_EOL;
144 }
145
146 int
147 Box::print_smob (SCM /* x */, SCM p, scm_print_state *)
148 {
149   scm_puts ("#<Box>", p);
150   return 1;
151 }