xref: /aoo41x/main/tools/source/generic/line.cxx (revision 89b56da7)
1*89b56da7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*89b56da7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*89b56da7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*89b56da7SAndrew Rist  * distributed with this work for additional information
6*89b56da7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*89b56da7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*89b56da7SAndrew Rist  * "License"); you may not use this file except in compliance
9*89b56da7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*89b56da7SAndrew Rist  *
11*89b56da7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*89b56da7SAndrew Rist  *
13*89b56da7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*89b56da7SAndrew Rist  * software distributed under the License is distributed on an
15*89b56da7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*89b56da7SAndrew Rist  * KIND, either express or implied.  See the License for the
17*89b56da7SAndrew Rist  * specific language governing permissions and limitations
18*89b56da7SAndrew Rist  * under the License.
19*89b56da7SAndrew Rist  *
20*89b56da7SAndrew Rist  *************************************************************/
21*89b56da7SAndrew Rist 
22*89b56da7SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_tools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #define _LINE_CXX
28cdf0e10cSrcweir #include <tools/link.hxx>
29cdf0e10cSrcweir #include <tools/line.hxx>
30cdf0e10cSrcweir #include <tools/debug.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <cstdlib>
33cdf0e10cSrcweir #include <math.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir // --------
36cdf0e10cSrcweir // - Line -
37cdf0e10cSrcweir // --------
38cdf0e10cSrcweir 
GetLength() const39cdf0e10cSrcweir double Line::GetLength() const
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     return hypot( maStart.X() - maEnd.X(), maStart.Y() - maEnd.Y() );
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
44cdf0e10cSrcweir // ------------------------------------------------------------------------
45cdf0e10cSrcweir 
Intersection(const Line & rLine,Point & rIntersection) const46cdf0e10cSrcweir sal_Bool Line::Intersection( const Line& rLine, Point& rIntersection ) const
47cdf0e10cSrcweir {
48cdf0e10cSrcweir 	double	fX, fY;
49cdf0e10cSrcweir 	sal_Bool	bRet;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	if( Intersection( rLine, fX, fY ) )
52cdf0e10cSrcweir 	{
53cdf0e10cSrcweir 		rIntersection.X() = FRound( fX );
54cdf0e10cSrcweir 		rIntersection.Y() = FRound( fY );
55cdf0e10cSrcweir 		bRet = sal_True;
56cdf0e10cSrcweir 	}
57cdf0e10cSrcweir 	else
58cdf0e10cSrcweir 		bRet = sal_False;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	return bRet;
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir // ------------------------------------------------------------------------
64cdf0e10cSrcweir 
Intersection(const Line & rLine,double & rIntersectionX,double & rIntersectionY) const65cdf0e10cSrcweir sal_Bool Line::Intersection( const Line& rLine, double& rIntersectionX, double& rIntersectionY ) const
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     const double    fAx = maEnd.X() - maStart.X();
68cdf0e10cSrcweir     const double    fAy = maEnd.Y() - maStart.Y();
69cdf0e10cSrcweir     const double    fBx = rLine.maStart.X() - rLine.maEnd.X();
70cdf0e10cSrcweir     const double    fBy = rLine.maStart.Y() - rLine.maEnd.Y();
71cdf0e10cSrcweir     const double    fDen = fAy * fBx - fAx * fBy;
72cdf0e10cSrcweir     sal_Bool            bOk = sal_False;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     if( fDen != 0. )
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir         const double    fCx = maStart.X() - rLine.maStart.X();
77cdf0e10cSrcweir         const double    fCy = maStart.Y() - rLine.maStart.Y();
78cdf0e10cSrcweir         const double    fA = fBy * fCx - fBx * fCy;
79cdf0e10cSrcweir         const sal_Bool      bGreater = ( fDen > 0. );
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         bOk = sal_True;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         if ( bGreater )
84cdf0e10cSrcweir         {
85cdf0e10cSrcweir             if ( ( fA < 0. ) || ( fA > fDen ) )
86cdf0e10cSrcweir                 bOk = sal_False;
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir         else if ( ( fA > 0. ) || ( fA < fDen ) )
89cdf0e10cSrcweir             bOk = sal_False;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         if ( bOk )
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir             const double fB = fAx * fCy - fAy * fCx;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir             if ( bGreater )
96cdf0e10cSrcweir             {
97cdf0e10cSrcweir                 if ( ( fB < 0. ) || ( fB > fDen ) )
98cdf0e10cSrcweir                     bOk = sal_False;
99cdf0e10cSrcweir             }
100cdf0e10cSrcweir             else if ( ( fB > 0. ) || ( fB < fDen ) )
101cdf0e10cSrcweir                 bOk = sal_False;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir             if( bOk )
104cdf0e10cSrcweir             {
105cdf0e10cSrcweir                 const double fAlpha = fA / fDen;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir                 rIntersectionX = ( maStart.X() + fAlpha * fAx );
108cdf0e10cSrcweir                 rIntersectionY = ( maStart.Y() + fAlpha * fAy );
109cdf0e10cSrcweir             }
110cdf0e10cSrcweir         }
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     return bOk;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir // ------------------------------------------------------------------------
117cdf0e10cSrcweir 
Intersection(const Rectangle & rRect,Line & rIntersection) const118cdf0e10cSrcweir sal_Bool Line::Intersection( const Rectangle& rRect, Line& rIntersection ) const
119cdf0e10cSrcweir {
120cdf0e10cSrcweir     const sal_Bool  bStartInside = rRect.IsInside( maStart );
121cdf0e10cSrcweir     const sal_Bool  bEndInside = rRect.IsInside( maEnd );
122cdf0e10cSrcweir     sal_Bool        bRet = sal_True;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     if( bStartInside && bEndInside )
125cdf0e10cSrcweir     {
126cdf0e10cSrcweir         // line completely inside rect
127cdf0e10cSrcweir         rIntersection.maStart = maStart;
128cdf0e10cSrcweir         rIntersection.maEnd = maEnd;
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir     else
131cdf0e10cSrcweir     {
132cdf0e10cSrcweir         // calculate intersections
133cdf0e10cSrcweir         const Point aTL( rRect.TopLeft() ), aTR( rRect.TopRight() );
134cdf0e10cSrcweir         const Point aBR( rRect.BottomRight() ), aBL( rRect.BottomLeft() );
135cdf0e10cSrcweir         Point       aIntersect1, aIntersect2;
136cdf0e10cSrcweir         Point*      pCurIntersection = &aIntersect1;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         if( Intersection( Line( aTL, aTR ), *pCurIntersection ) )
139cdf0e10cSrcweir             pCurIntersection = &aIntersect2;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         if( Intersection( Line( aTR, aBR ), *pCurIntersection ) )
142cdf0e10cSrcweir             pCurIntersection = ( pCurIntersection == &aIntersect1 ) ? &aIntersect2 : NULL;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         if( pCurIntersection && Intersection( Line( aBR, aBL ), *pCurIntersection ) )
145cdf0e10cSrcweir             pCurIntersection = ( pCurIntersection == &aIntersect1 ) ? &aIntersect2 : NULL;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         if( pCurIntersection && Intersection( Line( aBL, aTL ), *pCurIntersection ) )
148cdf0e10cSrcweir             pCurIntersection = ( pCurIntersection == &aIntersect1 ) ? &aIntersect2 : NULL;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir         if( !pCurIntersection )
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir             // two intersections
153cdf0e10cSrcweir             rIntersection.maStart = aIntersect1;
154cdf0e10cSrcweir             rIntersection.maEnd = aIntersect2;
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir         else if( pCurIntersection == &aIntersect2 )
157cdf0e10cSrcweir         {
158cdf0e10cSrcweir             // one intersection
159cdf0e10cSrcweir             rIntersection.maStart = aIntersect1;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir             if( ( maStart != aIntersect1 ) && bStartInside )
162cdf0e10cSrcweir                 rIntersection.maEnd = maStart;
163cdf0e10cSrcweir             else if( ( maEnd != aIntersect1 ) && bEndInside )
164cdf0e10cSrcweir                 rIntersection.maEnd = maEnd;
165cdf0e10cSrcweir             else
166cdf0e10cSrcweir                 rIntersection.maEnd = rIntersection.maStart;
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir         else
169cdf0e10cSrcweir             bRet = sal_False;
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     return bRet;
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir // ------------------------------------------------------------------------
176cdf0e10cSrcweir 
NearestPoint(const Point & rPoint) const177cdf0e10cSrcweir Point Line::NearestPoint( const Point& rPoint ) const
178cdf0e10cSrcweir {
179cdf0e10cSrcweir     Point aRetPt;
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     if ( maStart != maEnd )
182cdf0e10cSrcweir     {
183cdf0e10cSrcweir         const double    fDistX = maEnd.X() - maStart.X();
184cdf0e10cSrcweir         const double    fDistY = maStart.Y() - maEnd.Y();
185cdf0e10cSrcweir         const double    fTau = ( ( maStart.Y() - rPoint.Y() ) * fDistY -
186cdf0e10cSrcweir                                  ( maStart.X() - rPoint.X() ) * fDistX ) /
187cdf0e10cSrcweir                                ( fDistX * fDistX + fDistY * fDistY );
188cdf0e10cSrcweir 
189cdf0e10cSrcweir         if( fTau < 0.0 )
190cdf0e10cSrcweir             aRetPt = maStart;
191cdf0e10cSrcweir         else if( fTau <= 1.0 )
192cdf0e10cSrcweir         {
193cdf0e10cSrcweir             aRetPt.X() = FRound( maStart.X() + fTau * fDistX );
194cdf0e10cSrcweir             aRetPt.Y() = FRound( maStart.Y() - fTau * fDistY );
195cdf0e10cSrcweir         }
196cdf0e10cSrcweir         else
197cdf0e10cSrcweir             aRetPt = maEnd;
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir     else
200cdf0e10cSrcweir         aRetPt = maStart;
201cdf0e10cSrcweir 
202cdf0e10cSrcweir     return aRetPt;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir // ------------------------------------------------------------------------
206cdf0e10cSrcweir 
GetDistance(const double & rPtX,const double & rPtY) const207cdf0e10cSrcweir double Line::GetDistance( const double& rPtX, const double& rPtY ) const
208cdf0e10cSrcweir {
209cdf0e10cSrcweir     double fDist;
210cdf0e10cSrcweir 
211cdf0e10cSrcweir     if( maStart != maEnd )
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         const double    fDistX = maEnd.X() - maStart.X();
214cdf0e10cSrcweir         const double    fDistY = maEnd.Y() - maStart.Y();
215cdf0e10cSrcweir 		const double	fACX = maStart.X() - rPtX;
216cdf0e10cSrcweir 		const double	fACY = maStart.Y() - rPtY;
217cdf0e10cSrcweir 		const double	fL2 = fDistX * fDistX + fDistY * fDistY;
218cdf0e10cSrcweir         const double    fR = ( fACY * -fDistY - fACX * fDistX ) / fL2;
219cdf0e10cSrcweir         const double    fS = ( fACY * fDistX - fACX * fDistY ) / fL2;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir         if( fR < 0.0 )
222cdf0e10cSrcweir 		{
223cdf0e10cSrcweir 			fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 			if( fS < 0.0 )
226cdf0e10cSrcweir 				fDist *= -1.0;
227cdf0e10cSrcweir 		}
228cdf0e10cSrcweir         else if( fR <= 1.0 )
229cdf0e10cSrcweir 			fDist = fS * sqrt( fL2 );
230cdf0e10cSrcweir         else
231cdf0e10cSrcweir 		{
232cdf0e10cSrcweir 			fDist = hypot( maEnd.X() - rPtX, maEnd.Y() - rPtY );
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 			if( fS < 0.0 )
235cdf0e10cSrcweir 				fDist *= -1.0;
236cdf0e10cSrcweir 		}
237cdf0e10cSrcweir     }
238cdf0e10cSrcweir     else
239cdf0e10cSrcweir 		fDist = hypot( maStart.X() - rPtX, maStart.Y() - rPtY );
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     return fDist;
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir // ------------------------------------------------------------------------
245cdf0e10cSrcweir 
Enum(const Link & rEnumLink)246cdf0e10cSrcweir void Line::Enum( const Link& rEnumLink )
247cdf0e10cSrcweir {
248cdf0e10cSrcweir     DBG_ASSERT( rEnumLink.IsSet(), "This call doesn't make any sense with !rEnumLink.IsSet()" );
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     Point   aEnum;
251cdf0e10cSrcweir     long    nX;
252cdf0e10cSrcweir     long    nY;
253cdf0e10cSrcweir 
254cdf0e10cSrcweir     if( maStart.X() == maEnd.X() )
255cdf0e10cSrcweir     {
256cdf0e10cSrcweir         const long nEndY = maEnd.Y();
257cdf0e10cSrcweir 
258cdf0e10cSrcweir         nX = maStart.X();
259cdf0e10cSrcweir         nY = maStart.Y();
260cdf0e10cSrcweir 
261cdf0e10cSrcweir         if( nEndY > nY )
262cdf0e10cSrcweir         {
263cdf0e10cSrcweir             while( nY <= nEndY )
264cdf0e10cSrcweir             {
265cdf0e10cSrcweir                 aEnum.X() = nX;
266cdf0e10cSrcweir                 aEnum.Y() = nY++;
267cdf0e10cSrcweir                 rEnumLink.Call( &aEnum );
268cdf0e10cSrcweir             }
269cdf0e10cSrcweir         }
270cdf0e10cSrcweir         else
271cdf0e10cSrcweir         {
272cdf0e10cSrcweir             while( nY >= nEndY )
273cdf0e10cSrcweir             {
274cdf0e10cSrcweir                 aEnum.X() = nX;
275cdf0e10cSrcweir                 aEnum.Y() = nY--;
276cdf0e10cSrcweir                 rEnumLink.Call( &aEnum );
277cdf0e10cSrcweir             }
278cdf0e10cSrcweir         }
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir     else if( maStart.Y() == maEnd.Y() )
281cdf0e10cSrcweir     {
282cdf0e10cSrcweir         const long nEndX = maEnd.X();
283cdf0e10cSrcweir 
284cdf0e10cSrcweir         nX = maStart.X();
285cdf0e10cSrcweir         nY = maStart.Y();
286cdf0e10cSrcweir 
287cdf0e10cSrcweir         if( nEndX > nX )
288cdf0e10cSrcweir         {
289cdf0e10cSrcweir             while( nX <= nEndX )
290cdf0e10cSrcweir             {
291cdf0e10cSrcweir                 aEnum.X() = nX++;
292cdf0e10cSrcweir                 aEnum.Y() = nY;
293cdf0e10cSrcweir                 rEnumLink.Call( &aEnum );
294cdf0e10cSrcweir             }
295cdf0e10cSrcweir         }
296cdf0e10cSrcweir         else
297cdf0e10cSrcweir         {
298cdf0e10cSrcweir             while( nX >= nEndX )
299cdf0e10cSrcweir             {
300cdf0e10cSrcweir                 aEnum.X() = nX--;
301cdf0e10cSrcweir                 aEnum.Y() = nY;
302cdf0e10cSrcweir                 rEnumLink.Call( &aEnum );
303cdf0e10cSrcweir             }
304cdf0e10cSrcweir         }
305cdf0e10cSrcweir     }
306cdf0e10cSrcweir     else
307cdf0e10cSrcweir     {
308cdf0e10cSrcweir         const long  nDX = labs( maEnd.X() - maStart.X() );
309cdf0e10cSrcweir         const long  nDY = labs( maEnd.Y() - maStart.Y() );
310cdf0e10cSrcweir 		const long	nStartX = maStart.X();
311cdf0e10cSrcweir 		const long	nStartY = maStart.Y();
312cdf0e10cSrcweir 		const long	nEndX = maEnd.X();
313cdf0e10cSrcweir 		const long	nEndY = maEnd.Y();
314cdf0e10cSrcweir 		const long	nXInc = ( nStartX < nEndX ) ? 1L : -1L;
315cdf0e10cSrcweir 		const long	nYInc = ( nStartY < nEndY ) ? 1L : -1L;
316cdf0e10cSrcweir 
317cdf0e10cSrcweir         if( nDX >= nDY )
318cdf0e10cSrcweir         {
319cdf0e10cSrcweir             const long  nDYX = ( nDY - nDX ) << 1;
320cdf0e10cSrcweir             const long  nDY2 = nDY << 1;
321cdf0e10cSrcweir             long        nD = nDY2 - nDX;
322cdf0e10cSrcweir 
323cdf0e10cSrcweir             for( nX = nStartX, nY = nStartY; nX != nEndX; nX += nXInc )
324cdf0e10cSrcweir             {
325cdf0e10cSrcweir                 aEnum.X() = nX;
326cdf0e10cSrcweir                 aEnum.Y() = nY;
327cdf0e10cSrcweir                 rEnumLink.Call( &aEnum );
328cdf0e10cSrcweir 
329cdf0e10cSrcweir 				if( nD < 0L )
330cdf0e10cSrcweir 					nD += nDY2;
331cdf0e10cSrcweir 				else
332cdf0e10cSrcweir 					nD += nDYX, nY += nYInc;
333cdf0e10cSrcweir             }
334cdf0e10cSrcweir         }
335cdf0e10cSrcweir         else
336cdf0e10cSrcweir         {
337cdf0e10cSrcweir             const long  nDYX = ( nDX - nDY ) << 1;
338cdf0e10cSrcweir             const long  nDY2 = nDX << 1;
339cdf0e10cSrcweir             long        nD = nDY2 - nDY;
340cdf0e10cSrcweir 
341cdf0e10cSrcweir             for( nX = nStartX, nY = nStartY; nY != nEndY; nY += nYInc )
342cdf0e10cSrcweir             {
343cdf0e10cSrcweir                 aEnum.X() = nX;
344cdf0e10cSrcweir                 aEnum.Y() = nY;
345cdf0e10cSrcweir                 rEnumLink.Call( &aEnum );
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 				if( nD < 0L )
348cdf0e10cSrcweir 					nD += nDY2;
349cdf0e10cSrcweir 				else
350cdf0e10cSrcweir 					nD += nDYX, nX += nXInc;
351cdf0e10cSrcweir             }
352cdf0e10cSrcweir         }
353cdf0e10cSrcweir 
354cdf0e10cSrcweir         // last point
355cdf0e10cSrcweir 		aEnum.X() = nEndX;
356cdf0e10cSrcweir         aEnum.Y() = nEndY;
357cdf0e10cSrcweir         rEnumLink.Call( &aEnum );
358cdf0e10cSrcweir     }
359cdf0e10cSrcweir }
360