-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Line2D.containsPoint, Line2D.covers, Improve Line2D.equals, Line2…
…D.overlaps, add unit tests
- Loading branch information
Showing
7 changed files
with
489 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { Vector2 } from "three"; | ||
import { Line2D } from "../Line2D"; | ||
import { Vec2 } from "../Vec2"; | ||
|
||
describe("containsPoint", () => { | ||
|
||
it("should return true if the point is on the line section", () => { | ||
// Arrange | ||
const line = new Line2D(new Vec2(0, 0), new Vec2(10, 10)); | ||
const point = new Vector2(5, 5); | ||
const tolerance = 0; | ||
|
||
// Act | ||
const contains = line.containsPoint(point, tolerance); | ||
|
||
// Assert | ||
expect(contains).toBe(true); | ||
}); | ||
|
||
it("should return true if the point is very close to the line section within the tolerance", () => { | ||
// Arrange | ||
const line = new Line2D(new Vec2(0, 0), new Vec2(10, 10)); | ||
const point = new Vector2(5.5, 5.5); | ||
const tolerance = 1; | ||
|
||
// Act | ||
const contains = line.containsPoint(point, tolerance); | ||
|
||
// Assert | ||
expect(contains).toBe(true); | ||
}); | ||
|
||
it("should return true if the point is exactly on the start point of the line section", () => { | ||
// Arrange | ||
const line = new Line2D(new Vec2(0, 0), new Vec2(10, 10)); | ||
const point = new Vector2(0, 0); | ||
const tolerance = 0; | ||
|
||
// Act | ||
const contains = line.containsPoint(point, tolerance); | ||
|
||
// Assert | ||
expect(contains).toBe(true); | ||
}); | ||
|
||
it("should return true if the point is exactly on the end point of the line section", () => { | ||
// Arrange | ||
const line = new Line2D(new Vec2(0, 0), new Vec2(10, 10)); | ||
const point = new Vector2(10, 10); | ||
const tolerance = 0; | ||
|
||
// Act | ||
const contains = line.containsPoint(point, tolerance); | ||
|
||
// Assert | ||
expect(contains).toBe(true); | ||
}); | ||
|
||
it("should return false when the point is not on the line section and not within the tolerance", () => { | ||
// Arrange | ||
const line = new Line2D(new Vec2(0, 0), new Vec2(10, 0)); | ||
const point = new Vector2(5, 1.1); | ||
const tolerance = 1; | ||
|
||
// Act | ||
const result = line.containsPoint(point, tolerance); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("should return true when the point is on the line section", () => { | ||
// Arrange | ||
const line = Line2D.fromCoordinates(0, 0, 10, 0); | ||
const point = new Vector2(5, 0); | ||
|
||
// Act | ||
const result = line.containsPoint(point); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return true when the point is on the line section", () => { | ||
// Arrange | ||
const line = Line2D.fromCoordinates(0, 0, 0, 10); | ||
const point = new Vector2(0, 5); | ||
const tolerance = 0; | ||
|
||
// Act | ||
const result = line.containsPoint(point, tolerance); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return true when the point is on the line section", () => { | ||
// Arrange | ||
const line = Line2D.fromCoordinates(0, 0, 2, 2); | ||
const point = new Vector2(1, 1); | ||
const tolerance = 0; | ||
|
||
// Act | ||
const result = line.containsPoint(point, tolerance); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Line2D } from "../Line2D"; | ||
import { Vec2 } from "../Vec2"; | ||
|
||
describe("covers", () => { | ||
|
||
it("should return true when the other line is parallel and both endpoints are contained within the current line", () => { | ||
// Arrange | ||
const line1 = new Line2D(new Vec2(0, 0), new Vec2(10, 10)); | ||
const line2 = new Line2D(new Vec2(2, 2), new Vec2(8, 8)); | ||
|
||
// Act | ||
const result = line1.covers(line2); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return true when the other line is parallel and both endpoints are contained within the current line within tolerance", () => { | ||
// Arrange | ||
const line1 = new Line2D(new Vec2(0, 0), new Vec2(10, 0)); | ||
const line2 = new Line2D(new Vec2(2, 1), new Vec2(8, 1)); | ||
const tolerance = 1; | ||
|
||
// Act | ||
const result = line1.covers(line2, tolerance); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return false when the other line is not within the tolerance and both endpoints are contained within the current line within tolerance", () => { | ||
// Arrange | ||
const line1 = new Line2D(new Vec2(0, 0), new Vec2(10, 0)); | ||
const line2 = new Line2D(new Vec2(2, 2), new Vec2(8, 1)); | ||
const tolerance = 10; | ||
|
||
// Act | ||
const result = line1.covers(line2, tolerance); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("should return true when the other line direction is within tolerance and both endpoints are contained within the current line within tolerance", () => { | ||
// Arrange | ||
const line1 = new Line2D(new Vec2(0, 0), new Vec2(10, 0)); | ||
const line2 = new Line2D(new Vec2(0, 0), new Vec2(5, 5)); | ||
const tolerance = 10; | ||
|
||
// Act | ||
const result = line1.covers(line2, tolerance, Math.PI / 4); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return true when the other line is identical to the current line", () => { | ||
// Arrange | ||
const line1 = new Line2D(new Vec2(0, 0), new Vec2(10, 10)); | ||
const line2 = new Line2D(new Vec2(0, 0), new Vec2(10, 10)); | ||
|
||
// Act | ||
const result = line1.covers(line2); | ||
|
||
// Assert | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return false when the other line is parallel but one endpoint is not contained within the current line", () => { | ||
// Arrange | ||
const line1 = new Line2D(new Vec2(0, 0), new Vec2(10, 10)); | ||
const line2 = new Line2D(new Vec2(2, 2), new Vec2(12, 12)); | ||
|
||
// Act | ||
const result = line1.covers(line2); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("should return false when the other line is not parallel to the current line", () => { | ||
// Arrange | ||
const line1 = new Line2D(new Vec2(0, 0), new Vec2(10, 10)); | ||
const line2 = new Line2D(new Vec2(0, 0), new Vec2(5, 15)); | ||
const tolerance1 = 20; | ||
const parallelTolerance = 0; | ||
|
||
// Act | ||
const result = line1.covers(line2, tolerance1, parallelTolerance); | ||
|
||
// Assert | ||
expect(result).toBe(false); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Vector2 } from "three"; | ||
import { Line2D } from "../Line2D"; | ||
import { Vec2 } from "../Vec2"; | ||
|
||
describe("distanceToPoint", () => { | ||
|
||
it("should return the correct distance when the point is on the line segment", () => { | ||
// Arrange | ||
const line = new Line2D(new Vec2(0, 0), new Vec2(0, 5)); | ||
const point = new Vector2(0, 3); | ||
|
||
// Act | ||
const distance = line.distanceToPoint(point); | ||
|
||
// Assert | ||
expect(distance).toBe(0); | ||
}); | ||
|
||
it("should return the correct distance when the point is on the line but not on the line segment when measuring to line segment", () => { | ||
// Arrange | ||
const line = new Line2D(new Vec2(0, 0), new Vec2(0, 5)); | ||
const point = new Vector2(0, 6); | ||
|
||
// Act | ||
const distance = line.distanceToPoint(point); | ||
|
||
// Assert | ||
expect(distance).toBeCloseTo(1); | ||
}); | ||
|
||
it("should return the correct distance when the point is on the line but not on the line segment when measuring to line", () => { | ||
// Arrange | ||
const line = new Line2D(new Vec2(0, 0), new Vec2(0, 5)); | ||
const point = new Vector2(0, 6); | ||
|
||
// Act | ||
const distance = line.distanceToPoint(point, false); | ||
|
||
// Assert | ||
expect(distance).toBeCloseTo(0); | ||
}); | ||
|
||
it("should return 0 when the point is on the line", () => { | ||
// Arrange | ||
const line = new Line2D(new Vec2(0, 0), new Vec2(2, 2)); | ||
const point = new Vector2(1, 1); | ||
|
||
// Act | ||
const distance = line.distanceToPoint(point); | ||
|
||
// Assert | ||
expect(distance).toBe(0); | ||
}); | ||
|
||
it("should return the correct distance when the point is not on the line", () => { | ||
// Arrange | ||
const line = new Line2D(new Vec2(0, 0), new Vec2(2, 2)); | ||
const point = new Vector2(3, 1); | ||
|
||
// Act | ||
const distance = line.distanceToPoint(point); | ||
|
||
// Assert | ||
expect(distance).toBeCloseTo(1.414, 3); | ||
}); | ||
|
||
}); |
Oops, something went wrong.