00001 /* 00002 * This file is part of liberscribble. 00003 * 00004 * liberscribble is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * liberscribble is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00023 #include "scbutil.h" 00024 #include <math.h> 00025 #include <glib.h> 00026 #include <scblog.h> 00027 //#define MIN(a,b) (((a)<(b))?(a):(b)) 00028 //#define MAX(a,b) (((a)>(b))?(a):(b)) 00029 00030 /************************************************************************* 00031 00032 * FUNCTION: CCW (CounterClockWise) 00033 * 00034 * PURPOSE 00035 * Determines, given three points, if when travelling from the first to 00036 * the second to the third, we travel in a counterclockwise direction. 00037 * 00038 * RETURN VALUE 00039 * (int) 1 if the movement is in a counterclockwise direction, -1 if 00040 * not. 00041 *************************************************************************/ 00042 int CCW(const ScbPointPtr p0, const ScbPointPtr p1, const ScbPointPtr p2) 00043 { 00044 long dx1, dx2 ; 00045 long dy1, dy2 ; 00046 00047 dx1 = p1->x - p0->x ; dx2 = p2->x - p0->x ; 00048 dy1 = p1->y - p0->y ; dy2 = p2->y - p0->y ; 00049 00050 /* This is basically a slope comparison: we don't do divisions because 00051 00052 * of divide by zero possibilities with pure horizontal and pure 00053 * vertical lines. 00054 */ 00055 return ((dx1 * dy2 > dy1 * dx2) ? 1 : -1) ; 00056 } 00057 00058 00059 /************************************************************************* 00060 * FUNCTION: Intersect 00061 * 00062 * PURPOSE 00063 * Given two line segments, determine if they intersect. 00064 * 00065 * RETURN VALUE 00066 * TRUE if they intersect, FALSE if not. 00067 *************************************************************************/ 00068 gboolean Intersect(const ScbPointPtr p1, const ScbPointPtr p2, const ScbPointPtr p3, const ScbPointPtr p4) 00069 { 00070 return ((( CCW(p1, p2, p3) * CCW(p1, p2, p4)) <= 0) 00071 && (( CCW(p3, p4, p1) * CCW(p3, p4, p2) <= 0) )) ; 00072 } 00073 00074 //------------------------------------------------------------------------ 00075 // check two rectangles is intersect or not 00076 // these two rectangles should be normalized. 00077 // We do not check them here. 00078 gboolean scb_is_rect_intersect(const ScbRectPtr r1, 00079 const ScbRectPtr r2) 00080 { 00081 return ! ( r2->left > r1->right 00082 || r2->right < r1->left 00083 || r2->top > r1->bottom 00084 || r2->bottom < r1->top 00085 ); 00086 } 00087 00088 //------------------------------------------------------------------------ 00089 gboolean scb_is_lines_intersect(const ScbPointPtr p1, 00090 const ScbPointPtr p2, 00091 const ScbPointPtr p3, 00092 const ScbPointPtr p4) 00093 { 00094 gboolean bRet = ((( CCW(p1, p2, p3) * CCW(p1, p2, p4)) <= 0) 00095 && (( CCW(p3, p4, p1) * CCW(p3, p4, p2) <= 0) )) ; 00096 00097 00098 return bRet; 00099 }
1.5.6