简体   繁体   中英

Good way to represent a vertical lines in computational geometry?

I've been working on a computational geometry related project in C++ recently, and decided to try and write my own geometry classes/structures and functions as a practice in understanding how things in computational geometry work.

When I first started, I represented a Line by its slope and y-intercept (keeping in mind the general equation y = mx + c ). Soon I realized that representing vertical lines like this doesn't really work out. As a sort of quick fix I decided to include a boolean is_vertical along with another field vertical_x (for the position on the x-axis of the vertical line) which only gets used is is_vertical is true.

While this approach works for now, it gets quite tedious to have to write extra code to deal with the vertical condition when using the above specified representation of a 2d line. Is there a good/better way to represent vertical lines, or lines in general in computational geometry?

There is a lot of ways to represent lines in computer geometry.

Two main approaches:

As said in comments, general equation of line is

a * x + b * y - c = 0

if you have two points to define a line, then coefficients are

a = (y1 - y2)
b = (x2 - x1)
c = (x2 * y1 - x1 * y2)

This form is also convenient to determine - which side of line some point belongs to, and for finding line-point distance.

Parametric approach is also wide used. Line is defined by base point and direction vector

Base = P1
Dir = P2 - P1  (mignt be normalized to get unit length)

Note that component of normalized direction vector are essentially cosine and sine of Fi - angle between OX and the line.

Any point at the line might be described using parameter t

X(t) = X1 + t * Dir.X
Y(t) = Y1 + t * Dir.Y

在此处输入图片说明


There is also less-used Rho-Theta definition, which requires only two scalar parameters to define any line - normal distance from coordinate origin to line and angle between OX and that normal:

x * Sin(Theta) - y * Cos(Theta) + p = 0

在此处输入图片说明

As Mbo pointed out in their answer , there are several different ways to represent lines with equations.

A general equation for a line is of the form A * x + B * y = C .

If you know that a line is not vertical, then you can use a simplified form y = a * x + C (obtained from the previous equation by noting that B is nonzero, and letting a = - A/B ).

If you know that a line is vertical, you can use a simplified form x = c (by noting that B = 0 and A is nonzero and letting c = C/A ).

If you know that a line is not horizontal, then you can use a simplified form x = b * y + C .

If you know that a line is horizontal, then you can use a simplified form y = c .

Since you can have all those different representations, one way to deal with line in an object-oriented program is to write a virtual class Line , then several children classes GeneralEquationLine, VerticalLine, NonVerticalLine, ParametricRepresentationLine, etc.

Most functions that deal with lines do not need to know how lines are represented internally, so they expect an object of class Line . A few functions might be specific to non-vertical lines, and accordingly expect an object of class NonVerticalLine (or alternatively, the class Line might have a method isVertical that returns true or false).

You can also write functions to transform an instance of a children class to another, when possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM