summaryrefslogtreecommitdiff
path: root/trunk/reprap/miscellaneous/adrians-java/Polygon-java/rr_box.java
blob: c16f41c01bf52d06415ee6b0f6c5ef6d6dc10241 (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
/*

RepRap
------

The Replicating Rapid Prototyper Project


Copyright (C) 2005
Adrian Bowyer & The University of Bath

http://reprap.org

Principal author:

Adrian Bowyer
Department of Mechanical Engineering
Faculty of Engineering and Design
University of Bath
Bath BA2 7AY
U.K.

e-mail: A.Bowyer@bath.ac.uk

RepRap is free; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
Licence as published by the Free Software Foundation; either
version 2 of the Licence, or (at your option) any later version.

RepRap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public Licence for more details.

For this purpose the words "software" and "library" in the GNU Library
General Public Licence are taken to mean any and all computer programs
computer files data results documents and other copyright information
available from the RepRap project.

You should have received a copy of the GNU Library General Public
Licence along with RepRap; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA,
or see

http://www.gnu.org/

=====================================================================

rr_box: 2D rectangles

First version 20 May 2005
This version: 2 October 2005 (translation to Java)

*/


// A 2D box is an X and a Y interval

class rr_box
{
    public rr_interval x;
    public rr_interval y;
    private boolean empty;

    public rr_box()
    {
	empty = true;
    }

    public rr_box(rr_box b)
    {
	x = new rr_interval(b.x);
	y = new rr_interval(b.y);
        empty = b.empty;
    }


    // Expand the box to incorporate another or a point

    public void expand(rr_box a)
    {
	if(a.empty)
	    return;
	if(empty)
	    {
                empty = false;
                x = new rr_interval(a.x);
                y = new rr_interval(a.y);
	    } else
	    {
		x.expand(a.x);
		y.expand(a.y);
	    }
    }

    public void expand(rr_2point a)
    {
	if(empty)
	    {
                empty = false;
                x = new rr_interval(a.x, a.x);
                y = new rr_interval(a.y, a.y);
	    } else
	    {
		x.expand(a.x);
		y.expand(a.y);
	    }
    }

    // Corner points and center

    public rr_2point max()
    {
	return new rr_2point(x.high, y.high);
    }

    public rr_2point min()
    {
	return new rr_2point(x.low, y.low);
    }

    public rr_2point center()
    {
	return ((max().add(min()).mul(0.5)));
    }

    // Scale the box by a factor about its center
    
    public rr_box scale(double f)
    {
        rr_box r = new rr_box();
	if(empty)
	    return r;
	f = 0.5*f;
	rr_2point p = new rr_2point(x.length()*f, y.length()*f);
	rr_2point c = center();
        r.expand(c.add(p));
        r.expand(c.sub(p));
	return r;
    }

    // Convert to a string
    
    public String toString()
    {
	if(empty)
	    return "[empty]";
        return "<BOX x:" + x.toString() + ", y: " + y.toString() + ">";
    }
 

    // Squared diagonal

    public double d_2()
    {
	if(empty)
	    return 0;
	return max().d_2(min());
    }
}