画一条平行线

发布于 2024-09-01 09:09:14 字数 250 浏览 8 评论 0原文

我有 x1,y1 和 x2,y2 形成一条线段。如何获得与第一条线平行的另一条线 x3,y3 - x4,y4,如图所示。我可以简单地将 n 添加到 x1 和 x2 以获得平行线,但这不是我想要的。我希望线条在图片中是平行的。

输入图像描述这里

I have x1,y1 and x2,y2 which forms a line segment. How can I get another line x3,y3 - x4,y4 which is parallel to the first line as in the picture. I can simply add n to x1 and x2 to get a parallel line but it is not what i wanted. I want the lines to be as parallel in the picture.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北凤男飞 2024-09-08 09:09:14

您要做的就是在正交方向上偏移坐标。如果您了解向量数学,请将由直线端点之间的距离创建的向量乘以以下矩阵:

[ 0 -1 ]
[ 1  0 ]

假设第一条线有点 (x1,y1)(x2 ,y2),其中 x=x2-x1y=y2-y1
我们还有 L = sqrt(x*x+y*y),即线的长度(请原谅符号)。那么下一行应该偏移

[ 0 -1 ] [x]
[ 1  0 ] [y]

=> dx = -y / Ldy = x / L
这是新行的标准化偏移量。

在类似 C# 的伪代码中:

var x1 = ..., x2 = ..., y1 = ..., y2 = ... // The original line
var L = Math.Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))

var offsetPixels = 10.0

// This is the second line
var x1p = x1 + offsetPixels * (y2-y1) / L
var x2p = x2 + offsetPixels * (y2-y1) / L
var y1p = y1 + offsetPixels * (x1-x2) / L
var y2p = y2 + offsetPixels * (x1-x2) / L

g.MoveTo(x1p,y1p) // I don't remember if this is the way
g.LineTo(x2p,y2p) // to draw a line in GDI+ but you get the idea

What you want to do is to offset the coordinates in the orthogonal direction. If you know vector math, multiply the vector created by the distance between the endpoints of the line by the following matrix:

[ 0 -1 ]
[ 1  0 ]

Say that the first line has the points (x1,y1), (x2,y2), with x=x2-x1, y=y2-y1.
We also have L = sqrt(x*x+y*y), the length of the line (pardon the notation). Then the next line should be offset by

[ 0 -1 ] [x]
[ 1  0 ] [y]

=> dx = -y / L, dy = x / L
which is the normalized offset for the new line.

In C#-like pseudocode:

var x1 = ..., x2 = ..., y1 = ..., y2 = ... // The original line
var L = Math.Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))

var offsetPixels = 10.0

// This is the second line
var x1p = x1 + offsetPixels * (y2-y1) / L
var x2p = x2 + offsetPixels * (y2-y1) / L
var y1p = y1 + offsetPixels * (x1-x2) / L
var y2p = y2 + offsetPixels * (x1-x2) / L

g.MoveTo(x1p,y1p) // I don't remember if this is the way
g.LineTo(x2p,y2p) // to draw a line in GDI+ but you get the idea
秋凉 2024-09-08 09:09:14

您是否尝试过将 n 减去 y1 和 y2,同时将 n 添加到 x1 和 x2?我想这可能有用

Did you try subtracting n to y1 and y2 along with adding n to x1 and x2? I guess that may work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文