Draw an infinite line through 2 points?

后端 未结 2 873
情深已故
情深已故 2021-01-21 07:37

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.

2条回答
  •  盖世英雄少女心
    2021-01-21 07:51

    By multiplying the second point by 10, you're not maintaining the x to y ratio. You need to subtract the origin before multiplying.

    g2.draw(new Line2D.Double(0, 61.632653061218946, 944*10, (217.25510204080692-61.632653061218946)*10+61.632653061218946)); should work.

    In general, g2.draw(new Line2D.Double(x0,y0,(x1-x0)*k+x0,(y1-y0)*k+y0) should work.

    The math behind this is to consider the two points as vectors (a & b). Now, define a third and fourth vector: the difference vector (d) and the infinite end point vector (c). The difference vector is simply the difference between b and a; that is d = b-a. To find an infinite end point, we simply need to add infinity times the difference vector to the starting point (a). So, c = a + d*k, where k is the arbitrarily large constant. After replacing d, we have c = a+(b-a)*k.

提交回复
热议问题