1 Introduction

Intersection algorithms in \(E^2\) and \(E^3\) have been intensively studied for a long time as they play important roles in many applications. In computer graphics, clipping algorithms have been deeply studied and fundamental algorithms have been described [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]. A survey of algorithms is given in [22].

Clipping algorithms in the \(E^2\) space mainly deal with a line or a line segment clipping by convex, non-convex non-intersecting polygons, and non-convex self-intersecting polygons [22, 23]. Clipping algorithms in the \(E^3\) space solve the intersection problem of line or line segments with convex and non-convex polyhedrons [22]. In practice, polyhedrons are represented by triangular meshes.

A deep survey of clipping and intersection algorithms in \(E^2\) and \(E^3\) spaces with an extensive list of references was published [22]. It contains over 230 references to known published papers related to clipping in \(E^2\) and \(E^3\).

Fig. 1
figure 1

Projective extension and dual space

The majority of algorithms have been developed for the Euclidean case. Few algorithms use homogeneous coordinates and the projective extension of Euclidean space or the principle of duality [24,25,26,27,28].

In the \(E^2\) case, algorithms deal with an intersection of a line or a half-line (ray) or a line segment with a 2D geometric entity, e.g., a rectangle [29,30,31,32], convex polygon [33,34,35], non-convex polygon [36], quadric and cubic curves, parametric curves [37] and areas with quadratic arcs [23, 38]. The famous line clipping algorithms are:

  • the Cohen–Sutherland algorithm [29] for a line segment clipping by a rectangular window; more efficient coding was described in [39],

  • the Cyrus–Beck’s line clipping algorithm by a convex polygon in \(E^2\) and its modification for line clipping by a convex polyhedron in \(E^3\) of the O(N) complexity [33, 34, 40].

However, the test if a line intersects a convex polygon in \(E^2\) is dual to the test point-in-convex polygon, which has the optimal complexity \(O(\lg {N})\).

It means that the optimal complexity of the line clipping by a convex polygon algorithm is \(O(\lg {N})\) in the \(E^2\) case.

1.1 Homogeneous coordinates

The projective extension of Euclidean space is not a part of standard computer science courses. However, homogeneous coordinates are used in computer graphics and computer vision algorithms, as they enable the representation of geometric transformations like translation and rotation by matrix multiplication and also offer to represent a point in infinity.

The mutual conversion between Euclidean space and projective space in the \(E^2\) case is given as:

$$ \begin{aligned} \begin{aligned} X = \frac{x}{w} \quad , \quad Y = \frac{y}{w} \quad \& \quad w \ne 0 \end{aligned} \end{aligned}$$
(1)

where \(\textbf{X}=(X,Y)\), resp. \(\textbf{x}=[x,y:w]^T\), are coordinates in Euclidean space \(E^2\), resp. in projective space \(P^2\). The extension to the \(E^3\) case is straightforward.

The geometrical interpretation of Euclidean and projective spaces is presented in Fig. 1.

If the line p is given by two points \(\textbf{X}_A=(X_A,Y_A)\) and \(\textbf{X}_B=(X_B,Y_B)\) in Euclidean space, resp. \(\textbf{x}_A=[x_A,y_A:w_A]^T\) and \(\textbf{x}_B=[x_B,y_B:w_B]^T\) & \(w_A,w_B \ne 0\), the coefficients of the line p in \(E^2\) can be determined as [41]:

$$\begin{aligned} \begin{aligned} \begin{vmatrix} a&b&c \\ X_A&Y_A&1 \\ X_B&Y_B&1 \end{vmatrix} = 0 \quad \text {, or } \quad \begin{vmatrix} a&b&c \\ x_A&y_A&w_A \\ x_B&y_B&w_B \end{vmatrix} = 0 \end{aligned} \end{aligned}$$
(2)

It can be rewritten in more compact form as:

$$\begin{aligned} \begin{aligned} \textbf{p} = \textbf{x}_A \times \textbf{x}_B = \textbf{x}_A \wedge \textbf{x}_B \end{aligned} \end{aligned}$$
(3)

where \(\textbf{x} =[x,y:w]^T\), \(\textbf{p} = [a,b:c]^T\) and \(\wedge \) is the outer product formally equivalent to the cross-product in this case.Footnote 1

The projective extension of Euclidean space enables the use of the principle of duality [25, 42, 43] in the intersection computation of two lines \(p_1\) and \(p_2\) in the \(E^2\) case using the outer product:

