Skip to content

Commit

Permalink
Merge b7b997e into 02f8f45
Browse files Browse the repository at this point in the history
  • Loading branch information
bubkoo committed Mar 23, 2020
2 parents 02f8f45 + b7b997e commit af52951
Show file tree
Hide file tree
Showing 73 changed files with 8,989 additions and 1,154 deletions.
2 changes: 1 addition & 1 deletion packages/x6-react-shape/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"watch": "watch 'yarn build' ./src",
"build:esm": "tsc --module esnext --target es2015 --outDir ./es",
"build:cjs": "tsc --module commonjs --target es5 --outDir ./lib",
"build:umd": "webpack --config webpack.config.ts ",
"build:umd": "webpack --config webpack.config.ts --mode production",
"build": "run-p build:cjs build:esm build:umd",
"prebuild": "run-s lint clean",
"prepare": "yarn build",
Expand Down
3 changes: 1 addition & 2 deletions packages/x6/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"clean:build": "rimraf dist es lib",
"clean:coverage": "rimraf ./test/coverage",
"clean": "run-p clean:build clean:coverage",
"lint": "run-s lint:ts lint:es lint:style",
"lint:es": "eslint --ext .js scripts --fix",
"lint:ts": "tslint -c tslint.json -p tsconfig.json --fix",
"lint:style": "stylelint 'src/**/*.less' --syntax less --fix",
"lint": "run-s lint:ts lint:es lint:style",
"build:esm": "tsc --module esnext --target es2015 --outDir ./es",
"build:cjs": "tsc --module commonjs --target es5 --outDir ./lib",
"build:umd": "webpack --config webpack.config.ts --mode production",
Expand All @@ -41,7 +41,6 @@
"prebuild": "run-s lint clean",
"watch": "watch 'yarn build:dev' ./src",
"test": "jest --coverage",
"test1": "karma start",
"test:watch": "jest --watch --coverage",
"test:debug": "DEBUG_MODE=1 jest",
"coveralls": "cat ./test/coverage/lcov.info | coveralls",
Expand Down
2 changes: 1 addition & 1 deletion packages/x6/src/geometry/curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ export namespace Curve {
function getCurveControlPoints(
points: (Point | Point.PointLike | Point.PointData)[],
) {
const knots = points.map(p => Point.parse(p))
const knots = points.map(p => Point.clone(p))
const firstControlPoints = []
const secondControlPoints = []
const n = knots.length - 1
Expand Down
6 changes: 3 additions & 3 deletions packages/x6/src/geometry/ellipse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Ellipse implements Ellipse.EllipseLike {
* lying on the ellipse boundary and `n > 1` for points outside the ellipse.
*/
normalizedDistance(p: Point | Point.PointLike | Point.PointData) {
const ref = Point.parse(p)
const ref = Point.clone(p)
const dx = ref.x - this.x
const dy = ref.y - this.y
const a = this.a
Expand Down Expand Up @@ -133,7 +133,7 @@ export class Ellipse implements Ellipse.EllipseLike {
p: Point | Point.PointLike | Point.PointData,
angle: number = 0,
) {
const ref = Point.parse(p)
const ref = Point.clone(p)

if (angle) {
ref.rotate(angle, this.getCenter())
Expand Down Expand Up @@ -167,7 +167,7 @@ export class Ellipse implements Ellipse.EllipseLike {
}

tangentTheta(p: Point | Point.PointLike | Point.PointData) {
const ref = Point.parse(p)
const ref = Point.clone(p)
const x0 = ref.x
const y0 = ref.y
const a = this.a
Expand Down
2 changes: 1 addition & 1 deletion packages/x6/src/geometry/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export class Line {
* `0` if the point lies on the line.
*/
pointOffset(p: Point | Point.PointLike | Point.PointData) {
const ref = Point.parse(p)
const ref = Point.clone(p)
const start = this.start
const end = this.end
const determinant =
Expand Down
1 change: 1 addition & 0 deletions packages/x6/src/geometry/path/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './path'
export * from './segment'
20 changes: 15 additions & 5 deletions packages/x6/src/geometry/path/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export class Path {
return lastVisibleIndex
}

protected getSegmentSubdivisions(options: Path.Options = {}): Segment[][] {
getSegmentSubdivisions(options: Path.Options = {}): Segment[][] {
const precision = this.getPrecision(options)
const segmentSubdivisions = []
for (let i = 0, ii = this.segments.length; i < ii; i += 1) {
Expand Down Expand Up @@ -1088,8 +1088,8 @@ export class Path {

export namespace Path {
export interface Options {
precision?: number
segmentSubdivisions?: Segment[][]
precision?: number | null
segmentSubdivisions?: Segment[][] | null
}
}
export namespace Path {
Expand Down Expand Up @@ -1154,10 +1154,20 @@ export namespace Path {
): Segment
export function createSegment(
type: 'C',
...args: GetConstructorArgs<typeof CurveTo>
x0: number,
y0: number,
x1: number,
y1: number,
x2: number,
y2: number,
): Segment
export function createSegment(
type: 'C',
p1: Point | Point.PointLike | Point.PointData,
p2: Point | Point.PointLike | Point.PointData,
p3: Point | Point.PointLike | Point.PointData,
): Segment
export function createSegment(type: 'Z' | 'z'): Segment

export function createSegment(
type: 'L' | 'M' | 'C' | 'Z' | 'z',
...args: any[]
Expand Down
91 changes: 67 additions & 24 deletions packages/x6/src/geometry/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class Point implements Point.PointLike {
* If `points` is an empty array, `null` is returned.
*/
closest(points: (Point | Point.PointLike | Point.PointData)[]) {
const pts = points.map(p => Point.parse(p))
const pts = points.map(p => Point.clone(p))
if (pts.length === 1) {
return pts[0]
}
Expand All @@ -99,17 +99,17 @@ export class Point implements Point.PointLike {
}

distance(p: Point | Point.PointLike | Point.PointData) {
const ref = Point.parse(p)
const ref = Point.clone(p)
return new Line(this, ref).length()
}

squaredDistance(p: Point | Point.PointLike | Point.PointData) {
const ref = Point.parse(p)
const ref = Point.clone(p)
return new Line(this, ref).squaredLength()
}

manhattanDistance(p: Point | Point.PointLike | Point.PointData) {
const ref = Point.parse(p)
const ref = Point.clone(p)
return Math.abs(ref.x - this.x) + Math.abs(ref.y - this.y)
}

Expand All @@ -126,7 +126,7 @@ export class Point implements Point.PointLike {
* Returns the angle between vector from me to `p` and the x axis.
*/
theta(p: Point | Point.PointLike | Point.PointData = new Point()): number {
const ref = Point.parse(p)
const ref = Point.clone(p)
const y = -(ref.y - this.y) // invert the y-axis.
const x = ref.x - this.x
let rad = Math.atan2(y, x)
Expand Down Expand Up @@ -214,7 +214,7 @@ export class Point implements Point.PointLike {
* Returns the bearing between me and the given point.
*/
bearing(p: Point | Point.PointLike | Point.PointData) {
const ref = Point.parse(p)
const ref = Point.clone(p)
return new Line(this, ref).bearing()
}

Expand All @@ -229,8 +229,8 @@ export class Point implements Point.PointLike {
p2: Point | Point.PointLike | Point.PointData,
) {
if (p1 != null && p2 != null) {
const a = Point.parse(p1)
const b = Point.parse(p2)
const a = Point.clone(p1)
const b = Point.clone(p2)
return (b.x - this.x) * (a.y - this.y) - (b.y - this.y) * (a.x - this.x)
}

Expand All @@ -241,7 +241,7 @@ export class Point implements Point.PointLike {
* Returns the dot product of this point with given other point.
*/
dot(p: Point | Point.PointLike | Point.PointData) {
const ref = Point.parse(p)
const ref = Point.clone(p)
return this.x * ref.x + this.y * ref.y
}

Expand All @@ -255,7 +255,7 @@ export class Point implements Point.PointLike {
return new Point(this.x - dx, this.y - dy!)
}

const p = Point.parse(dx)
const p = Point.clone(dx)
return new Point(this.x - p.x, this.y - p.y)
}

Expand All @@ -266,7 +266,7 @@ export class Point implements Point.PointLike {
lerp(p: Point | Point.PointLike | Point.PointData, t: number) {
const x = this.x
const y = this.y
const ref = Point.parse(p)
const ref = Point.clone(p)
return new Point((1 - t) * x + t * ref.x, (1 - t) * y + t * ref.y)
}

Expand All @@ -276,13 +276,13 @@ export class Point implements Point.PointLike {
}

move(ref: Point | Point.PointLike | Point.PointData, distance: number) {
const p = Point.parse(ref)
const p = Point.clone(ref)
const rad = Angle.toRad(p.theta(this))
return this.translate(Math.cos(rad) * distance, -Math.sin(rad) * distance)
}

reflection(ref: Point | Point.PointLike | Point.PointData) {
const p = Point.parse(ref)
const p = Point.clone(ref)
return p.move(this, this.distance(p))
}

Expand All @@ -296,16 +296,16 @@ export class Point implements Point.PointLike {
}

equals(p: Point | Point.PointLike | Point.PointData) {
const ref = Point.parse(p)
const ref = Point.clone(p)
return ref != null && ref.x === this.x && ref.y === this.y
}

clone() {
return new Point(this.x, this.y)
return Point.clone(this)
}

toJSON() {
return { x: this.x, y: this.y }
return Point.toJSON(this)
}

valueOf() {
Expand All @@ -324,6 +324,25 @@ export namespace Point {
}

export type PointData = [number, number]

export function isPointLike(o: any): o is PointLike {
return (
o != null &&
typeof o === 'object' &&
typeof o.x === 'number' &&
typeof o.y === 'number'
)
}

export function isPointData(o: any): o is PointData {
return (
o != null &&
Array.isArray(o) &&
o.length === 2 &&
typeof o[0] === 'number' &&
typeof o[1] === 'number'
)
}
}

export namespace Point {
Expand All @@ -335,12 +354,12 @@ export namespace Point {
return new Point(x, y)
}

return parse(x)
return clone(x)
}

export function parse(p: Point | PointLike | PointData) {
export function clone(p: Point | PointLike | PointData) {
if (p instanceof Point) {
return p.clone()
return new Point(p.x, p.y)
}

if (Array.isArray(p)) {
Expand All @@ -350,6 +369,18 @@ export namespace Point {
return new Point(p.x, p.y)
}

export function toJSON(p: Point | PointLike | PointData) {
if (p instanceof Point) {
return { x: p.x, y: p.y }
}

if (Array.isArray(p)) {
return { x: p[0], y: p[1] }
}

return { x: p.x, y: p.y }
}

/**
* Returns a new Point object from the given polar coordinates.
* @see http://en.wikipedia.org/wiki/Polar_coordinate_system
Expand All @@ -361,7 +392,7 @@ export namespace Point {
) {
let x = Math.abs(r * Math.cos(rad))
let y = Math.abs(r * Math.sin(rad))
const org = parse(origin)
const org = clone(origin)
const deg = Angle.normalize(Angle.toDeg(rad))

if (deg < 90) {
Expand All @@ -380,8 +411,8 @@ export namespace Point {
point: Point | PointLike | PointData,
origin: Point | PointLike | PointData = new Point(),
) {
const p = parse(point)
const o = parse(origin)
const p = clone(point)
const o = clone(origin)
const dx = p.x - o.x
const dy = p.y - o.y
return new Point(
Expand All @@ -390,6 +421,18 @@ export namespace Point {
)
}

export function equals(p1?: Point.PointLike, p2?: Point.PointLike) {
if (p1 === p2) {
return true
}

if (p1 != null && p2 != null) {
return p1.x === p2.x && p1.y === p2.y
}

return false
}

export function equalPoints(p1: Point[], p2: Point[]) {
if (
(p1 == null && p2 != null) ||
Expand Down Expand Up @@ -448,8 +491,8 @@ export namespace Point {
sin: number,
center: Point | PointLike | PointData = new Point(),
) {
const source = parse(point)
const origin = parse(center)
const source = clone(point)
const origin = clone(center)
const dx = source.x - origin.x
const dy = source.y - origin.y
const x1 = dx * cos - dy * sin
Expand Down

0 comments on commit af52951

Please sign in to comment.