Skip to content

Commit

Permalink
Merge pull request #108 from leviat-tech/feat-annotation-arrow
Browse files Browse the repository at this point in the history
feat: annotations - arrow
  • Loading branch information
PPYChan-Leviat authored Oct 16, 2024
2 parents 25063a3 + 109c016 commit 0602685
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/src/guide/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,25 @@ Places an angle dimension given a center point, a point along a line, and an ang
// Construction: center, point, angle
const result = sketch.angle_dim([0, 0], [100, 0], 30);
// Will result in an angle dimension indicating 30 degrees.

```


### arrow( { startPoint, endPoint, neckWidth, arrowHeadLength, arrowHeadWidth, } )_

Places a basic arrow using the params object structure example below:.
params = {
startPoint: [0, 0],
endPoint: [0, 20],
neckWidth: 2,
arrowHeadLength: 7.5,
arrowHeadWidth: 6,
}

```js
const result = sketch.arrow({ startPoint: [0, 0], endPoint: [0, 20], neckWidth: 2, arrowHeadLength: 7.5, arrowHeadWidth: 6 });
// Will result in an arrow pointing vertically upwards.

```

## Operations
Expand Down
38 changes: 38 additions & 0 deletions src/features/annotation/arrow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = function arrow(sketch, params) {
// start points = start points, x, y
// end points = end points, x, y
// width = width or thickness of neck of arrow, required parameter
// arrowHeadLength = Arrow Head Length, required parameter
// arrowHeadWidth = Arrow Head Width, required parameter

const { startPoint, endPoint, neckWidth, arrowHeadLength, arrowHeadWidth } = params;
const radsToDegrees = (rads) => (rads / Math.PI) * 180.0;
const relativeX = endPoint[0] - startPoint[0];
const relativeY = endPoint[1] - startPoint[1];
const arrowLength = Math.sqrt(relativeX ** 2 + relativeY ** 2);
const rotationRads = Math.atan2(relativeY, relativeX);
const rotationDegrees = radsToDegrees(rotationRads) - 90;
const neckWidthHalf = neckWidth / 2;
const arrowHeadWidthHalf = arrowHeadWidth / 2;
const neckLength = arrowLength - arrowHeadLength;

const pointsHead = [
[-arrowHeadWidthHalf, neckLength],
[0, arrowLength],
[arrowHeadWidthHalf, neckLength],
[0, neckLength],
];
const pointsNeck = [
[-neckWidthHalf, neckLength],
[neckWidthHalf, neckLength],
[neckWidthHalf, 0],
[-neckWidthHalf, 0],
[-neckWidthHalf, neckLength],
];

const arrowHead = sketch.polyface(...pointsHead);
const arrowNeck = sketch.polyface(...pointsNeck);
const arrowSketch = arrowNeck.union(arrowHead);

return sketch.new.add(arrowSketch.rotate(rotationDegrees).translate(startPoint[0], startPoint[1]));
};
2 changes: 2 additions & 0 deletions src/features/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const style_all = require('./style/style_all.js');

const aligned_dim = require('./annotation/aligned-dim.js');
const angle_dim = require('./annotation/angle-dim.js');
const arrow = require('./annotation/arrow.js');
const diameter_dim = require('./annotation/diameter-dim.js');
const dim_string = require('./annotation/dim-string.js');
const leader = require('./annotation/leader.js');
Expand Down Expand Up @@ -67,6 +68,7 @@ module.exports = {

aligned_dim,
angle_dim,
arrow,
diameter_dim,
dim_string,
leader,
Expand Down

0 comments on commit 0602685

Please sign in to comment.