aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/Intersection/LineParameters.h
blob: 188962263ee5f35a72411ec5ac08e59df56942fd (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "DataTypes.h"

// Sources
// computer-aided design - volume 22 number 9 november 1990 pp 538 - 549
// online at http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf

class LineParameters {
public:
    void cubicEndPoints(const Cubic& pts) {
        a = pts[0].y - pts[3].y;
        b = pts[3].x - pts[0].x;
        c = pts[0].x * pts[3].y - pts[3].x * pts[0].y;
    }
    
    void cubicEndPoints(const Cubic& pts, int s, int e) {
        a = pts[s].y - pts[e].y;
        b = pts[e].x - pts[s].x;
        c = pts[s].x * pts[e].y - pts[e].x * pts[s].y;
    }
    
    void lineEndPoints(const _Line& pts) {
        a = pts[0].y - pts[1].y;
        b = pts[1].x - pts[0].x;
        c = pts[0].x * pts[1].y - pts[1].x * pts[0].y;
    }
    
    void quadEndPoints(const Quadratic& pts) {
        a = pts[0].y - pts[2].y;
        b = pts[2].x - pts[0].x;
        c = pts[0].x * pts[2].y - pts[2].x * pts[0].y;
    }

    void quadEndPoints(const Quadratic& pts, int s, int e) {
        a = pts[s].y - pts[e].y;
        b = pts[e].x - pts[s].x;
        c = pts[s].x * pts[e].y - pts[e].x * pts[s].y;
    }

    double normalSquared() {
        return a * a + b * b;
    }

    bool normalize() {
        double normal = sqrt(normalSquared());
        if (normal < SquaredEpsilon) {
            a = b = c = 0;
            return false;
        }
        double reciprocal = 1 / normal;
        a *= reciprocal;
        b *= reciprocal;
        c *= reciprocal;
        return true;
    }
    
    void cubicDistanceY(const Cubic& pts, Cubic& distance) {
        double oneThird = 1 / 3.0;
        for (int index = 0; index < 4; ++index) {
            distance[index].x = index * oneThird;
            distance[index].y = a * pts[index].x + b * pts[index].y + c;
        }
    }

    void quadDistanceY(const Quadratic& pts, Quadratic& distance) {
        double oneHalf = 1 / 2.0;
        for (int index = 0; index < 3; ++index) {
            distance[index].x = index * oneHalf;
            distance[index].y = a * pts[index].x + b * pts[index].y + c;
        }
    }

    void controlPtDistance(const Cubic& pts, double distance[2]) {
        for (int index = 0; index < 2; ++index) {
            distance[index] = a * pts[index + 1].x + b * pts[index + 1].y + c;
        }
    }
    
    void controlPtDistance(const Cubic& pts, int i, int j, double distance[2]) {
        distance[0] = a * pts[i].x + b * pts[i].y + c;
        distance[1] = a * pts[j].x + b * pts[j].y + c;
    }
    
    double controlPtDistance(const Quadratic& pts) {
        return a * pts[1].x + b * pts[1].y + c;
    }
    
    double pointDistance(const _Point& pt) {
        return a * pt.x + b * pt.y + c;
    }
private:
    double a;
    double b;
    double c;
};