summaryrefslogtreecommitdiff
path: root/third_party/agg23/agg_math.h
blob: 31e0daf3bb9a3de6bab012cf050c07bc19d99da3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.3
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
//          mcseemagg@yahoo.com
//          http://www.antigrain.com
//----------------------------------------------------------------------------
// Bessel function (besj) was adapted for use in AGG library by Andy Wilk
// Contact: castor.vulgaris@gmail.com
//----------------------------------------------------------------------------
#ifndef AGG_MATH_INCLUDED
#define AGG_MATH_INCLUDED
#include "agg_basics.h"
namespace agg
{
const FX_FLOAT intersection_epsilon = 1.0e-30f;
AGG_INLINE FX_FLOAT calc_point_location(FX_FLOAT x1, FX_FLOAT y1,
                                        FX_FLOAT x2, FX_FLOAT y2,
                                        FX_FLOAT x,  FX_FLOAT y)
{
    return FXSYS_Mul(x - x2, y2 - y1) - FXSYS_Mul(y - y2, x2 - x1);
}
AGG_INLINE FX_FLOAT calc_distance(FX_FLOAT x1, FX_FLOAT y1, FX_FLOAT x2, FX_FLOAT y2)
{
    FX_FLOAT dx = x2 - x1;
    FX_FLOAT dy = y2 - y1;
    return FXSYS_sqrt2(dx, dy);
}
AGG_INLINE FX_FLOAT calc_line_point_distance(FX_FLOAT x1, FX_FLOAT y1,
        FX_FLOAT x2, FX_FLOAT y2,
        FX_FLOAT x,  FX_FLOAT y)
{
    FX_FLOAT dx = x2 - x1;
    FX_FLOAT dy = y2 - y1;
    FX_FLOAT d = FXSYS_sqrt2(dx, dy);
    if(d < intersection_epsilon) {
        return calc_distance(x1, y1, x, y);
    }
    return FXSYS_MulDiv(x - x2, dy, d) - FXSYS_MulDiv(y - y2, dx, d);
}
AGG_INLINE bool calc_intersection(FX_FLOAT ax, FX_FLOAT ay, FX_FLOAT bx, FX_FLOAT by,
                                  FX_FLOAT cx, FX_FLOAT cy, FX_FLOAT dx, FX_FLOAT dy,
                                  FX_FLOAT* x, FX_FLOAT* y)
{
    FX_FLOAT num = FXSYS_Mul(ay - cy, dx - cx) - FXSYS_Mul(ax - cx, dy - cy);
    FX_FLOAT den = FXSYS_Mul(bx - ax, dy - cy) - FXSYS_Mul(by - ay, dx - cx);
    if (FXSYS_fabs(den) < intersection_epsilon) {
        return false;
    }
    *x = ax + FXSYS_MulDiv(bx - ax, num, den);
    *y = ay + FXSYS_MulDiv(by - ay, num, den);
    return true;
}
}
#endif