Skip to content

Commit

Permalink
feat: getPoints
Browse files Browse the repository at this point in the history
  • Loading branch information
F-star committed May 20, 2024
1 parent 27dfc05 commit b37a9ea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 34 deletions.
17 changes: 8 additions & 9 deletions packages/core/src/graphs/regular_polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ export class RegularPolygon extends Graph<RegularPolygonAttrs> {
};
}

private getPoints() {
return getRegularPolygon(this.getSize(), this.attrs.count);
}

override getMinBbox(): Readonly<IBox> {
if (this._cacheMinBbox) {
return this._cacheMinBbox;
}
const tf = new Matrix(...this.attrs.transform);
const points = getRegularPolygon(this.getSize(), this.attrs.count).map(
(pt) => tf.apply(pt),
);
const points = this.getPoints().map((pt) => tf.apply(pt));
const bbox = getPointsBbox(points);
this._cacheMinBbox = bbox;
return bbox;
Expand Down Expand Up @@ -91,7 +93,7 @@ export class RegularPolygon extends Graph<RegularPolygonAttrs> {

ctx.transform(...attrs.transform);

const points = getRegularPolygon(this.getSize(), attrs.count);
const points = this.getPoints();

ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
Expand Down Expand Up @@ -161,10 +163,7 @@ export class RegularPolygon extends Graph<RegularPolygonAttrs> {
// TODO: solve padding
const tf = new Matrix(...this.attrs.transform);
const point = tf.applyInverse({ x, y });
return isPointInConvexPolygon(
getRegularPolygon(this.getSize(), this.attrs.count),
point,
);
return isPointInConvexPolygon(this.getPoints(), point);
}

override getSVGTagHead(offset?: IPoint) {
Expand All @@ -174,7 +173,7 @@ export class RegularPolygon extends Graph<RegularPolygonAttrs> {
tf[5] += offset.y;
}

const points = getRegularPolygon(this.getSize(), this.attrs.count);
const points = this.getPoints();

return `<polygon points="${points
.map((p) => `${p.x},${p.y}`)
Expand Down
23 changes: 8 additions & 15 deletions packages/core/src/graphs/star.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ export class Star extends Graph<StarAttrs> {
};
}

private getPoints() {
return getStar(this.getSize(), this.attrs.count, this.attrs.starInnerScale);
}

override getMinBbox(): Readonly<IBox> {
if (this._cacheMinBbox) {
return this._cacheMinBbox;
}
const tf = new Matrix(...this.attrs.transform);
const points = getStar(
this.getSize(),
this.attrs.count,
this.attrs.starInnerScale,
).map((pt) => tf.apply(pt));
const points = this.getPoints().map((pt) => tf.apply(pt));
const bbox = getPointsBbox(points);
this._cacheMinBbox = bbox;
return bbox;
Expand Down Expand Up @@ -94,7 +94,7 @@ export class Star extends Graph<StarAttrs> {

ctx.transform(...attrs.transform);

const points = getStar(this.getSize(), attrs.count, attrs.starInnerScale);
const points = this.getPoints();

ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
Expand Down Expand Up @@ -172,10 +172,7 @@ export class Star extends Graph<StarAttrs> {
// TODO: solve padding
const tf = new Matrix(...this.attrs.transform);
const point = tf.applyInverse({ x, y });
return isPointInPolygon(
getStar(this.getSize(), this.attrs.count, this.attrs.starInnerScale),
point,
);
return isPointInPolygon(this.getPoints(), point);
}

protected override getSVGTagHead(offset?: IPoint) {
Expand All @@ -185,11 +182,7 @@ export class Star extends Graph<StarAttrs> {
tf[5] += offset.y;
}

const points = getStar(
this.getSize(),
this.attrs.count,
this.attrs.starInnerScale,
);
const points = this.getPoints();

return `<polygon points="${points
.map((p) => `${p.x},${p.y}`)
Expand Down
10 changes: 0 additions & 10 deletions packages/geo/src/geo/geo_rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ export const getRectByTwoPoint = (point1: IPoint, point2: IPoint): IRect => {
};
};

export const getRectByPoints = (points: IPoint[]): IRect => {
const xs = points.map((p) => p.x);
const ys = points.map((p) => p.y);
const x = Math.min(...xs);
const y = Math.min(...ys);
const width = Math.max(...xs) - x;
const height = Math.max(...ys) - y;
return { x, y, width, height };
};

export const isPointInRect = (
point: IPoint,
rect: {
Expand Down

0 comments on commit b37a9ea

Please sign in to comment.