Skip to content

Commit

Permalink
Fixed rendering tangent circles
Browse files Browse the repository at this point in the history
  • Loading branch information
Gutza committed Jan 2, 2021
1 parent 1c2d845 commit e379f50
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/geometry/Circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,18 @@ export class Circle extends PureGeometry implements IRegion, IDrawable {
};
}

private _getContour = (isClockwise: boolean): CircleArc => (
new CircleArc(this, 0, 0, this.zeroPoint, this.zeroPoint, isClockwise)
);

private _innerContour?: ArcPolygon;
public get innerContour(): ArcPolygon {
if (this._innerContour !== undefined) {
return this._innerContour;
}

return this._innerContour = new ArcPolygon(
[new CircleArc(this, 0, TWO_PI, this.zeroPoint, this.zeroPoint, false)],
[this._getContour(false)],
ERegionType.region
);
}
Expand All @@ -345,7 +349,7 @@ export class Circle extends PureGeometry implements IRegion, IDrawable {
}

return this._outerContour = new ArcPolygon(
[new CircleArc(this, TWO_PI, 0, this.zeroPoint, this.zeroPoint, true)],
[this._getContour(true)],
ERegionType.outerContour
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/geometry/helpers/BezierHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IPoint } from "../../Types";
import { ArcPolygon } from "../ArcPolygon";
import { makeSafeRenderingArc } from "./helperHelpers";

/**
* The main convenience Bezier function -- it rolls together all necessary logic and callbacks
Expand Down Expand Up @@ -95,7 +96,7 @@ function arcsToVertices(arcPolygon: ArcPolygon): IArcDTO[] {

const singleCircle = arcPolygon.arcs.length == 1;
for (let arcIndex = 0; arcIndex < arcPolygon.arcs.length; arcIndex++) {
const arc = arcPolygon.arcs[arcIndex];
const arc = makeSafeRenderingArc(arcPolygon.arcs[arcIndex]);
const arcMeta: IArcDTO = {
vertices: [],
singleCircle: singleCircle,
Expand Down
4 changes: 3 additions & 1 deletion src/geometry/helpers/PolygonHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IPoint } from "../../Types";
import { ArcPolygon } from "../ArcPolygon";
import { makeSafeRenderingArc } from "./helperHelpers";

const DEFAULT_RESOLUTION = 0.1;

Expand Down Expand Up @@ -38,7 +39,8 @@ function arcsToVertices (arcPolygon: ArcPolygon, resolution: number): IPoint[] {
const vertices: IPoint[] = new Array(vertexCount);

let vertexIndex = 0;
arcPolygon.arcs.forEach(arc => {
arcPolygon.arcs.forEach(unsafeArc => {
const arc = makeSafeRenderingArc(unsafeArc);
const { startAngle, endAngle, totalLength } = arc;
const { x: centerX, y: centerY } = arc.circle.center;
const radius = arc.circle.radius;
Expand Down
14 changes: 14 additions & 0 deletions src/geometry/helpers/helperHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CircleArc } from "../CircleArc";
import { TWO_PI } from "../utils/angles";

export function makeSafeRenderingArc(arc: CircleArc): CircleArc {
if (arc.startAngle != arc.endAngle) {
return arc;
}

if (arc.isClockwise) {
return new CircleArc(arc.circle, arc.startAngle, arc.endAngle + TWO_PI, arc.startPoint, arc.endPoint, true);
} else {
return new CircleArc(arc.circle, arc.startAngle + TWO_PI, arc.endAngle, arc.startPoint, arc.endPoint, false);
}
}
20 changes: 20 additions & 0 deletions test/bezierHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import assert from 'assert';
import { BezierHelper } from '../src';
import { RegionEngine } from '../src/RegionEngine';

describe("Bezier helper", () => {
it("Tangent circles should render correctly", () => {
const engine = new RegionEngine();
engine.add(-1, 0, 1);
engine.add(+1, 0, 1);
const regions = engine.computeRegions();
regions.forEach(r => {
const poly = BezierHelper.renderPolygonArc(
r,
v => v.vcoords,
anchors => anchors
);
assert.equal(poly.length > 2, true, `Received only ${poly.length} vertices!`);
});
})
});
1 change: 0 additions & 1 deletion test/generic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import assert from 'assert';
import { ArcPolygon } from '../src/geometry/ArcPolygon';

import { Circle } from '../src/geometry/Circle';
import { Point } from '../src/geometry/Point';
Expand Down

0 comments on commit e379f50

Please sign in to comment.