John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1 | |
| 2 | //---------------------------------------------------------------------------- |
| 3 | // Anti-Grain Geometry - Version 2.3 |
| 4 | // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) |
| 5 | // |
| 6 | // Permission to copy, use, modify, sell and distribute this software |
| 7 | // is granted provided this copyright notice appears in all copies. |
| 8 | // This software is provided "as is" without express or implied |
| 9 | // warranty, and with no claim as to its suitability for any purpose. |
| 10 | // |
| 11 | //---------------------------------------------------------------------------- |
| 12 | // Contact: mcseem@antigrain.com |
| 13 | // mcseemagg@yahoo.com |
| 14 | // http://www.antigrain.com |
| 15 | //---------------------------------------------------------------------------- |
| 16 | // Bessel function (besj) was adapted for use in AGG library by Andy Wilk |
| 17 | // Contact: castor.vulgaris@gmail.com |
| 18 | //---------------------------------------------------------------------------- |
| 19 | #ifndef AGG_MATH_INCLUDED |
| 20 | #define AGG_MATH_INCLUDED |
| 21 | #include "agg_basics.h" |
| 22 | namespace agg |
| 23 | { |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 24 | const float intersection_epsilon = 1.0e-30f; |
| 25 | AGG_INLINE float calc_point_location(float x1, float y1, |
| 26 | float x2, float y2, |
| 27 | float x, float y) |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 28 | { |
Dan Sinclair | 2c8fad4 | 2016-02-23 15:23:29 -0500 | [diff] [blame] | 29 | return ((x - x2) * (y2 - y1)) - ((y - y2) * (x2 - x1)); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 30 | } |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 31 | AGG_INLINE float calc_distance(float x1, float y1, float x2, float y2) |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 32 | { |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 33 | float dx = x2 - x1; |
| 34 | float dy = y2 - y1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 35 | return FXSYS_sqrt2(dx, dy); |
| 36 | } |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 37 | AGG_INLINE float calc_line_point_distance(float x1, float y1, |
| 38 | float x2, float y2, |
| 39 | float x, float y) |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 40 | { |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 41 | float dx = x2 - x1; |
| 42 | float dy = y2 - y1; |
| 43 | float d = FXSYS_sqrt2(dx, dy); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 44 | if(d < intersection_epsilon) { |
| 45 | return calc_distance(x1, y1, x, y); |
| 46 | } |
Dan Sinclair | 435604d | 2016-02-23 16:31:44 -0500 | [diff] [blame] | 47 | return ((x - x2) * dy / d) - ((y - y2) * dx / d); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 48 | } |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 49 | AGG_INLINE bool calc_intersection(float ax, float ay, float bx, float by, |
| 50 | float cx, float cy, float dx, float dy, |
| 51 | float* x, float* y) |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 52 | { |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 53 | float num = ((ay - cy) * (dx - cx)) - ((ax - cx) * (dy - cy)); |
| 54 | float den = ((bx - ax) * (dy - cy)) - ((by - ay) * (dx - cx)); |
Dan Sinclair | 669a418 | 2017-04-03 14:51:45 -0400 | [diff] [blame^] | 55 | if (fabs(den) < intersection_epsilon) { |
| 56 | return false; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 57 | } |
Dan Sinclair | 435604d | 2016-02-23 16:31:44 -0500 | [diff] [blame] | 58 | *x = ax + ((bx - ax) * num / den); |
| 59 | *y = ay + ((by - ay) * num / den); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | } |
| 63 | #endif |