最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

svg - Javascript: Find point on perpendicular line always the same distance away - Stack Overflow

programmeradmin0浏览0评论

I'm trying to find a point that is equal distance away from the middle of a perpendicular line. I want to use this point to create a Bézier curve using the start and end points, and this other point I'm trying to find.

I've calculated the perpendicular line, and I can plot points on that line, but the problem is that depending on the angle of the line, the points get further away or closer to the original line, and I want to be able to calculate it so it's always X units away.

Take a look at this JSFiddle which shows the original line, with some points plotted along the perpendicular line:

/.

If you change the start and end points, you can see these plotted points getting closer together or further away.

How do I get them to be uniformly the same distance apart from each other no matter what the angle is?

Code snippit below:

// Start and end points
var startX = 120
var startY = 150
var endX = 180
var endY = 130

// Calculate how far above or below the control point should be
var centrePointX = ((startX + endX) / 2);
var centrePointY = ((startY + endY) / 2);

// Calculate slopes and Y intersects
var lineSlope = (endY - startY) / (endX - startX);
var perpendicularSlope = -1 / lineSlope;
var yIntersect = centrePointY - (centrePointX * perpendicularSlope);

// Draw a line between the two original points
R.path('M '+startX+' '+startY+', L '+endX+' '+endY);

I'm trying to find a point that is equal distance away from the middle of a perpendicular line. I want to use this point to create a Bézier curve using the start and end points, and this other point I'm trying to find.

I've calculated the perpendicular line, and I can plot points on that line, but the problem is that depending on the angle of the line, the points get further away or closer to the original line, and I want to be able to calculate it so it's always X units away.

Take a look at this JSFiddle which shows the original line, with some points plotted along the perpendicular line:

http://jsfiddle/eLxcB/1/.

If you change the start and end points, you can see these plotted points getting closer together or further away.

How do I get them to be uniformly the same distance apart from each other no matter what the angle is?

Code snippit below:

// Start and end points
var startX = 120
var startY = 150
var endX = 180
var endY = 130

// Calculate how far above or below the control point should be
var centrePointX = ((startX + endX) / 2);
var centrePointY = ((startY + endY) / 2);

// Calculate slopes and Y intersects
var lineSlope = (endY - startY) / (endX - startX);
var perpendicularSlope = -1 / lineSlope;
var yIntersect = centrePointY - (centrePointX * perpendicularSlope);

// Draw a line between the two original points
R.path('M '+startX+' '+startY+', L '+endX+' '+endY);
Share Improve this question edited Aug 1, 2013 at 8:48 Dave Bish 19.7k7 gold badges49 silver badges63 bronze badges asked Aug 1, 2013 at 8:43 dKendKen 3,1271 gold badge29 silver badges37 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 11

Generally you can get the coordinates of a normal of a line like this:

P1 = {r * cos(a) + Cx, -r * sin(a) + Cy},
P2 = {-r * cos(a) + Cx, r * sin(a) + Cy}.

A demo applying this to your case at jsFiddle.

发布评论

评论列表(0)

  1. 暂无评论