Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ export class Segment extends Line {

/** Contracts (or expands) a line by a specific ratio. */
contract(x: number) {
return new Segment(this.at(x), this.at(1 - x));
return this.part(x, 1 - x);
}

/** Get part of a segment from a start to end ratio. */
part(a: number, b: number) {
return new Segment(this.at(a), this.at(b));
}

equals(other: Segment, tolerance?: number, oriented = false) {
Expand Down
12 changes: 11 additions & 1 deletion test/line-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import {nearlyEquals} from '@mathigon/fermat';
import tape from 'tape';
import {Line, Point} from '../src';
import {Line, Point, Segment} from '../src';


tape('offset and projections', (test) => {
Expand Down Expand Up @@ -42,3 +42,13 @@ tape('line intercepts', (test) => {

test.end();
});

tape('segment part', (test) => {
const a = new Segment(new Point(1, 1), new Point(2, 2));
const b = a.part(0.25, 0.75);
test.true(b.p1.equals(new Point(1.25, 1.25)));
test.true(b.p2.equals(new Point(1.75, 1.75)));

test.end();
});