Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * The Contents of this file are made available subject to the terms of
5 : * either of the following licenses
6 : *
7 : * - GNU Lesser General Public License Version 2.1
8 : * - Sun Industry Standards Source License Version 1.1
9 : *
10 : * Sun Microsystems Inc., October, 2000
11 : *
12 : * GNU Lesser General Public License Version 2.1
13 : * =============================================
14 : * Copyright 2000 by Sun Microsystems, Inc.
15 : * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 : *
17 : * This library is free software; you can redistribute it and/or
18 : * modify it under the terms of the GNU Lesser General Public
19 : * License version 2.1, as published by the Free Software Foundation.
20 : *
21 : * This library is distributed in the hope that it will be useful,
22 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : * Lesser General Public License for more details.
25 : *
26 : * You should have received a copy of the GNU Lesser General Public
27 : * License along with this library; if not, write to the Free Software
28 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 : * MA 02111-1307 USA
30 : *
31 : *
32 : * Sun Industry Standards Source License Version 1.1
33 : * =================================================
34 : * The contents of this file are subject to the Sun Industry Standards
35 : * Source License Version 1.1 (the "License"); You may not use this file
36 : * except in compliance with the License. You may obtain a copy of the
37 : * License at http://www.openoffice.org/license.html.
38 : *
39 : * Software provided under this License is provided on an "AS IS" basis,
40 : * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 : * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 : * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 : * See the License for the specific provisions governing your rights and
44 : * obligations concerning the Software.
45 : *
46 : * The Initial Developer of the Original Code is: IBM Corporation
47 : *
48 : * Copyright: 2008 by IBM Corporation
49 : *
50 : * All Rights Reserved.
51 : *
52 : * Contributor(s): _______________________________________
53 : *
54 : *
55 : ************************************************************************/
56 : /*****************************************************************************
57 : * Change History
58 : * Mar 2005 Revised for lwpfilter
59 : ****************************************************************************/
60 : /**
61 : * @file
62 : * For LWP filter architecture prototype
63 : * Implementation file of SdwRectangle.
64 : */
65 : #include "lwpsdwrect.hxx"
66 :
67 : /**************************************************************************
68 : * @date: 11/19/2004
69 : * @short: Default constructor
70 : **************************************************************************/
71 0 : SdwRectangle::SdwRectangle() : m_bRotated(sal_False)
72 : {
73 0 : for (sal_uInt16 i = 0; i < 4; i++)
74 : {
75 0 : m_nRectCorner[0] = Point(0, 0);
76 : }
77 0 : }
78 : /**************************************************************************
79 : * @date: 11/19/2004
80 : * @short: Constructor
81 : * @param: aPt0~aPt3 four corner points of a rectangle.
82 : **************************************************************************/
83 0 : SdwRectangle::SdwRectangle(const Point& rPt0, const Point& rPt1,
84 0 : const Point& rPt2, const Point& rPt3) : m_bRotated(sal_True)
85 : {
86 0 : m_nRectCorner[0] = rPt0;
87 0 : m_nRectCorner[1] = rPt1;
88 0 : m_nRectCorner[2] = rPt2;
89 0 : m_nRectCorner[3] = rPt3;
90 :
91 0 : if (rPt0.Y() == rPt1.Y() && rPt0.Y() < rPt3.Y())
92 : {
93 0 : m_bRotated = sal_False;
94 : }
95 0 : }
96 :
97 : /**************************************************************************
98 : * @date: 12/15/2004
99 : * @short: Copy constructor
100 : **************************************************************************/
101 0 : SdwRectangle::SdwRectangle(const SdwRectangle& rOther)
102 : {
103 0 : m_nRectCorner[0] = rOther.m_nRectCorner[0];
104 0 : m_nRectCorner[1] = rOther.m_nRectCorner[1];
105 0 : m_nRectCorner[2] = rOther.m_nRectCorner[2];
106 0 : m_nRectCorner[3] = rOther.m_nRectCorner[3];
107 :
108 0 : m_bRotated = rOther.IsRectRotated();
109 0 : }
110 : /**************************************************************************
111 : * @date: 11/19/2004
112 : * @short: Destructor
113 : **************************************************************************/
114 0 : SdwRectangle::~SdwRectangle()
115 : {
116 0 : }
117 : /**************************************************************************
118 : * @date: 11/19/2004
119 : * @short: Get the flag whether if the rectangle has been rotated.
120 : * @return: m_bRotated rotation flag.
121 : **************************************************************************/
122 0 : sal_Bool SdwRectangle::IsRectRotated() const
123 : {
124 0 : return m_bRotated;
125 : }
126 : /**************************************************************************
127 : * @date: 11/19/2004
128 : * @short: Calculate and return center point of the rectangle.
129 : * @return: center point
130 : **************************************************************************/
131 0 : Point SdwRectangle::GetRectCenter() const
132 : {
133 0 : long nX = (long)((double)(m_nRectCorner[0].X() + m_nRectCorner[2].X())/2 + 0.5);
134 0 : long nY = (long)((double)(m_nRectCorner[0].Y() + m_nRectCorner[2].Y())/2 + 0.5);
135 :
136 0 : return Point(nX, nY);
137 : }
138 : /**************************************************************************
139 : * @date: 11/19/2004
140 : * @short: Calculate width of the rectangle.
141 : * @return: rectangle width.
142 : **************************************************************************/
143 0 : long SdwRectangle::GetWidth() const
144 : {
145 0 : long nX0 = m_nRectCorner[0].X();
146 0 : long nY0 = m_nRectCorner[0].Y();
147 0 : long nX1 = m_nRectCorner[1].X();
148 0 : long nY1 = m_nRectCorner[1].Y();
149 :
150 0 : return (long)CalcDistBetween2Points(nX0, nY0, nX1, nY1);
151 : }
152 : /**************************************************************************
153 : * @date: 11/19/2004
154 : * @short: Calculate height of the rectangle.
155 : * @return: rectangle height.
156 : **************************************************************************/
157 0 : long SdwRectangle::GetHeight() const
158 : {
159 0 : long nX1 = m_nRectCorner[1].X();
160 0 : long nY1 = m_nRectCorner[1].Y();
161 0 : long nX2 = m_nRectCorner[2].X();
162 0 : long nY2 = m_nRectCorner[2].Y();
163 :
164 0 : return (long)CalcDistBetween2Points(nX1, nY1, nX2, nY2);
165 : }
166 : /**************************************************************************
167 : * @date: 11/19/2004
168 : * @short: Calculate coordinate of the original rectangle.
169 : * @return: a prz rectangle
170 : **************************************************************************/
171 0 : Rectangle SdwRectangle::GetOriginalRect() const
172 : {
173 0 : if (m_bRotated)
174 : {
175 0 : long nHeight = GetHeight();
176 0 : long nWidth = GetWidth();
177 0 : Point aCenter = GetRectCenter();
178 :
179 0 : Point aLT(aCenter.X()-(long)((double)nWidth/2+0.5),
180 0 : aCenter.Y()-(long)((double)nHeight/2+0.5));
181 0 : Point aRT(aLT.X()+nWidth, aLT.Y());
182 0 : Point aLB(aLT.X(), aLT.Y()-nHeight);
183 0 : Point aRB(aLT.X()+nWidth, aLT.Y()+nHeight);
184 :
185 0 : return Rectangle(aLT, aRB);
186 : }
187 : else
188 : {
189 0 : return Rectangle(m_nRectCorner[3], m_nRectCorner[1]);
190 : }
191 : }
192 : /**************************************************************************
193 : * @date: 11/19/2004
194 : * @short: Calculate rotation angle of the rectangle.
195 : * @return: rotation angle.
196 : **************************************************************************/
197 0 : double SdwRectangle::GetRotationAngle() const
198 : {
199 0 : if (!m_bRotated)
200 : {
201 0 : return 0.00;
202 : }
203 :
204 0 : double fX1 = (double)(m_nRectCorner[1].X());
205 0 : double fY1 = (double)(m_nRectCorner[1].Y());
206 0 : double fX2 = (double)(m_nRectCorner[2].X());
207 0 : double fY2 = (double)(m_nRectCorner[2].Y());
208 0 : double fMidX = (fX1 + fX2) / 2;
209 0 : double fMidY = (fY1 + fY2) / 2;
210 :
211 0 : Point aCenter = GetRectCenter();
212 0 : double fCenterX = (double)aCenter.X();
213 0 : double fCenterY = (double)aCenter.Y();
214 :
215 0 : double fAngle = atan2((fMidY - fCenterY), (fMidX - fCenterX));
216 :
217 0 : return -fAngle;
218 : }
219 :
220 0 : double SdwRectangle::CalcDistBetween2Points(long nX1, long nY1, long nX2, long nY2)
221 : {
222 0 : return sqrt((double)((nX1-nX2)*(nX1-nX2) + (nY1-nY2)*(nY1-nY2)));
223 : }
224 :
225 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|