$$\begin{aligned} \begin{aligned} p_1: a_1 + b_1 y + c_1 = 0 \quad ,\quad p_2: a_2 + b_2 y + c_2 = 0 \\ \textbf{x}= \textbf{p}_1 \wedge \textbf{p}_2 =\\ \begin{vmatrix} \textbf{e}_1&\textbf{e}_2&\textbf{e}_w \\ a_1&b_1&c_1 \\ a_2&b_2&c_2\\ \end{vmatrix} \\ = [b_1 c_2 - b_2 c_1, -(a_1 c_2 - a_2 c_1):a_1 b_2 - a_2 b_1]^T\\ =[x,y:w]^T\\ \end{aligned} \end{aligned}$$
(4)

where \(\textbf{e}_i\) are basis vector of projective space \(P^2\), i.e., xyw coordinates.

It is due to the duality of lines and points in projective space [25, 42,43,44,45,46,47].

The outer product \(\textbf{x}_A \wedge \textbf{x}_B\) is formally equivalent to the cross-product \(\textbf{x}_A \times \textbf{x}_B \) in the \(P^2\) case. The non-normalized normal vector of the line p is \(\textbf{n}=(a,b) \triangleq [a,b:0]^T\) and \(c_i\) is the pseudo-distance of the line p from the origin of the coordinate system. (The symbol \(\triangleq \) means projectively equivalent.)

1.2 Principle of duality

The principle of duality is essential in general. Its application in geometry in connection with the implicit representation using projective geometry brings some new formulations or even new ones [25, 48,49,50,51]. The duality principle for basic geometric entities and operators is presented in Tables 1 and 2.

Table 1 Duality of geometric entities

In the \(E^2\) case, a point is dual to a line and vice versa. The intersection of two lines is dual to a union of two points, i.e., a line given by two points, similarly for the \(E^3\) case.

Table 2 Duality of operators

1.3 Point-in-convex polygon test

The point-in-convex polygon test seems to be quite simple, and probably the first description was given in [52, 53]. The algorithms are simple with O(N) computational complexity, see Fig. 2. They are based on the half-plane tests, i.e., if the given point lies inside all half-planes, which form the given convex polygon and which can be given in an arbitrary order.

However, if the edges, resp. vertices, are ordered in the clockwise or anti-clockwise order, the point-in-convex polygon test has optimal complexity \(O(\lg {N})\), as binary search over vertex indexes can be used. Algorithms can also be found in [54,55,56,57], etc.

Fig. 2
figure 2

Convex polygon in \(E^2\) with the centroid \(\textbf{x}_T\) and tested point \(\textbf{x} \equiv \varvec{\xi }\)

