scbutil.c

Go to the documentation of this file.
00001 /*
00002  * File Name: scbutil.c
00003  */
00004 
00005 /*
00006  * This file is part of liberscribble.
00007  *
00008  * liberscribble is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * liberscribble is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 
00028 #define _GNU_SOURCE
00029 
00030 //----------------------------------------------------------------------------
00031 // Include Files
00032 //----------------------------------------------------------------------------
00033 
00034 // system include files, between < >
00035 #include <math.h>
00036 #include <glib.h>
00037 
00038 // ereader include files, between < >
00039 
00040 // local include files, between " "
00041 #include "scblog.h"
00042 #include "scbutil.h"
00043 
00044 
00045 //----------------------------------------------------------------------------
00046 // Type Declarations
00047 //----------------------------------------------------------------------------
00048 
00049 
00050 //----------------------------------------------------------------------------
00051 // Global Constants
00052 //----------------------------------------------------------------------------
00053 
00054 
00055 //----------------------------------------------------------------------------
00056 // Static Variables
00057 //----------------------------------------------------------------------------
00058 
00059 
00060 //============================================================================
00061 // Local Function Definitions
00062 //============================================================================
00063 
00064 static int CCW(const ScbPointPtr p0, const ScbPointPtr p1, const ScbPointPtr p2);
00065 
00066 
00067 //============================================================================
00068 // Functions Implementation
00069 //============================================================================
00070 
00071 gboolean erscribble_is_rect_intersect(const ScbRectPtr r1, 
00072                                       const ScbRectPtr r2)
00073 {
00074     // check two rectangles is intersect or not, 
00075     // these two rectangles should be normalized.
00076     return ! ( r2->left > r1->right 
00077         || r2->right < r1->left 
00078         || r2->top > r1->bottom 
00079         || r2->bottom < r1->top 
00080         );
00081 }
00082 
00083 
00084 gboolean erscribble_is_lines_intersect(const ScbPointPtr p1, 
00085                                        const ScbPointPtr p2,
00086                                        const ScbPointPtr p3, 
00087                                        const ScbPointPtr p4)
00088 {
00089     gboolean bRet = ((( CCW(p1, p2, p3) * CCW(p1, p2, p4)) <= 0)
00090         && (( CCW(p3, p4, p1) * CCW(p3, p4, p2)  <= 0) )) ;
00091 
00092    
00093     return bRet;
00094 }
00095 
00096 
00097 /*************************************************************************
00098 * FUNCTION:   CCW (CounterClockWise)
00099 *
00100 * PURPOSE
00101 * Determines, given three points, if when travelling from the first to
00102 * the second to the third, we travel in a counterclockwise direction.
00103 *
00104 * RETURN VALUE
00105 * (int) 1 if the movement is in a counterclockwise direction, -1 if
00106 * not.
00107 *************************************************************************/ 
00108 static int CCW(const ScbPointPtr p0, const ScbPointPtr p1, const ScbPointPtr p2)
00109 {
00110     long dx1, dx2 ;
00111     long dy1, dy2 ;
00112 
00113     dx1 = p1->x - p0->x ; dx2 = p2->x - p0->x ;
00114     dy1 = p1->y - p0->y ; dy2 = p2->y - p0->y ;
00115     /* This is basically a slope comparison: we don't do divisions because
00116     * of divide by zero possibilities with pure horizontal and pure
00117     * vertical lines.
00118     */ 
00119     return ((dx1 * dy2 > dy1 * dx2) ? 1 : -1) ;
00120 }
Generated by  doxygen 1.6.2-20100208