Skip to content

Commit 4949f0c

Browse files
authored
Merge pull request #20 from ichiro-its/19-add-support-for-dot-product-and-cross-product
Add Supoort for Dot Product and Cross Product
2 parents 7eee244 + b3317b4 commit 4949f0c

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

include/keisan/point_2.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ struct Point2
4949

5050
static double distance_between(Point2 & point_a, Point2 & point_b);
5151
static double angle_between(Point2 & point_a, Point2 & point_b);
52+
static double dot_product(Point2 & point_a, Point2 & point_b);
53+
static double cross_product(Point2 & point_a, Point2 & point_b);
5254

5355
double magnitude();
5456
double direction();

include/keisan/point_3.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ struct Point3
4949

5050
static double distance_between(Point3 & point_a, Point3 & point_b);
5151
static double angle_between(Point3 & point_a, Point3 & point_b);
52+
static double dot_product(Point3 & point_a, Point3 & point_b);
53+
static double cross_product(Point3 & point_a, Point3 & point_b);
5254

5355
double magnitude();
5456
double direction();

src/point_2.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,20 @@ double Point2::distance_between(Point2 & point_a, Point2 & point_b)
140140

141141
double Point2::angle_between(Point2 & point_a, Point2 & point_b)
142142
{
143-
double dot_product = point_a.x * point_b.x + point_a.y * point_b.y;
143+
double dot = dot_product(point_a, point_b);
144144
double mag = point_a.magnitude() * point_b.magnitude();
145145

146-
return wrap_rad(acos(dot_product / mag));
146+
return wrap_rad(acos(dot / mag));
147+
}
148+
149+
double Point2::dot_product(Point2 & point_a, Point2 & point_b)
150+
{
151+
return point_a.x * point_b.x + point_a.y * point_b.y;
152+
}
153+
154+
double Point2::cross_product(Point2 & point_a, Point2 & point_b)
155+
{
156+
return point_a.magnitude() * point_b.magnitude() * sin(angle_between(point_a, point_b));
147157
}
148158

149159
double Point2::magnitude()

src/point_3.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,20 @@ double Point3::distance_between(Point3 & point_a, Point3 & point_b)
151151

152152
double Point3::angle_between(Point3 & point_a, Point3 & point_b)
153153
{
154-
double dot_product = point_a.x * point_b.x + point_a.y * point_b.y + point_a.z * point_b.z;
154+
double dot = dot_product(point_a, point_b);
155155
double mag = point_a.magnitude() * point_b.magnitude();
156156

157-
return wrap_rad(acos(dot_product / mag));
157+
return wrap_rad(acos(dot / mag));
158+
}
159+
160+
double Point3::dot_product(Point3 & point_a, Point3 & point_b)
161+
{
162+
return point_a.x * point_b.x + point_a.y * point_b.y + point_a.z * point_b.z;
163+
}
164+
165+
double Point3::cross_product(Point3 & point_a, Point3 & point_b)
166+
{
167+
return point_a.magnitude() * point_b.magnitude() * sin(angle_between(point_a, point_b));
158168
}
159169

160170
double Point3::magnitude()

test/point_2_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,17 @@ TEST(Point2Test, AngleBetween)
133133

134134
ASSERT_DOUBLE_EQ(keisan::Point2::angle_between(point_a, point_b), keisan::deg_to_rad(90.0));
135135
}
136+
137+
TEST(Point2Test, DotProduct)
138+
{
139+
auto point_a = keisan::Point2(4.0, 3.0);
140+
auto point_b = keisan::Point2(3.0, 4.0);
141+
ASSERT_DOUBLE_EQ(keisan::Point2::dot_product(point_a, point_b), 24.0);
142+
}
143+
144+
TEST(Point2Test, CrossProduct)
145+
{
146+
auto point_a = keisan::Point2(4.0, 3.0);
147+
auto point_b = keisan::Point2(-3.0, 4.0);
148+
ASSERT_DOUBLE_EQ(keisan::Point2::cross_product(point_a, point_b), 25.0);
149+
}

test/point_3_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,17 @@ TEST(Point3Test, AngleBetween)
133133

134134
ASSERT_DOUBLE_EQ(keisan::Point3::angle_between(point_a, point_b), keisan::deg_to_rad(90.0));
135135
}
136+
137+
TEST(Point3Test, DotProduct)
138+
{
139+
auto point_a = keisan::Point3(4.0, 3.0, 2.0);
140+
auto point_b = keisan::Point3(3.0, 4.0, 5.0);
141+
ASSERT_DOUBLE_EQ(keisan::Point3::dot_product(point_a, point_b), 34.0);
142+
}
143+
144+
TEST(Point3Test, CrossProduct)
145+
{
146+
auto point_a = keisan::Point3(3.0, 6.0, -3.0);
147+
auto point_b = keisan::Point3(0.0, 2.0, 4.0);
148+
ASSERT_DOUBLE_EQ(keisan::Point3::cross_product(point_a, point_b), sqrt(54.0) * sqrt(20.0));
149+
}

0 commit comments

Comments
 (0)