There are two basic approaches:

  • An implicit formulation \(F_i(\textbf{x})=0\), resp. \(F_i(\textbf{X})=0\), is used to represent polygon edges \(e_i\), see Eq. 5:

    $$ \begin{aligned} \begin{aligned}&F_i(\textbf{X}) = a_i X + b_i Y +c_i \quad \text {in Euclidean space}\, E^2 \\&F_i(\textbf{x})= a_i x + b_i y + c_i w = \textbf{a}_i^T\textbf{x} \quad \text {in projective space}\, P^2 \\&X = \frac{x}{w} \quad , \quad Y = \frac{y}{w} \quad \& \quad w \ne 0 \end{aligned} \end{aligned}$$
    (5)

    and the half-plane test is defined as

    $$ \begin{aligned} \begin{aligned}&F(\textbf{X}) = a_i X + b_i Y +c_i \ge 0 \quad \text {in Euclidean space} E^2 \\&F(\textbf{x}) = a_i x + b_i y + c_i w = \textbf{a}_i^T\textbf{x} \ge 0 ~ \& ~ w \ge 0 \quad \\&\text {in projective space} P^2 \end{aligned} \end{aligned}$$
    (6)

    If the edge \(e_i\) is given by vertices \(\textbf{x}_i\) and \(\textbf{x}_j = \textbf{x}_{i \oplus 1}\), then the coefficients \(\textbf{a}_i=[a_i,b_i:c_i]^T\) can be easily computed using the outer product.Footnote 2

    $$\begin{aligned} \begin{aligned} \textbf{a}_i= \textbf{x}_i \wedge \textbf{x}_j = \textbf{x}_i \times \textbf{x}_j = \begin{vmatrix} \textbf{e}_1&\textbf{e}_2&\textbf{e}_w \\ x_i&y_i&w_i \\ x_j&y_j&w_j\\ \end{vmatrix} ~,\\~ i=0,\ldots N-1 ~,~ j= i \oplus 1\\ \end{aligned} \end{aligned}$$
    (7)

    where \(\textbf{e}_1\), \(\textbf{e}_2\), \(\textbf{e}_w\) form the orthonormal vector basis in projective space \(P^2\) and \(\oplus \) means \({\textbf {mod }}N\) addition operation. Then, the coefficients \(\textbf{a}_i\) of the line \(p_i\) on which the edge \(e_i\) lies can be expressed as:

    $$\begin{aligned} \begin{aligned} \begin{vmatrix} a_i&b_i&c_i \\ x_i&y_i&w_i \\ x_j&y_j&w_j \end{vmatrix} = 0 \quad \text { or } \quad \begin{vmatrix} a_i&b_i&c_i \\ X_i&Y_i&1 \\ X_j&Y_j&1 \end{vmatrix} = 0 \end{aligned} \end{aligned}$$
    (8)

    where \(\textbf{a}_i=\textbf{x}_i \wedge \textbf{x}_j\), \(\textbf{x}_j=[x_i,y_i:w_i]^T, w_i\ne 0\) and \(\textbf{x}_j=[x_j,y_j:w_j]^T, w_j\ne 0\), using homogeneous coordinates.Footnote 3 This enables an easy extension of algorithms for the case when the polygon vertices are given in projective space.

  • The edges are represented as vectors in Euclidean space, i.e., the edge \(e_i\) given by points \(\textbf{X}_i\) and \(\textbf{X}_j\) is represented by a vector \(\textbf{s}_i=\textbf{X}_j-\textbf{X}_i\). In this case, the computation is restricted to Euclidean space representation.

However, there are other factors to be considered if an algorithm is to be used in practical applications:

  • Is the convex polygon given as an unordered set of edges, or are they ordered consequently? If ordered, are the edges oriented clockwise or anti-clockwise? If not ordered, are normal vectors oriented consistently?

  • Can preprocessing be beneficial to use? If more points are to be tested against the same convex polygon, some properties might be pre-computed, e.g., directional vectors \(\textbf{s}_i\), coefficients of the functions \(F_i(\textbf{x})\), etc. An extreme pre-computation case leads to O(1) run-time complexity [56, 58] with O(M N) preprocessing complexity, where M depends on the polygon’s geometric properties.

  • Is the centroid of the polygon or its estimation \(\textbf{x}_T\) given? It might be beneficial in some algorithms.

  • Efficiency of some algorithms might be sensitive to the ratio \(\frac{Area_{polygon}}{Area_{data~domain}}\).

  • Invariance factor? Is the algorithm invariant to translation and rotation?

Due to the principle of duality in \(E^2\), resp. \(P^2\), a point is dual to a plane and vice versa. Therefore, the point-in-convex polygon test is dual to a line convex polygon intersection algorithm (line clipping) test with \(O(\lg {N})\) complexity.

In the following, a new line convex polygon intersection (line clipping) algorithm with \(O(\lg {N})\) complexity in \(E^2\), resp. \(P^2\), is described.

2 Convex polygon clipping

The simplest case in the \(E^2\) case is a line clipping by a convex polygon. Algorithms used are of the O(N) complexity [31,32,33,34] and require oriented edges, i.e., clockwise or anticlockwise orientation. The edges can be given in an arbitrary order with consistently oriented normals.

Fig. 3
figure 3

Cyrus–Beck clipping algorithm against the convex polygon in \(E^2\)

Algorithm 1
figure b

Cyrus–Beck’s line convex polygon clipping algorithm

The Cyrus–Beck(CB) [33] is the algorithm for a line clipping by a convex polygon with O(N) complexity, see Algorithm 1 and Fig. 3, It does not use the usual property of the consecutive order of vertices, i.e., edges. The algorithm is extensible for a line clipping by a convex polyhedron in the \(E^3\) case [59], where no specific order of faces is possible. It should be noted that the algorithm for a line clipping by a convex polygon with O(1) run-time complexity using preprocessing was described in [60].

