diff --git a/docs/src/guide/api.md b/docs/src/guide/api.md index 8b8ad518..8e2e555e 100644 --- a/docs/src/guide/api.md +++ b/docs/src/guide/api.md @@ -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 diff --git a/src/features/annotation/arrow.js b/src/features/annotation/arrow.js new file mode 100644 index 00000000..2f6a6e28 --- /dev/null +++ b/src/features/annotation/arrow.js @@ -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])); +}; diff --git a/src/features/index.js b/src/features/index.js index 74109a5a..eb5bd91b 100644 --- a/src/features/index.js +++ b/src/features/index.js @@ -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'); @@ -67,6 +68,7 @@ module.exports = { aligned_dim, angle_dim, + arrow, diameter_dim, dim_string, leader,