| 1 | #include "test.h" |
| 2 | |
| 3 | #include <base/vmath.h> |
| 4 | |
| 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | TEST(VMath, IntersectLineCircleMiss) |
| 8 | { |
| 9 | vec2 Start(0, 0); |
| 10 | vec2 End(0, 1); |
| 11 | vec2 Circle(2, 0); |
| 12 | float Radius = 1.0f; |
| 13 | |
| 14 | vec2 aIntersections[2]; |
| 15 | int Size = intersect_line_circle(LineStart: Start, LineEnd: End, CircleCenter: Circle, Radius, aIntersections); |
| 16 | EXPECT_EQ(Size, 0); |
| 17 | } |
| 18 | |
| 19 | TEST(VMath, IntersectLineCircleTangent) |
| 20 | { |
| 21 | vec2 Start(0, 0); |
| 22 | vec2 End(0, 1); |
| 23 | vec2 Circle(1, 0); |
| 24 | float Radius = 1.0f; |
| 25 | |
| 26 | vec2 aIntersections[2]; |
| 27 | int Size = intersect_line_circle(LineStart: Start, LineEnd: End, CircleCenter: Circle, Radius, aIntersections); |
| 28 | EXPECT_EQ(Size, 1); |
| 29 | EXPECT_EQ(aIntersections[0], Start); |
| 30 | } |
| 31 | |
| 32 | TEST(VMath, IntersectLineCircleMultipleIntersections) |
| 33 | { |
| 34 | vec2 Start(0, 0); |
| 35 | vec2 End(0, 1); |
| 36 | vec2 Circle(0, 0); |
| 37 | float Radius = 1.0f; |
| 38 | |
| 39 | vec2 aIntersections[2]; |
| 40 | int Size = intersect_line_circle(LineStart: Start, LineEnd: End, CircleCenter: Circle, Radius, aIntersections); |
| 41 | EXPECT_EQ(Size, 2); |
| 42 | EXPECT_EQ(aIntersections[0], -End); |
| 43 | EXPECT_EQ(aIntersections[1], End); |
| 44 | } |
| 45 | |
| 46 | TEST(VMath, IntersectLineCircleMultipleIntersectionsOutsideOfLineSegment) |
| 47 | { |
| 48 | vec2 Start(1, 0); |
| 49 | vec2 End(1, 1); |
| 50 | vec2 Circle(1, 10); |
| 51 | float Radius = 1.0f; |
| 52 | |
| 53 | vec2 aIntersections[2]; |
| 54 | int Size = intersect_line_circle(LineStart: Start, LineEnd: End, CircleCenter: Circle, Radius, aIntersections); |
| 55 | EXPECT_EQ(Size, 2); |
| 56 | EXPECT_EQ(aIntersections[0], vec2(1, 9)); |
| 57 | EXPECT_EQ(aIntersections[1], vec2(1, 11)); |
| 58 | |
| 59 | vec2 Circle2(1, -10); |
| 60 | int Size2 = intersect_line_circle(LineStart: Start, LineEnd: End, CircleCenter: Circle2, Radius, aIntersections); |
| 61 | EXPECT_EQ(Size2, 2); |
| 62 | EXPECT_EQ(aIntersections[0], vec2(1, -11)); |
| 63 | EXPECT_EQ(aIntersections[1], vec2(1, -9)); |
| 64 | } |
| 65 | |