通过2点画一条无限线? [英] Draw an infinite line through 2 points?
问题描述
我正在寻找一种通过2个点画一条无限线(无尽的线,也称为射线)的方法.我可以使用Line2D在2点之间画一条线,这里没有问题.
I'm looking for a way to draw an infinite line (a line with no end, also known as a ray) through 2 points. I can draw a line between 2 points with Line2D, no problem here.
接下来,无限部分需要看一下.我想以简单的思路,将第二个点的x和y坐标乘以100,然后重画该线.这有效,但仅在简单情况下.
Next the infinite part needed a look. With my simple mind I thought, lets multiply the x and y coordinates from the second point with 100 and redraw the line. This works, but only in simple cases.
例如,在这种情况下,它会产生不同角度的线:
For example here is a case in which it produces lines with different angles:
g.setColor(Color.red);
g2.setStroke(new BasicStroke(4.0f));
g2.draw(new Line2D.Double(0, 61.632653061218946, 944, 217.25510204080692));
g.setColor(Color.blue);
g2.setStroke(new BasicStroke(1.0f));
g2.draw(new Line2D.Double(0, 61.632653061218946, 944*10, 217.25510204080692*10));
这将首先绘制一条红色的粗线,然后再绘制一条蓝色的细线.
This will first draw a fat red line, next it will draw a blue thin line.
与红线相比,蓝线具有不同的角度.以下是说明此效果的屏幕截图:
The blue line has a different angle compared the the red line. Here's a screenshot to illustrate this effect:
有人知道解决此问题的方法,还是一种更好的方法来通过2点画一条无限线?
Does somebody know a way to fix this, or maybe a better way to draw a infinite line through 2 points?
推荐答案
让我们进行一秒钟的数学运算.
Let's do the math for a second.
- 第一行是
(0, 61.632653061218946)-(944, 217.25510204080692)
.斜率是rise/run
,因此是m = 0.16485428917329234533898305084746
. - 第二行是
(0, 61.632653061218946)-(9440, 2172.5510204080692)
;m = 0.22361423382911549300847457627119
.
- The first line is
(0, 61.632653061218946)-(944, 217.25510204080692)
. Slope isrise/run
, thereforem = 0.16485428917329234533898305084746
. - The second line is
(0, 61.632653061218946)-(9440, 2172.5510204080692)
;m = 0.22361423382911549300847457627119
.
坡度不同,这只是表示角度不同的另一种方式.
您需要做的是扩展行.您不能只将一个点的两个坐标乘以10.首先确定画布边界之外的x或y,然后求解另一个值.
What you need to do is extend the line. You cannot just multiply both coordinates of one of the points by 10. First determine either an x or a y beyond the bounds of your canvas, then solve for the other value.
您如何做到的?
-
首先,获得直线的方程式.一条线由
y=m*x+b
定义,其中m
是斜率,而b
是y轴截距.
First, get the equation for the line. A line is defined by
y=m*x+b
, wherem
is the slope, andb
is the y-intercept.
- 我们已经知道如何计算斜率(
rise/run
=y2 - y1 / x2 - x1
).我们得到0.16485428917329234533898305084746
- 插入斜率并求解
b
(y - m*x
),得到61.632653061218946
.在您的情况下,您已经具有此值,因为x=0
时y截距是y坐标. - 然后得到等式
y = 0.16485428917329234533898305084746 * x + 61.632653061218946
- We already know how to calculate the slope (
rise/run
=y2 - y1 / x2 - x1
). We get0.16485428917329234533898305084746
- Plug in the slope and solve for
b
(y - m*x
), you get61.632653061218946
. In your case you already have this value as the y-intercept is the y-coordinate whenx=0
. - You then get the equation
y = 0.16485428917329234533898305084746 * x + 61.632653061218946
现在,选择一个足够大的x,例如10000.插入该值并求解y.您得到1710.1755447941423993898305084746
.
最后,将线画到这个新点(0, 61.632653061218946)-(10000,1710.1755447941423993898305084746)
Finally, draw your line to this new point, (0, 61.632653061218946)-(10000,1710.1755447941423993898305084746)
太好了,现在让我们概括一下.
Great, now let's generalize this.
- 我们有两个点
(x1, y1)
和(x2, y2)
.我们要解决(10000, y3)
. - 因此,
y3 = m*x3 + b
或y3 = m * 10000 + b
. - 我们也知道
b = y - m * x
,因此将其插入并任意选择点1,y3 = m * 10000 + y1 - m * x1
. - 好吧,我们将
m
排除在外:y3 = m * (10000 + x1) - y1
. - 我们知道
m = (y2 - y1) / (x2 - x1)
,因此将其插入:y3 = ((y2 - y1) / (x2 - x1)) * (10000 + x1) - y1
.
- We have two points
(x1, y1)
and(x2, y2)
. We want to solve for(10000, y3)
. - Therefore,
y3 = m*x3 + b
, ory3 = m * 10000 + b
. - We also know that
b = y - m * x
, so plugging this in and arbitrarily choosing point 1,y3 = m * 10000 + y1 - m * x1
. - Ok, let's factor out the
m
:y3 = m * (10000 + x1) - y1
. - We know
m = (y2 - y1) / (x2 - x1)
, so plugging this in:y3 = ((y2 - y1) / (x2 - x1)) * (10000 + x1) - y1
.
如果您的行不是从x = 0
开始,则需要为x = 0
重复此过程,这意味着您应该绘制一条(0, ((y2 - y1) / (x2 - x1)) * x1 - y1)-(10000,((y2 - y1) / (x2 - x1)) * (10000 + x1) - y1)
线.
If your line doesn't start at x = 0
, you will need to repeat this process for x = 0
, meaning you should plot a line (0, ((y2 - y1) / (x2 - x1)) * x1 - y1)-(10000,((y2 - y1) / (x2 - x1)) * (10000 + x1) - y1)
.
注意:如果x2 - x1
为0,则表示斜率无限大.这是一条垂直线,您必须单独处理这种情况.
Note: If x2 - x1
is 0, you have an infinite slope. This is a vertical line, and you'll have to handle this case separately.
这篇关于通过2点画一条无限线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!