blob: 6d5e39ac3e4afd0dc91049b6d42664996350ec78 [file] [log] [blame]
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001
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"
22namespace agg
23{
Dan Sinclair05df0752017-03-14 14:43:42 -040024const float intersection_epsilon = 1.0e-30f;
25AGG_INLINE float calc_point_location(float x1, float y1,
26 float x2, float y2,
27 float x, float y)
John Abd-El-Malek5110c472014-05-17 22:33:34 -070028{
Dan Sinclair2c8fad42016-02-23 15:23:29 -050029 return ((x - x2) * (y2 - y1)) - ((y - y2) * (x2 - x1));
John Abd-El-Malek5110c472014-05-17 22:33:34 -070030}
Dan Sinclair05df0752017-03-14 14:43:42 -040031AGG_INLINE float calc_distance(float x1, float y1, float x2, float y2)
John Abd-El-Malek5110c472014-05-17 22:33:34 -070032{
Dan Sinclair05df0752017-03-14 14:43:42 -040033 float dx = x2 - x1;
34 float dy = y2 - y1;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070035 return FXSYS_sqrt2(dx, dy);
36}
Dan Sinclair05df0752017-03-14 14:43:42 -040037AGG_INLINE float calc_line_point_distance(float x1, float y1,
38 float x2, float y2,
39 float x, float y)
John Abd-El-Malek5110c472014-05-17 22:33:34 -070040{
Dan Sinclair05df0752017-03-14 14:43:42 -040041 float dx = x2 - x1;
42 float dy = y2 - y1;
43 float d = FXSYS_sqrt2(dx, dy);
John Abd-El-Malek5110c472014-05-17 22:33:34 -070044 if(d < intersection_epsilon) {
45 return calc_distance(x1, y1, x, y);
46 }
Dan Sinclair435604d2016-02-23 16:31:44 -050047 return ((x - x2) * dy / d) - ((y - y2) * dx / d);
John Abd-El-Malek5110c472014-05-17 22:33:34 -070048}
Dan Sinclair05df0752017-03-14 14:43:42 -040049AGG_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-Malek5110c472014-05-17 22:33:34 -070052{
Dan Sinclair05df0752017-03-14 14:43:42 -040053 float num = ((ay - cy) * (dx - cx)) - ((ax - cx) * (dy - cy));
54 float den = ((bx - ax) * (dy - cy)) - ((by - ay) * (dx - cx));
Dan Sinclair669a4182017-04-03 14:51:45 -040055 if (fabs(den) < intersection_epsilon) {
56 return false;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070057 }
Dan Sinclair435604d2016-02-23 16:31:44 -050058 *x = ax + ((bx - ax) * num / den);
59 *y = ay + ((by - ay) * num / den);
John Abd-El-Malek5110c472014-05-17 22:33:34 -070060 return true;
61}
62}
63#endif