summaryrefslogtreecommitdiffstats
path: root/src/math/vector.php
blob: c34b34d8d1950c6f8508c9de136af4d79b2d8f79 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
 * File containing the ezcGraphVector class
 *
 * @package Graph
 * @version //autogentag//
 * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved.
 * @license http://ez.no/licenses/new_bsd New BSD License
 * @access private
 */
/**
 * Represents two dimensional vectors
 *
 * This class is internally used to represent vectors for geometric calculation
 * in the two dimensional cartesian coordinate system.
 *
 * Vectors are an extension of the basic coordinate class, and add methods to
 * calculate the length of a vector, perform various operations on angles, like
 * rotations and the calculation of angles between two vectors.
 *
 * @version //autogentag//
 * @package Graph
 * @access private
 */
class ezcGraphVector extends ezcGraphCoordinate
{
    /**
     * Rotates vector to the left by 90 degrees
     * 
     * @return void
     */
    public function rotateCounterClockwise()
    {
        $tmp = $this->x;
        $this->x = $this->y;
        $this->y = -$tmp;

        return $this;
    }

    /**
     * Rotates vector to the right by 90 degrees
     * 
     * @return void
     */
    public function rotateClockwise()
    {
        $tmp = $this->x;
        $this->x = -$this->y;
        $this->y = $tmp;

        return $this;
    }
    
    /**
     * Unifies vector length to 1
     * 
     * @return void
     */
    public function unify()
    {
        $length = $this->length();
        if ( $length == 0 )
        {
            return $this;
        }

        $this->x /= $length;
        $this->y /= $length;

        return $this;
    }

    /**
     * Returns length of vector
     * 
     * @return float
     */
    public function length()
    {
        return sqrt(
            pow( $this->x, 2 ) +
            pow( $this->y, 2 )
        );
    }

    /**
     * Multiplies vector with a scalar
     * 
     * @param float $value 
     * @return void
     */
    public function scalar( $value )
    {
        $this->x *= $value;
        $this->y *= $value;

        return $this;
    }

    /**
     * Calculates scalar product of two vectors
     * 
     * @param ezcGraphCoordinate $vector 
     * @return void
     */
    public function mul( ezcGraphCoordinate $vector )
    {
        return $this->x * $vector->x + $this->y * $vector->y;
    }

    /**
     * Returns the angle between two vectors in radian
     * 
     * @param ezcGraphCoordinate $vector 
     * @return float
     */
    public function angle( ezcGraphCoordinate $vector )
    {
        if ( !$vector instanceof ezcGraphVector )
        {
            // Ensure beeing a vector for calling length()
            $vector = ezcGraphVector::fromCoordinate( $vector );
        }
        
        $factor = $this->length() * $vector->length();

        if ( $factor == 0 )
        {
            return false;
        }
        else
        {
            return acos( $this->mul( $vector ) / $factor );
        }
    }

    /**
     * Adds a vector to another vector
     * 
     * @param ezcGraphCoordinate $vector 
     * @return void
     */
    public function add( ezcGraphCoordinate $vector )
    {
        $this->x += $vector->x;
        $this->y += $vector->y;

        return $this;
    }

    /**
     * Subtracts a vector from another vector
     * 
     * @param ezcGraphCoordinate $vector 
     * @return void
     */
    public function sub( ezcGraphCoordinate $vector )
    {
        $this->x -= $vector->x;
        $this->y -= $vector->y;

        return $this;
    }

    /**
     * Creates a vector from a coordinate object
     * 
     * @param ezcGraphCoordinate $coordinate 
     * @return ezcGraphVector
     */
    public static function fromCoordinate( ezcGraphCoordinate $coordinate )
    {
        return new ezcGraphVector( $coordinate->x, $coordinate->y );
    }

    /**
     * Transform vector using transformation matrix
     * 
     * @param ezcGraphTransformation $transformation 
     * @return ezcGraphVector
     */
    public function transform( ezcGraphTransformation $transformation )
    {
        $result = $transformation->transformCoordinate( $this );

        $this->x = $result->x;
        $this->y = $result->y;

        return $this;
    }
}

?>
OpenPOWER on IntegriCloud