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(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(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 = 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: Calculate and return center point of the rectangle.
120 : * @return: center point
121 : **************************************************************************/
122 0 : Point SdwRectangle::GetRectCenter() const
123 : {
124 0 : long nX = (long)((double)(m_nRectCorner[0].X() + m_nRectCorner[2].X())/2 + 0.5);
125 0 : long nY = (long)((double)(m_nRectCorner[0].Y() + m_nRectCorner[2].Y())/2 + 0.5);
126 :
127 0 : return Point(nX, nY);
128 : }
129 : /**************************************************************************
130 : * @date: 11/19/2004
131 : * @short: Calculate width of the rectangle.
132 : * @return: rectangle width.
133 : **************************************************************************/
134 0 : long SdwRectangle::GetWidth() const
135 : {
136 0 : long nX0 = m_nRectCorner[0].X();
137 0 : long nY0 = m_nRectCorner[0].Y();
138 0 : long nX1 = m_nRectCorner[1].X();
139 0 : long nY1 = m_nRectCorner[1].Y();
140 :
141 0 : return (long)CalcDistBetween2Points(nX0, nY0, nX1, nY1);
142 : }
143 : /**************************************************************************
144 : * @date: 11/19/2004
145 : * @short: Calculate height of the rectangle.
146 : * @return: rectangle height.
147 : **************************************************************************/
148 0 : long SdwRectangle::GetHeight() const
149 : {
150 0 : long nX1 = m_nRectCorner[1].X();
151 0 : long nY1 = m_nRectCorner[1].Y();
152 0 : long nX2 = m_nRectCorner[2].X();
153 0 : long nY2 = m_nRectCorner[2].Y();
154 :
155 0 : return (long)CalcDistBetween2Points(nX1, nY1, nX2, nY2);
156 : }
157 : /**************************************************************************
158 : * @date: 11/19/2004
159 : * @short: Calculate coordinate of the original rectangle.
160 : * @return: a prz rectangle
161 : **************************************************************************/
162 0 : Rectangle SdwRectangle::GetOriginalRect() const
163 : {
164 0 : if (m_bRotated)
165 : {
166 0 : long nHeight = GetHeight();
167 0 : long nWidth = GetWidth();
168 0 : Point aCenter = GetRectCenter();
169 :
170 0 : Point aLT(aCenter.X()-(long)((double)nWidth/2+0.5),
171 0 : aCenter.Y()-(long)((double)nHeight/2+0.5));
172 0 : Point aRB(aLT.X()+nWidth, aLT.Y()+nHeight);
173 :
174 0 : return Rectangle(aLT, aRB);
175 : }
176 : else
177 : {
178 0 : return Rectangle(m_nRectCorner[3], m_nRectCorner[1]);
179 : }
180 : }
181 : /**************************************************************************
182 : * @date: 11/19/2004
183 : * @short: Calculate rotation angle of the rectangle.
184 : * @return: rotation angle.
185 : **************************************************************************/
186 0 : double SdwRectangle::GetRotationAngle() const
187 : {
188 0 : if (!m_bRotated)
189 : {
190 0 : return 0.00;
191 : }
192 :
193 0 : double fX1 = (double)(m_nRectCorner[1].X());
194 0 : double fY1 = (double)(m_nRectCorner[1].Y());
195 0 : double fX2 = (double)(m_nRectCorner[2].X());
196 0 : double fY2 = (double)(m_nRectCorner[2].Y());
197 0 : double fMidX = (fX1 + fX2) / 2;
198 0 : double fMidY = (fY1 + fY2) / 2;
199 :
200 0 : Point aCenter = GetRectCenter();
201 0 : double fCenterX = (double)aCenter.X();
202 0 : double fCenterY = (double)aCenter.Y();
203 :
204 0 : double fAngle = atan2((fMidY - fCenterY), (fMidX - fCenterX));
205 :
206 0 : return -fAngle;
207 : }
208 :
209 0 : double SdwRectangle::CalcDistBetween2Points(long nX1, long nY1, long nX2, long nY2)
210 : {
211 0 : return sqrt((double)((nX1-nX2)*(nX1-nX2) + (nY1-nY2)*(nY1-nY2)));
212 : }
213 :
214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|