Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: experimental/flocking_geese/nacl_app/vector2.h

Issue 10928195: First round of dead file removal (Closed) Base URL: https://github.com/samclegg/nativeclient-sdk.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « experimental/flocking_geese/nacl_app/sprite.cc ('k') | experimental/flocking_geese/scons » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef VECTOR2_H_
6 #define VECTOR2_H_
7
8 #include <stdlib.h>
9 #include <cmath>
10 #include <limits>
11
12 // A small class that encapsulates a 2D vector. PR\rovides a few simple
13 // operations.
14
15 class Vector2 {
16 public:
17 Vector2() : x_(0.0), y_(0.0) {}
18 Vector2(double x, double y) : x_(x), y_(y) {}
19 ~Vector2() {}
20
21 // Create a random vector of unit length. Works in-place.
22 static Vector2 RandomUnit() {
23 double angle = static_cast<double>(rand()) * M_PI * 2.0 / RAND_MAX;
24 Vector2 rand_vec(cos(angle), sin(angle));
25 return rand_vec;
26 }
27
28 // Create a new vector that represents a - b.
29 static Vector2 Difference(const Vector2& a, const Vector2& b) {
30 Vector2 diff(a.x() - b.x(), a.y() - b.y());
31 return diff;
32 }
33
34 // The magnitude of this vector.
35 double Magnitude() const {
36 return sqrt(x_ * x_ + y_ * y_);
37 }
38
39 // Add |vec| to this vector. Works in-place.
40 void Add(const Vector2& vec) {
41 x_ += vec.x();
42 y_ += vec.y();
43 }
44
45 // Normalize this vector in-place. If the vector is degenerate (size 0)
46 // then do nothing.
47 void Normalize() {
48 double mag = Magnitude();
49 if (fabs(mag) < std::numeric_limits<double>::epsilon())
50 return;
51 Scale(1.0 / mag);
52 }
53
54 // Scale the vector in-place by |scale|.
55 void Scale(double scale) {
56 x_ *= scale;
57 y_ *= scale;
58 }
59
60 // Clamp a vector to a maximum magnitude. Works on the vector in-place.
61 // @param max_mag The maximum magnitude of the vector.
62 void Clamp(double max_mag) {
63 double mag = Magnitude();
64 if (mag > max_mag) {
65 Scale(max_mag / mag); // Does Normalize() followed by Scale(max_mag).
66 }
67 }
68
69 // Compute the "heading" of a vector - this is the angle in radians between
70 // the vector and the x-axis.
71 // @return {!number} The "heading" angle in radians.
72 double Heading() const {
73 double angle = atan2(y_, x_);
74 return angle;
75 }
76
77 // Accessors and mutators for the coordinate values.
78 double x() const { return x_; }
79 void set_x(double x) { x_ = x; }
80
81 double y() const { return y_; }
82 void set_y(double y) { y_ = y; }
83
84 private:
85 double x_;
86 double y_;
87 };
88
89 #endif // VECTOR2_H_
OLDNEW
« no previous file with comments | « experimental/flocking_geese/nacl_app/sprite.cc ('k') | experimental/flocking_geese/scons » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698