Ana Ramirez
Ana Ramirez

Reputation: 13

Given two points inside a window frame (x0,y0 and x1,y1) - What method would you used to extend a line to the edges of the window?

For an introduction computer graphics paper I am struggling to extend a line defined by two points using the same gradient to the edge of the window. It's quite hard to explain so this image will likely help.

IMG

The dotted line is the part not defined by the line (x0,y0 to x1,y1).

We have tried a few things using the implicit equation of a line (ax+bx+c = 0) and finding an intersection point with the all four edges of the window frame.

Any help is much appreciated!

Upvotes: 1

Views: 854

Answers (3)

MBo
MBo

Reputation: 80285

When you have implicit equation

ax + by + c = 0

and window edges coordinates Left, Top, Right, Bottom (Left and Top might be zero), then you can substitute these coordinates into equation and check that calculated point doesn't exceed window's border.

For example, to get intersection with right edge, substitute Right x-coordinate

a * Right + b * y + c = 0
if b = 0 then
   no intersection (vertical)  
else
   y =  (a * Right + c ) / b    
check whether Top <= y <= Bottom

Upvotes: 0

fabian
fabian

Reputation: 82471

You could come up with the normal form of the line equation:

n = (y1-y0, x0-x1)

(n is the direction from one point to another rotated by 90°)

Equation for points p on the line:

p * n = (x0, y0) * n

or

(p - (x0, y0)) * n = 0

* denotes the scalar product above, but not below.

so the equation to solve is

(px - x0) * (y1 - y0) + (py - y0) * (x0 - x1) = 0

All coordinated but px and py (the x and y coordinates of the point) are given, and one of px and py is determined by the side you want to intersect. Beware of divisions by 0 however, if you're solving this equation (horizontal/vertical lines).

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234795

Your first job is to check that (x0, y0) and (x1, y1) are distinct points.

The implicit equation of a non-vertical line is actually y = mx + c where the gradient m is (y1 - y0) / (x1 - x0), and c is the interception on the y axis, which can be found by applying c = y1 - mx1 or c = y0 - mx0.

Once you have the values of m and c, you can apply the line equation to find the y points given your set of x points (or vice-versa), in order to plot the line to the desired length.

A vertical line is characterised by x0 being equal to x1. In that case, your plot is simply a line that crosses the x axis at x0.

Also note that the line will only ever intercept with 2 edges (unless the line is collinear with one of the edges).

Upvotes: 1

Related Questions