两点与任意宽度之间绘制矩形 [英] Drawing rectangle between two points with arbitrary width

查看:426
本文介绍了两点与任意宽度之间绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想画的两(2D)行指出,当用户扫描他们的手指在触摸屏上。要做到这一点,我计划在previous触摸更新最新的触摸更新的X和Y和X和Y之间的每一个接触的更新绘制一个矩形。这应该作为用户扫描他们的手指在屏幕上创建一个连续的实线。不过,我也希望此行有一个任意宽度。我的问题是,我应该如何去计算我需要为每个矩形(X1,Y1,X2,Y2)的坐标?

I'm trying to draw a line between two (2D) points when the user swipes their finger across a touch screen. To do this, I plan on drawing a rectangle on every touch update between the X and Y of the previous touch update and the X and Y of the latest touch update. This should create a continuous and solid line as the user swipes their finger across the screen. However, I would also like this line to have an arbitrary width. My question is, how should I go about calculating the coordinates I need for each rectangle (x1, y1, x2, y2)?

-

另外:如果任何人对我怎么可能那么去申请抗锯齿这一行它会是一个巨大的帮助的任何信息

Also: if anyone has any information on how I could then go about applying anti-aliasing to this line it'd be a massive help.

推荐答案

计算起点和终点之间的矢量

Calculate a vector between start and end points

V.X := Point2.X - Point1.X;
V.Y := Point2.Y - Point1.Y;

然后计算出一个垂直于它(只是换X和Y坐标)

Then calculate a perpendicular to it (just swap X and Y coordinates)

P.X := V.Y; //Use separate variable otherwise you overwrite X coordinate here
P.Y := -V.X; //Flip the sign of either the X or Y (edit by adam.wulf)

正常化的垂直

Normalize that perpendicular

Length = sqrt(P.X * P.X + P.Y * P.Y); //Thats length of perpendicular
N.X = P.X / Length;
N.Y = P.Y / Length; //Now N is normalized perpendicular

计算4点形成通过添加归垂直和它的一半所需宽度的相乘的矩形

Calculate 4 points that form a rectangle by adding normalized perpendicular and multiplying it by half of the desired width

R1.X := Point1.X + N.X * Width / 2;
R1.Y := Point1.Y + N.Y * Width / 2;
R2.X := Point1.X - N.X * Width / 2;
R2.Y := Point1.Y - N.Y * Width / 2;
R3.X := Point2.X + N.X * Width / 2;
R3.Y := Point2.Y + N.Y * Width / 2;
R4.X := Point2.X - N.X * Width / 2;
R4.Y := Point2.Y - N.Y * Width / 2;

使用这4个点来绘制矩形。

Draw rectangle using these 4 points.

下面的图片:

编辑::要回答这个意见:如果X和Y是相同的,那么该行正是对角线垂直对角线是一个对角。规范化是一种制造长度的方法,以等于1,从而使你的线的在本实施例的宽度将不依赖于垂线的长度(它等于线长度在这里)。

To answer the comments: If X and Y are the same then the line is exactly diagonal and perpendicular to a diagonal is a diagonal. Normalization is a method of making a length to equal to 1, so that the width of your line in this example will not depend on perpendiculars length (which is equal to lines length here).

这篇关于两点与任意宽度之间绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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