从 2 个点和一个宽度获取一个矩形
可能的重复:
在任意宽度的两点之间绘制矩形
我有以下内容
RectangleF GetRectangleFrom2PointsAndWidth(Point p1, Point p2, int width)
如何实现? (点位于图片中线的末端。)
Possible Duplicate:
Drawing rectangle between two points with arbitrary width
I have the following
RectangleF GetRectangleFrom2PointsAndWidth(Point p1, Point p2, int width)
How to implement it? (points are located to the end of the middle line from the picture.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,找到中线的斜率。称之为
m
。问题:中线可能是垂直的。这可能会导致问题。因此,请计算点 P1 和 P2 之间的 deltaX(X 的变化)和 deltaY(Y 的变化)。把这两个数字当作给你一个“方向”,是解决这个问题的关键。要制作矩形的角,您需要从点 1 沿垂直方向移动。垂直方向由
-1/m
给出。为了避免被 0 除的危险,更好的思考方式是,每次在 Y 方向移动deltaX< 时,都希望在 X 方向移动
-deltaY
/代码>。您需要标准化
-deltaY
和deltaX
给出的“方向”。为此,请找到 P1 和 P2 之间的距离。我将这个距离称为D
。现在您想要执行以下操作:对于第一个角,从 P1 开始,沿 x 方向移动
-deltaY / D
乘以width/2
。在 y 方向上移动deltaX / D
乘以width/2
。对于第二个角,从 P1 开始,沿 x 方向移动
-deltaY / D
乘以-width/2
。在 y 方向上移动deltaX / D
乘以-width/2
。对于第三个角,从 P2 开始,沿 x 方向移动
-deltaY / D
乘以width/2
。在 y 方向上移动deltaX / D
乘以width/2
。对于第四个角,从 P2 开始,沿 x 方向移动
-deltaY / D
乘以-width/2
。在 y 方向上移动deltaX / D
乘以-width/2
。祝你好运!我们在这里使用的称为向量,但我在上面的答案中的语言有点尴尬,因为我在编写它时避免使用几乎所有向量语言。 “标准化”这个词溜进去了。期待大学里一门叫做“线性代数”的课程,它会让你成为这个问题的专家。
First, find the slope of your middle line. Call it
m
. Problem: The middle line might be vertical. This could cause problems. So instead calculatedeltaX
(the change in X) anddeltaY
(the change in Y) between the points P1 and P2. Thinking of these two numbers as giving you a "direction" is the key to solving this problem.To make the corners of your rectangle, you want to move from Point 1 in the perpendicular direction. The perpendicular direction is given by
-1/m
. To avoid the danger of dividing by 0, a better way to think about it is that you want to move in the X direction by-deltaY
each time you move in the Y direction bydeltaX
.You will want to normalize your "direction" given by
-deltaY
anddeltaX
. To do this, find the distance between P1 and P2. I will call this distanceD
. Now you want to do the following:For the first corner, start at P1 and move in the x-direction by
-deltaY / D
timeswidth/2
. Move in the y-direction bydeltaX / D
timeswidth/2
.For the second corner, start at P1 and move in the x-direction by
-deltaY / D
times-width/2
. Move in the y-direction bydeltaX / D
times-width/2
.For the third corner, start at P2 and move in the x-direction by
-deltaY / D
timeswidth/2
. Move in the y-direction bydeltaX / D
timeswidth/2
.For the fourth corner, start at P2 and move in the x-direction by
-deltaY / D
times-width/2
. Move in the y-direction bydeltaX / D
times-width/2
.Good luck! What we are working with here is called a vector, but my language in the above answer is a little awkward because I've avoided using almost all of the language of vectors while writing it. The word "normalize" slipped in. Look forward to a class in college called "linear algebra," which will make you an expert at this question.
你不能。
Rectangle
类只能保存平行于 x 和 y 轴的矩形。You can't.
Rectangle
class can hold only rectangles that are parallel to the x and y axes.