[英]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.我最近一直在用 C++ 做一个计算几何相关的项目,并决定尝试编写我自己的几何类/结构和函数,作为理解计算几何如何工作的实践。
When I first started, I represented a Line by its slope and y-intercept (keeping in mind the general equation y = mx + c
).刚开始时,我用斜率和 y 截距表示一条线(记住一般方程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.作为一种快速修复,我决定包含一个布尔值is_vertical
以及另一个字段vertical_x
(用于垂直线的 x 轴上的位置),它只被使用is_vertical
为真。
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.虽然这种方法目前有效,但在使用上面指定的 2d 线表示时,必须编写额外的代码来处理垂直条件,这变得非常乏味。 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.请注意,归一化方向矢量的分量本质上是 Fi 的余弦和正弦 - OX 和线之间的角度。
Any point at the line might be described using parameter t
线上的任何点都可以使用参数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:还有一个较少使用的Rho-Theta定义,它只需要两个标量参数来定义任何线 - 从坐标原点到线的法线距离以及 OX 与该法线之间的角度:
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.正如 Mbo 在他们的回答中指出的那样,有几种不同的方法可以用方程来表示直线。
A general equation for a line is of the form A * x + B * y = C
.一条线的一般方程的形式为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
).如果您知道一条线不是垂直的,那么您可以使用简化形式y = a * x + C
(通过注意到B
非零并让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
).如果您知道一条线是垂直的,则可以使用简化形式x = c
(注意B = 0
且A
非零并让c = C/A
)。
If you know that a line is not horizontal, then you can use a simplified form x = b * y + C
.如果您知道一条线不是水平线,那么您可以使用简化形式x = b * y + C
。
If you know that a line is horizontal, then you can use a simplified form y = c
.如果您知道一条线是水平的,那么您可以使用简化形式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.由于您可以拥有所有这些不同的表示,因此在面向对象的程序中处理 line 的一种方法是编写一个虚拟类Line
,然后编写几个子类 GeneralEquationLine、VerticalLine、NonVerticalLine、ParametricRepresentationLine 等。
Most functions that deal with lines do not need to know how lines are represented internally, so they expect an object of class Line
.大多数处理线条的函数不需要知道线条在内部是如何表示的,因此它们需要一个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).一些函数可能特定于非垂直线,因此需要类 NonVerticalLine 的对象(或者,类Line
可能有一个返回 true 或 false 的方法isVertical
)。
You can also write functions to transform an instance of a children class to another, when possible.如果可能,您还可以编写函数将子类的实例转换为另一个实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.