The CB algorithm is based on a parametric formulation of the clipped line and an implicit formulation of lines of the convex polygon edges, see Eq. 9.

$$\begin{aligned} \begin{aligned}&p:~ \textbf{X} = \textbf{X}_A + \textbf{s}~t \\&e_i:~\textbf{n}_i^T \textbf{X} + c_i = 0 \quad , \quad i=0,\ldots ,N-1 \end{aligned} \end{aligned}$$
(9)

where \(\textbf{X}_A =(X_A,Y_A)\), \(\textbf{s}=(s_X,s_Y)\) is the directional vector of the clipped line p, \(\textbf{n}_i=(a_i,b_i)\) is the normal vector of the edge \(e_i\)Footnote 4 which lies on a line \(\textbf{p}_i= [a_i,b_i:c_i]^T\) given in the implicit form as \(a_i X + b_i Y + c_i = 0\).

Solving Eq. 9, the values \(t_0,\ldots ,t_{N-1}\) are computed and compared. However, \(N-2\) values are not used in the final processing, as the given line p can have up to 2 intersections with a convex polygon. It leads to significant computational inefficiency.

In most cases, the convex polygons in \(E^2\) are given by edges, resp. vertices, with a clockwise or anti-clockwise orientation. Such property is so significant that such knowledge should lead to algorithm complexity decrease. The orientation of edges, together with the convex property of the polygon, leads to the algorithm with \(O(\lg {N})\) complexity described [61].

The algorithm expects known knowledge of the convex polygon orientation, i.e., if the vertices are clockwise or anti-clockwise ordered. Also, it is not easy to correctly implement the CB algorithm, due to the close singular or singular cases solution, see Algorithm 1, line 10–11, as the value of \(\varepsilon \) is the programmer’s choice. Also, the vertices and the line are expected to be given in Euclidean space; otherwise, they must be converted

In the following, a new simple and robust algorithm for a line clipping by a convex polygon with \(O(\lg {N})\) in projective space \(P^2\), i.e., in the projective extension of Euclidean space \(E^2\), is given. It does not require prior knowledge of the convex polygon orientation, as the formulation uses the projective extension of Euclidean space \(P^2\). Due to the projective formulation, the vector–vector operations can be used, which is convenient for SSEFootnote 5 or AVXFootnote 6 vector–vector instructions use or in an implementation on GPUs.

It also eliminates close-to-singular cases caused by division operations.

3 Proposed algorithm

Let us consider fundamental situations in line clipping by a convex polygon, see Fig.4, where the convex polygon is represented by a circle.

Fig. 4
figure 4

Line clipping by a convex polygon

Table 3 Table of possible cases

Let three polygon vertices be given as:

  • the vertex \(\textbf{P}_a\) has coordinates \(\textbf{x}_0\),

  • the vertex \(\textbf{P}_b\) has coordinates \(\textbf{x}_k\), where \(k=\lfloor N/4 \rfloor \),

  • the vertex \(\textbf{P}_c\) has coordinates \(\textbf{x}_k\), where \(k=\lfloor N/2 \rfloor \).Footnote 7

where N is the number of edges of the given convex polygon.Footnote 8

The algorithm [61, 64] uses only two polygon vertices, but complex decision logic requires known vertices orientation and is limited to Euclidean space only. The proposed algorithm uses three vertices instead, is fully in projective space and uses vector–vector operations, instead.

It leads to a simple solution as four possible situations have to be solved, see Fig. 4:

  • \(case_0\)–complex case, as the line does not intersect the convex polygon or the line can intersect the edges of the segment \(a-b\) or \(b-c\) or \(c-a\),

  • \(case_1\) the line intersects the edges of the segments \(c-a\) and \(b-c\),

  • \(case_2\) the line intersects the edges of the segments \(a-b\) and \(b-c\),

  • \(case_3\) the line intersects the edges of the segments \(a-b\) and \(c-a\) (not shown in Fig. 4),

The given line p is represented in the implicit form \(F(\textbf{X})=0\) in Euclidean space, resp. \(F(\textbf{x})=0\), if the homogeneous coordinates are used as [6, 27, 28, 65, 66]:

$$ \begin{aligned} \begin{aligned}&p:~ aX + bY + c = 0 \quad \text {, resp.} \quad \\&p:~ ax + by + cw = 0 \quad \& \quad w \ne 0 \\&X = \frac{x}{w} \quad , \quad Y = \frac{y}{w} \quad \& \quad w \ne 0 \end{aligned} \end{aligned}$$
(10)

