How to calculate internal angles of a closed polygon path with Paper.js?
Having a closed filled polygon defined as a set of points, question is how to get a list of internal angles between each 2 connecting segment lines that make up this polygon.
javascript 2d paperjs
add a comment |
Having a closed filled polygon defined as a set of points, question is how to get a list of internal angles between each 2 connecting segment lines that make up this polygon.
javascript 2d paperjs
add a comment |
Having a closed filled polygon defined as a set of points, question is how to get a list of internal angles between each 2 connecting segment lines that make up this polygon.
javascript 2d paperjs
Having a closed filled polygon defined as a set of points, question is how to get a list of internal angles between each 2 connecting segment lines that make up this polygon.
javascript 2d paperjs
javascript 2d paperjs
edited Nov 21 '18 at 12:48
Andriy B
asked Nov 21 '18 at 7:37
Andriy BAndriy B
654418
654418
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Paper.js allows to get angle between 2 vectors by calling getDirectedAngle (See class Point). So we can get angles between all segments, just we still need a way to decide which angle value is internal.
After spending some time i solved the task using this method.
First, i calculate angle between 2 connecting segments A and B. Next, i evaluate median vector M for A and B starting from the connecting point C where segments A and B meet. Finally i test whether point that lies on median between A and B is inside of polygon or outside of it and based on that i decide if this angle is internal or external.
Solution is below.
function checkPolygonAngle(path) {
if (path.curves.length<=1) {
return;
}
const angleText = function(point, angle) {
const text = new paper.PointText(point);
text.justification = 'center';
text.fillColor = 'black';
text.content = ""+(Math.round(angle*100)/100);
return text;
}
for (let i=1;i<=path.curves.length;i++) {
console.log(i);
const curve1 = path.curves[i-1];
const curve2 = i==path.curves.length ? path.curves[0] : path.curves[i];
const v1 = curve1.segment1.point.subtract(curve1.segment2.point);
const v2 = curve2.segment2.point.subtract(curve2.segment1.point);
const point = curve1.segment2.point;
(new paper.Path.Circle(point,3)).fillColor='red';
const angle = v1.getDirectedAngle(v2);
const medianAngle = angle/2;
const medianPoint = curve1.segment1.point.rotate(medianAngle, point);
const medianVector = medianPoint.subtract(point);
medianVector.length=30;
const pointOnMedian = point.add(medianVector);
(new paper.Path.Line(point,pointOnMedian)).strokeColor='green';
let insideAngle = null;
if (path.contains(pointOnMedian)) {
insideAngle = Math.abs(angle);
(new paper.Path.Circle(pointOnMedian,3)).fillColor='magenta';
angleText(pointOnMedian, insideAngle);
}
const oppositeMedianVector = medianVector.multiply(-1);
const pointOnOppositeMedian = point.add(oppositeMedianVector);
(new paper.Path.Line(point,pointOnOppositeMedian)).strokeColor='blue';
if (path.contains(pointOnOppositeMedian)) {
insideAngle = (360 - Math.abs(angle));
(new paper.Path.Circle(pointOnOppositeMedian,3)).fillColor='magenta';
angleText(pointOnOppositeMedian, insideAngle);
}
console.log(i+":", v1, v2, 'angle: ', insideAngle);
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407257%2fhow-to-calculate-internal-angles-of-a-closed-polygon-path-with-paper-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Paper.js allows to get angle between 2 vectors by calling getDirectedAngle (See class Point). So we can get angles between all segments, just we still need a way to decide which angle value is internal.
After spending some time i solved the task using this method.
First, i calculate angle between 2 connecting segments A and B. Next, i evaluate median vector M for A and B starting from the connecting point C where segments A and B meet. Finally i test whether point that lies on median between A and B is inside of polygon or outside of it and based on that i decide if this angle is internal or external.
Solution is below.
function checkPolygonAngle(path) {
if (path.curves.length<=1) {
return;
}
const angleText = function(point, angle) {
const text = new paper.PointText(point);
text.justification = 'center';
text.fillColor = 'black';
text.content = ""+(Math.round(angle*100)/100);
return text;
}
for (let i=1;i<=path.curves.length;i++) {
console.log(i);
const curve1 = path.curves[i-1];
const curve2 = i==path.curves.length ? path.curves[0] : path.curves[i];
const v1 = curve1.segment1.point.subtract(curve1.segment2.point);
const v2 = curve2.segment2.point.subtract(curve2.segment1.point);
const point = curve1.segment2.point;
(new paper.Path.Circle(point,3)).fillColor='red';
const angle = v1.getDirectedAngle(v2);
const medianAngle = angle/2;
const medianPoint = curve1.segment1.point.rotate(medianAngle, point);
const medianVector = medianPoint.subtract(point);
medianVector.length=30;
const pointOnMedian = point.add(medianVector);
(new paper.Path.Line(point,pointOnMedian)).strokeColor='green';
let insideAngle = null;
if (path.contains(pointOnMedian)) {
insideAngle = Math.abs(angle);
(new paper.Path.Circle(pointOnMedian,3)).fillColor='magenta';
angleText(pointOnMedian, insideAngle);
}
const oppositeMedianVector = medianVector.multiply(-1);
const pointOnOppositeMedian = point.add(oppositeMedianVector);
(new paper.Path.Line(point,pointOnOppositeMedian)).strokeColor='blue';
if (path.contains(pointOnOppositeMedian)) {
insideAngle = (360 - Math.abs(angle));
(new paper.Path.Circle(pointOnOppositeMedian,3)).fillColor='magenta';
angleText(pointOnOppositeMedian, insideAngle);
}
console.log(i+":", v1, v2, 'angle: ', insideAngle);
}
}
add a comment |
Paper.js allows to get angle between 2 vectors by calling getDirectedAngle (See class Point). So we can get angles between all segments, just we still need a way to decide which angle value is internal.
After spending some time i solved the task using this method.
First, i calculate angle between 2 connecting segments A and B. Next, i evaluate median vector M for A and B starting from the connecting point C where segments A and B meet. Finally i test whether point that lies on median between A and B is inside of polygon or outside of it and based on that i decide if this angle is internal or external.
Solution is below.
function checkPolygonAngle(path) {
if (path.curves.length<=1) {
return;
}
const angleText = function(point, angle) {
const text = new paper.PointText(point);
text.justification = 'center';
text.fillColor = 'black';
text.content = ""+(Math.round(angle*100)/100);
return text;
}
for (let i=1;i<=path.curves.length;i++) {
console.log(i);
const curve1 = path.curves[i-1];
const curve2 = i==path.curves.length ? path.curves[0] : path.curves[i];
const v1 = curve1.segment1.point.subtract(curve1.segment2.point);
const v2 = curve2.segment2.point.subtract(curve2.segment1.point);
const point = curve1.segment2.point;
(new paper.Path.Circle(point,3)).fillColor='red';
const angle = v1.getDirectedAngle(v2);
const medianAngle = angle/2;
const medianPoint = curve1.segment1.point.rotate(medianAngle, point);
const medianVector = medianPoint.subtract(point);
medianVector.length=30;
const pointOnMedian = point.add(medianVector);
(new paper.Path.Line(point,pointOnMedian)).strokeColor='green';
let insideAngle = null;
if (path.contains(pointOnMedian)) {
insideAngle = Math.abs(angle);
(new paper.Path.Circle(pointOnMedian,3)).fillColor='magenta';
angleText(pointOnMedian, insideAngle);
}
const oppositeMedianVector = medianVector.multiply(-1);
const pointOnOppositeMedian = point.add(oppositeMedianVector);
(new paper.Path.Line(point,pointOnOppositeMedian)).strokeColor='blue';
if (path.contains(pointOnOppositeMedian)) {
insideAngle = (360 - Math.abs(angle));
(new paper.Path.Circle(pointOnOppositeMedian,3)).fillColor='magenta';
angleText(pointOnOppositeMedian, insideAngle);
}
console.log(i+":", v1, v2, 'angle: ', insideAngle);
}
}
add a comment |
Paper.js allows to get angle between 2 vectors by calling getDirectedAngle (See class Point). So we can get angles between all segments, just we still need a way to decide which angle value is internal.
After spending some time i solved the task using this method.
First, i calculate angle between 2 connecting segments A and B. Next, i evaluate median vector M for A and B starting from the connecting point C where segments A and B meet. Finally i test whether point that lies on median between A and B is inside of polygon or outside of it and based on that i decide if this angle is internal or external.
Solution is below.
function checkPolygonAngle(path) {
if (path.curves.length<=1) {
return;
}
const angleText = function(point, angle) {
const text = new paper.PointText(point);
text.justification = 'center';
text.fillColor = 'black';
text.content = ""+(Math.round(angle*100)/100);
return text;
}
for (let i=1;i<=path.curves.length;i++) {
console.log(i);
const curve1 = path.curves[i-1];
const curve2 = i==path.curves.length ? path.curves[0] : path.curves[i];
const v1 = curve1.segment1.point.subtract(curve1.segment2.point);
const v2 = curve2.segment2.point.subtract(curve2.segment1.point);
const point = curve1.segment2.point;
(new paper.Path.Circle(point,3)).fillColor='red';
const angle = v1.getDirectedAngle(v2);
const medianAngle = angle/2;
const medianPoint = curve1.segment1.point.rotate(medianAngle, point);
const medianVector = medianPoint.subtract(point);
medianVector.length=30;
const pointOnMedian = point.add(medianVector);
(new paper.Path.Line(point,pointOnMedian)).strokeColor='green';
let insideAngle = null;
if (path.contains(pointOnMedian)) {
insideAngle = Math.abs(angle);
(new paper.Path.Circle(pointOnMedian,3)).fillColor='magenta';
angleText(pointOnMedian, insideAngle);
}
const oppositeMedianVector = medianVector.multiply(-1);
const pointOnOppositeMedian = point.add(oppositeMedianVector);
(new paper.Path.Line(point,pointOnOppositeMedian)).strokeColor='blue';
if (path.contains(pointOnOppositeMedian)) {
insideAngle = (360 - Math.abs(angle));
(new paper.Path.Circle(pointOnOppositeMedian,3)).fillColor='magenta';
angleText(pointOnOppositeMedian, insideAngle);
}
console.log(i+":", v1, v2, 'angle: ', insideAngle);
}
}
Paper.js allows to get angle between 2 vectors by calling getDirectedAngle (See class Point). So we can get angles between all segments, just we still need a way to decide which angle value is internal.
After spending some time i solved the task using this method.
First, i calculate angle between 2 connecting segments A and B. Next, i evaluate median vector M for A and B starting from the connecting point C where segments A and B meet. Finally i test whether point that lies on median between A and B is inside of polygon or outside of it and based on that i decide if this angle is internal or external.
Solution is below.
function checkPolygonAngle(path) {
if (path.curves.length<=1) {
return;
}
const angleText = function(point, angle) {
const text = new paper.PointText(point);
text.justification = 'center';
text.fillColor = 'black';
text.content = ""+(Math.round(angle*100)/100);
return text;
}
for (let i=1;i<=path.curves.length;i++) {
console.log(i);
const curve1 = path.curves[i-1];
const curve2 = i==path.curves.length ? path.curves[0] : path.curves[i];
const v1 = curve1.segment1.point.subtract(curve1.segment2.point);
const v2 = curve2.segment2.point.subtract(curve2.segment1.point);
const point = curve1.segment2.point;
(new paper.Path.Circle(point,3)).fillColor='red';
const angle = v1.getDirectedAngle(v2);
const medianAngle = angle/2;
const medianPoint = curve1.segment1.point.rotate(medianAngle, point);
const medianVector = medianPoint.subtract(point);
medianVector.length=30;
const pointOnMedian = point.add(medianVector);
(new paper.Path.Line(point,pointOnMedian)).strokeColor='green';
let insideAngle = null;
if (path.contains(pointOnMedian)) {
insideAngle = Math.abs(angle);
(new paper.Path.Circle(pointOnMedian,3)).fillColor='magenta';
angleText(pointOnMedian, insideAngle);
}
const oppositeMedianVector = medianVector.multiply(-1);
const pointOnOppositeMedian = point.add(oppositeMedianVector);
(new paper.Path.Line(point,pointOnOppositeMedian)).strokeColor='blue';
if (path.contains(pointOnOppositeMedian)) {
insideAngle = (360 - Math.abs(angle));
(new paper.Path.Circle(pointOnOppositeMedian,3)).fillColor='magenta';
angleText(pointOnOppositeMedian, insideAngle);
}
console.log(i+":", v1, v2, 'angle: ', insideAngle);
}
}
edited Nov 21 '18 at 8:42
answered Nov 21 '18 at 7:56
Andriy BAndriy B
654418
654418
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407257%2fhow-to-calculate-internal-angles-of-a-closed-polygon-path-with-paper-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown