通过2点画一条无限线? [英] Draw an infinite line through 2 points?

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

问题描述

我正在寻找一种通过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 is rise/run, therefore m = 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.

您如何做到的?

  1. 首先,获得直线的方程式.一条线由y=m*x+b定义,其中m是斜率,而b是y轴截距.

  1. First, get the equation for the line. A line is defined by y=m*x+b, where m is the slope, and b is the y-intercept.

  1. 我们已经知道如何计算斜率(rise/run = y2 - y1 / x2 - x1).我们得到0.16485428917329234533898305084746
  2. 插入斜率并求解b(y - m*x),得到61.632653061218946.在您的情况下,您已经具有此值,因为x=0时y截距是y坐标.
  3. 然后得到等式y = 0.16485428917329234533898305084746 * x + 61.632653061218946
  1. We already know how to calculate the slope (rise/run = y2 - y1 / x2 - x1). We get 0.16485428917329234533898305084746
  2. Plug in the slope and solve for b (y - m*x), you get 61.632653061218946. In your case you already have this value as the y-intercept is the y-coordinate when x=0.
  3. 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 + by3 = 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, or y3 = 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屋!

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