The function \(F(\textbf{X})=0\), resp. \(F(\textbf{x})=0\), splits the \(E^2\) plane into two half-planes, i.e., given as \(F(\textbf{X})<0\), resp. \(F(\textbf{x})<0\), and \(F(\textbf{X}) \ge 0\), resp. \(F(\textbf{x}) \ge 0\). Then, the code \(C_k\) for the vertex \(P_k\) of the convex polygon is given as:

$$\begin{aligned} C_k = {\left\{ \begin{array}{ll} 1,&{} \text {if } F(\textbf{x}_k) \ge 0\\ 0, &{} \text {otherwise} \end{array}\right. } \end{aligned}$$
(11)

Table 3 presents all possible cases in Fig. 4.

As the line p is not oriented, the table is "symmetrical" and represents four fundamental cases as the cases \((case_3,case_4)\), \((case_2,case_5)\), \((case_1,case_6)\) and \((case_0, case_7)\) are equivalent.

It can be seen that simple logic expressions determine the segments intersected by the line p uniquely, except for one special non-simple case, i.e., \(case_0\) and \(case_7\).

In the cases \(case_1-case_3\) and \(case_4-case_7\), two segments are intersected. The intersections can be computed independently, e.g., in parallel, using a binary search over ordered vertices indexes with computational complexity \(O(\lg {N})\).

The cases, represented by \(case_0\) and \(case_7\), are more complex, as they cover two possible situations, i.e., the line p does not intersect the convex polygon, or it intersects a segment of the polygon edges, see Fig. 4.

However, the cases are easy to distinguish by finding a position of the point Q, defined as the intersection of the line p and an orthogonal line to the line p passing the vertex \(P_b\). If the points Q and \(P_a\) do not lie in the same half-plane, i.e., codes of the point \(C_Q \ne C_b\), then there is a possible intersection only on the segment \(c-a\) and the process is restarted with this segment only.

It can be seen that the SQ_CLIP algorithm, see Algorithm 2, for a line convex polygon intersection (line clipping) in the \(E^2\) case, is simple. The given description is recursive, as a non-recursive version is not so simple for explanation.

It should be noted that the algorithm uses projective representation for the polygon vertices and the given line p. In the case of Euclidean space, some operations can be simplified as the homogeneous coordinate \(w=1\). The proposed algorithm can be implemented as a non-recursive algorithm.

Algorithm 2
figure c

SQ_CLIP algorithm for line-convex polygon clipping in \(E^2\) with O(lg N) complexity

4 Conclusion

A new algorithm for line clipping by a convex polygon in \(E^2\) with \(O(\lg {N})\) complexity has been described. It uses the usual assumption that vertices of the convex polygon are consequently arranged, which is a significant property. The proposed algorithm is based on a simple idea of splitting polygon edges into three segments and binary search over indexes of the vertices; intersections for segments can be solved in parallel. The presented algorithm does not require prior knowledge of the polygon orientation. It uses implicit formulation and is fully projective using homogeneous coordinates.

The proposed algorithm:

  • does not compute temporary lines as in [56, 58] and uses only the inner product (dot product) to test a vertex position against the given line, and the outer product (cross-product) is only used for the final intersection computation. It should be emphasized that both operations take only one clock on the GPU,

  • does not require any division operation, except in the case, when final intersection points have to be converted to Euclidean space,

  • is more numerically robust as it does not need to solve singular or close-to-singular cases using the IF \(|q| < \varepsilon \) statement, as the Cyrus-Beck’s algorithm does,

  • it eliminates \(N-2\) parameter \(t_i\) computations in the CB algorithm,

  • is convenient for implementation using vector–vector operations using SSE, or AVX or implementation on GPUs,

  • is not convenient for triangles (a trivial case); in the case of 4-sided polygons, the algorithm [30] is to be used.

The proposed algorithm is not directly extensible for a line clipping by a convex polyhedron in \(E^3\), as no "ordering" of faces (triangles) is defined in the \(E^3\) case.

However, a line clipping algorithm with the expected \(O_{exp}(\sqrt{N})\) complexity was described in [40] if a triangular mesh gives the convex polyhedron.

It should be noted that a description of relevant intersection (clipping) algorithms and their modifications can be found in the extensive survey [22].