Skip to content

Commit

Permalink
Fix 2
Browse files Browse the repository at this point in the history
  • Loading branch information
P-yiush07 committed Sep 30, 2024
1 parent c026b3b commit f42e735
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
24 changes: 7 additions & 17 deletions src/entities/CircleEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,24 +186,14 @@ export class CircleEntity implements Entity {

public cutAtPoints(pointsOnShape: Point[]): Entity[] {
if (!this.circle || !this.centerPoint) return [this];

const getAngle = (p: Point) => Math.atan2(p.y - this.centerPoint!.y, p.x - this.centerPoint!.x);

return pointsOnShape
.sort((a, b) => getAngle(a) - getAngle(b))
.map((point, i, arr) => {
const nextPoint = arr[(i + 1) % arr.length];
const arc = new ArcEntity(
this.centerPoint!,
this.circle!.r,
getAngle(point),
getAngle(nextPoint),
true
);
arc.lineColor = this.lineColor;
arc.lineWidth = this.lineWidth;
return arc;
});
const { centerPoint, circle } = this;
return pointsOnShape.map((point, i) => {
const nextPoint = pointsOnShape[(i + 1) % pointsOnShape.length];
const angle1 = Math.atan2(point.y - centerPoint.y, point.x - centerPoint.x);
const angle2 = Math.atan2(nextPoint.y - centerPoint.y, nextPoint.x - centerPoint.x);
return new ArcEntity(centerPoint, circle.r, angle1, angle2, true);
});
}

public async toJson(): Promise<JsonEntity<CircleJsonData> | null> {
Expand Down
53 changes: 41 additions & 12 deletions src/tools/eraser-tool.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Entity } from '../entities/Entity.ts';
import { Point, Segment } from '@flatten-js/core';
import { Circle, Point, Segment } from '@flatten-js/core';
import { compact } from 'es-toolkit';
import { LineEntity } from '../entities/LineEntity.ts';
import { findNeighboringPointsOnLine } from '../helpers/find-neighboring-points-on-line.ts';
import { addEntity, deleteEntity, setDebugEntities } from '../state.ts';
import { PointEntity } from '../entities/PointEntity.ts';
import { CircleEntity } from '../entities/CircleEntity.ts';
import { findNeighboringPointsOnCircle } from '../helpers/find-neighboring-points-on-circle.ts';
import { isPointEqual } from '../helpers/is-point-equal.ts';
import { ArcEntity } from '../entities/ArcEntity.ts';
import { findNeighboringPointsOnArc } from '../helpers/find-neighboring-points-on-arc.ts';

export function getAllIntersectionPoints(
entity: Entity,
Expand Down Expand Up @@ -59,21 +62,47 @@ export function eraseCircleSegment(
return;
}

const remainingArcs = circle.cutAtPoints(intersections)
.filter(arc => !arc.containsPointOnShape(clickedPointOnShape));
const [firstCutPoint, secondCutPoint] = findNeighboringPointsOnCircle(
clickedPointOnShape,
circle,
intersections,
);

const circleShape = circle.getShape() as Circle;
const center = circleShape.center;

const angles = [firstCutPoint, secondCutPoint, clickedPointOnShape].map(p =>
Math.atan2(p.y - center.y, p.x - center.x));

const [startAngle, endAngle] = isAngleBetween(angles[2], angles[0], angles[1])
? [angles[1], angles[0]]
: [angles[0], angles[1]];

const newArc = new ArcEntity(center, circleShape.r, startAngle, endAngle, true);
Object.assign(newArc, { lineColor: circle.lineColor, lineWidth: circle.lineWidth });

deleteEntity(circle);
addEntity(...remainingArcs);
addEntity(newArc);
}

export function eraseArcSegment(
arc: ArcEntity,
clickedPointOnShape: Point,
intersections: Point[],
): void {
const remainingArcs = arc.cutAtPoints(intersections)
.filter(cutArc => !cutArc.containsPointOnShape(clickedPointOnShape));
function isAngleBetween(angle: number, start: number, end: number): boolean {
const twoPi = 2 * Math.PI;
return ((angle - start + twoPi) % twoPi) <= ((end - start + twoPi) % twoPi);
}

export function eraseArcSegment(arc: ArcEntity, clickedPointOnShape: Point, intersections: Point[]): void {
const [first, second] = findNeighboringPointsOnArc(clickedPointOnShape, arc, intersections);

if (isPointEqual(first, second)) {
deleteEntity(arc);
return;
}

deleteEntity(arc);
addEntity(...remainingArcs);
arc.cutAtPoints([first, second])
.filter(cutArc => !cutArc.containsPointOnShape(clickedPointOnShape))
.forEach(newArc => {
Object.assign(newArc, { lineColor: arc.lineColor, lineWidth: arc.lineWidth });
addEntity(newArc);
});
}

0 comments on commit f42e735

Please sign in to comment.