Skip to content

Commit

Permalink
Fix Arc issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasdeluna committed Oct 21, 2023
1 parent 23d91c6 commit 757da70
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 70 deletions.
66 changes: 36 additions & 30 deletions src/Components/Arc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,56 +86,62 @@ class Arc extends Component {
}

_calcAngle() {
const pointAvec2 = toVector2(this.pointA);
const pointBvec2 = toVector2(this.pointB);
const pointCvec2 = toVector2(this.pointC);
const A = toVector2(this.pointA);
const B = toVector2(this.pointB);
const C = toVector2(this.pointC);

const vectorBtoA = pointAvec2.clone().sub(pointBvec2);
const vectorBtoC = pointCvec2.clone().sub(pointBvec2);
const BA = A.sub(B);
const BC = C.sub(B);

const dot = vectorBtoA.dot(vectorBtoC);
const det = vectorBtoA.x * vectorBtoC.y - vectorBtoA.y * vectorBtoC.x;
let angle = Math.atan2(det, dot);
let angle = BA.angle() - BC.angle();
angle = angle < 0 ? angle + 2 * Math.PI : angle;
return angle;
}

// Normalize the angle to be between 0 and 2π
while (angle < 0) angle += 2 * Math.PI;
while (angle > 2 * Math.PI) angle -= 2 * Math.PI;
_calcVectorAngle(point1: InputPosition, point2: InputPosition) {
const vector = toVector2(point2).sub(toVector2(point1));
return Math.atan2(vector.y, vector.x);
}

return angle;
_updateArc(angle: number, cameraZoom: number) {
this._arc.geometry.dispose();

const B = toVector2(this.pointB);
const C = toVector2(this.pointC);
const BC = C.sub(B);
const startAngle = BC.angle();

this._arc.geometry = new CircleGeometry(
this.radius / cameraZoom,
64,
startAngle,
angle
);
this._arc.geometry.computeVertexNormals();
}

_updateOutline(angle: number, cameraZoom: number) {
const startAngle = 0;
const endAngle = angle;
const clockwise = false;
const B = toVector2(this.pointB);
const C = toVector2(this.pointC);
const BC = C.sub(B);
const startAngle = BC.angle();
const endAngle = startAngle + angle;

// Create Arc-curve
const arcCurve = new ArcCurve(
0,
0,
this.radius / cameraZoom,
startAngle,
endAngle,
clockwise
false
);
// Generate points on ArcCurve

const points = arcCurve.getPoints(50);
this._curvedOutline.geometry.setPositions(
points.flatMap((v) => [v.x, v.y, 3])
);
}

_updateArc(angle: number, cameraZoom: number) {
this._arc.geometry.dispose();
this._arc.geometry = new CircleGeometry(
this.radius / cameraZoom,
64,
0,
angle
);
this._arc.geometry.computeVertexNormals();
}

_updateText(angle: number, cameraZoom: number) {
this._text.setText(Math.round((angle * 180) / Math.PI).toString() + "°");
this._text.position.set(
Expand All @@ -154,7 +160,7 @@ class Arc extends Component {

update(camera: OrthographicCamera) {
const pointBVec = toVector3(this.pointB);
this.position.set(pointBVec.x, pointBVec.y, 0);
this.position.set(pointBVec.x, pointBVec.y, this.position.z);

const angle = this._calcAngle();
this._updateOutline(angle, camera.zoom);
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Label.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Vector2, Object3D, Group, OrthographicCamera } from "three";
import { Vector2, OrthographicCamera } from "three";
import { toVector3 } from "../utils";
import Line from "./Line";
import Text from "./Text";
Expand Down
59 changes: 20 additions & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
// export { default as Graphica } from "./Graphica";
// export { default as Arc } from "./Components/Arc";
// export { default as Bracket } from "./Components/Bracket";
// export { default as Button } from "./Components/Button";
// export { default as Circle } from "./Components/Circle";
// export { default as Grid } from "./Components/Grid";
// export { default as InfiniteLine } from "./Components/InfiniteLine";
// export { default as InputField } from "./Components/InputField";
// export { default as Label } from "./Components/Label";
// export { default as Latex } from "./Components/Latex";
// export { default as Line } from "./Components/Line";
// export { default as Plot } from "./Components/Plot";
// export { default as Point } from "./Components/Point";
// export { default as Polygon } from "./Components/Shape";
// export { default as Slider } from "./Components/Slider";
// export { default as Text } from "./Components/Text";
// export { default as Vector } from "./Components/Vector";
// export { default as Fraction } from "./Components/Derived/Fraction";
// export { default as SVGLoader } from "./Components/SVGLoader";
// export * as three from "three";

import Arc from "./Components/Arc";
import Grid from "./Components/Grid";
import Label from "./Components/Label";
import Point from "./Components/Point";
import Graphica from "./Graphica";

const a = new Graphica();
const b = new Grid();
const p = new Point(0, 0, { draggable: "unrestricted" });
const pp = new Point(5, 0);
const ppp = new Point(0, 5);
const c = new Arc(p, pp, ppp);
a.add(b);
a.add(c);
a.add(p);
a.add(pp);
a.add(ppp);
a.run();
export { default as Graphica } from "./Graphica";
export { default as Arc } from "./Components/Arc";
export { default as Bracket } from "./Components/Bracket";
export { default as Button } from "./Components/Button";
export { default as Circle } from "./Components/Circle";
export { default as Grid } from "./Components/Grid";
export { default as InfiniteLine } from "./Components/InfiniteLine";
export { default as InputField } from "./Components/InputField";
export { default as Label } from "./Components/Label";
export { default as Latex } from "./Components/Latex";
export { default as Line } from "./Components/Line";
export { default as Plot } from "./Components/Plot";
export { default as Point } from "./Components/Point";
export { default as Polygon } from "./Components/Shape";
export { default as Slider } from "./Components/Slider";
export { default as Text } from "./Components/Text";
export { default as Vector } from "./Components/Vector";
export { default as Fraction } from "./Components/Derived/Fraction";
export { default as SVGLoader } from "./Components/SVGLoader";
export * as three from "three";

0 comments on commit 757da70

Please sign in to comment.