1 2 //---------------------------------------------------------------------------- 3 // XYQ: 2006-01-22 Copied from AGG project. 4 // TODO: This file uses intensive floating point operations, so it's NOT suitable 5 // for platforms like Symbian OS. We need to change to FIX format. 6 //---------------------------------------------------------------------------- 7 //---------------------------------------------------------------------------- 8 // Anti-Grain Geometry - Version 2.3 9 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) 10 // 11 // Permission to copy, use, modify, sell and distribute this software 12 // is granted provided this copyright notice appears in all copies. 13 // This software is provided "as is" without express or implied 14 // warranty, and with no claim as to its suitability for any purpose. 15 // 16 //---------------------------------------------------------------------------- 17 // Contact: mcseem (at) antigrain.com 18 // mcseemagg (at) yahoo.com 19 // http://www.antigrain.com 20 //---------------------------------------------------------------------------- 21 #include "../../../../include/fxcrt/fx_basic.h" 22 #include "agg_curves.h" 23 #include "agg_math.h" 24 namespace agg 25 { 26 const FX_FLOAT curve_collinearity_epsilon = 1e-30f; 27 enum curve_recursion_limit_e { curve_recursion_limit = 16 }; 28 void curve4_div::init(FX_FLOAT x1, FX_FLOAT y1, 29 FX_FLOAT x2, FX_FLOAT y2, 30 FX_FLOAT x3, FX_FLOAT y3, 31 FX_FLOAT x4, FX_FLOAT y4) 32 { 33 m_points.remove_all(); 34 m_distance_tolerance_square = 1.0f / 4; 35 m_distance_tolerance_manhattan = 1.0f * 4; 36 bezier(x1, y1, x2, y2, x3, y3, x4, y4); 37 m_count = 0; 38 } 39 void curve4_div::recursive_bezier(FX_FLOAT x1, FX_FLOAT y1, 40 FX_FLOAT x2, FX_FLOAT y2, 41 FX_FLOAT x3, FX_FLOAT y3, 42 FX_FLOAT x4, FX_FLOAT y4, 43 unsigned level) 44 { 45 if(level > curve_recursion_limit) { 46 return; 47 } 48 FX_FLOAT x12 = (x1 + x2) / 2; 49 FX_FLOAT y12 = (y1 + y2) / 2; 50 FX_FLOAT x23 = (x2 + x3) / 2; 51 FX_FLOAT y23 = (y2 + y3) / 2; 52 FX_FLOAT x34 = (x3 + x4) / 2; 53 FX_FLOAT y34 = (y3 + y4) / 2; 54 FX_FLOAT x123 = (x12 + x23) / 2; 55 FX_FLOAT y123 = (y12 + y23) / 2; 56 FX_FLOAT x234 = (x23 + x34) / 2; 57 FX_FLOAT y234 = (y23 + y34) / 2; 58 FX_FLOAT x1234 = (x123 + x234) / 2; 59 FX_FLOAT y1234 = (y123 + y234) / 2; 60 FX_FLOAT dx = x4 - x1; 61 FX_FLOAT dy = y4 - y1; 62 FX_FLOAT d2 = FXSYS_fabs(FXSYS_Mul(x2 - x4, dy) - FXSYS_Mul(y2 - y4, dx)); 63 FX_FLOAT d3 = FXSYS_fabs(FXSYS_Mul(x3 - x4, dy) - FXSYS_Mul(y3 - y4, dx)); 64 switch((int(d2 > curve_collinearity_epsilon) << 1) + 65 int(d3 > curve_collinearity_epsilon)) { 66 case 0: 67 if(FXSYS_fabs(x1 + x3 - x2 - x2) + 68 FXSYS_fabs(y1 + y3 - y2 - y2) + 69 FXSYS_fabs(x2 + x4 - x3 - x3) + 70 FXSYS_fabs(y2 + y4 - y3 - y3) <= m_distance_tolerance_manhattan) { 71 m_points.add(point_type(x1234, y1234, path_flags_jr)); 72 return; 73 } 74 break; 75 case 1: 76 if(FXSYS_Mul(d3, d3) <= FXSYS_Mul(m_distance_tolerance_square, 77 FXSYS_Mul(dx, dx) + FXSYS_Mul(dy, dy))) { 78 m_points.add(point_type(x23, y23, path_flags_jr)); 79 return; 80 } 81 break; 82 case 2: 83 if(FXSYS_Mul(d2, d2) <= FXSYS_Mul(m_distance_tolerance_square, 84 FXSYS_Mul(dx, dx) + FXSYS_Mul(dy, dy))) { 85 m_points.add(point_type(x23, y23, path_flags_jr)); 86 return; 87 } 88 break; 89 case 3: 90 if(FXSYS_Mul(d2 + d3, d2 + d3) <= FXSYS_Mul(m_distance_tolerance_square, 91 FXSYS_Mul(dx, dx) + FXSYS_Mul(dy, dy))) { 92 m_points.add(point_type(x23, y23, path_flags_jr)); 93 return; 94 } 95 break; 96 } 97 recursive_bezier(x1, y1, x12, y12, x123, y123, x1234, y1234, level + 1); 98 recursive_bezier(x1234, y1234, x234, y234, x34, y34, x4, y4, level + 1); 99 } 100 void curve4_div::bezier(FX_FLOAT x1, FX_FLOAT y1, 101 FX_FLOAT x2, FX_FLOAT y2, 102 FX_FLOAT x3, FX_FLOAT y3, 103 FX_FLOAT x4, FX_FLOAT y4) 104 { 105 m_points.add(point_type(x1, y1)); 106 recursive_bezier(x1, y1, x2, y2, x3, y3, x4, y4, 0); 107 m_points.add(point_type(x4, y4)); 108 } 109 } 110