画一条平行线 [英] Draw a parallel line

查看:850
本文介绍了画一条平行线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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.

< IMG SRC =http://i.stack.imgur.com/l7Nrq.pngALT =在这里输入的形象描述>

推荐答案

您想要做的是抵消坐标在正交方向。如果知道矢量数学,乘以由线路的端点之间的距离,由以下矩阵创建的矢量:

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 ]

说,第一行有个(X1,Y1)(x2,y2) X = X2-X1 Y = Y2-Y1 。结果
我们也有 L =开方(X * X + Y * Y),该线的长度(原谅符号)。然后,下一行应

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
它是归为新的行偏移。

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

在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

这篇关于画一条平行线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