Cyrus Beck is a line clipping algorithm that is made for convex polygons. It allows line clipping for non-rectangular windows, unlike Cohen Sutherland or Nicholl Le Nicholl. It also removes the repeated clipping needed in Cohen Sutherland.
Input: 1. Convex area of interest which is defined by a set of coordinates given in a clockwise fashion. 2. vertices which are an array of coordinates: consisting of pairs (x, y) 3. n which is the number of vertices 4. A line to be clipped given by a set of coordinates. 5. line which is an array of coordinates: consisting of two pairs, (x0, y0) and (x1, y1) Output: 1. Coordinates of line clipping which is the Accepted clipping 2. Coordinates (-1, -1) which is the Rejected clipping
- Normals of every edge is calculated.
- Vector for the clipping line is calculated.
- Dot product between the difference of one vertex per edge and one selected end point of the clipping line and the normal of the edge is calculated (for all edges).
- Dot product between the vector of the clipping line and the normal of edge (for all edges) is calculated.
- The former dot product is divided by the latter dot product and multiplied by -1. This is ‘t’.
- The values of ‘t’ are classified as entering or exiting (from all edges) by observing their denominators (latter dot product).
- One value of ‘t’ is chosen from each group, and put into the parametric form of a line to calculate the coordinates.
- If the entering ‘t’ value is greater than the exiting ‘t’ value, then the clipping line is rejected.
- Case 1: The line is partially inside the clipping window:
0 < tE < tL < 1 where tE is 't' value for entering intersection point tL is 't' value for exiting intersection point
- Case 2: The line has one point inside or both sides inside the window or the intersection points are on the end points of the line:
0 ≤ tE ≤ tL ≤ 1
- Case 3: The line is completely outside the window:
tL < tE
First, calculate the parametric form of the line to be clipped and then follow the algorithm.
P0 - PEi
P1 - P0
Ni . (P0 - PEi) Ni . (P1 - P0) where i -> ith edge of the convex polygon
Ni . (P0 - PEi) t = ------------------ -(Ni . (P1 - P0))
Implementation: Here is an implementation of the above steps in SFML C++ Graphics Library. You can also press any key to unclip the line and press any key to clip the line.
(102, 50) (240, 109)
- Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
- Point Clipping Algorithm in Computer Graphics
- Polygon Clipping | Sutherland–Hodgman Algorithm
- Bresenham's Algorithm for 3-D Line Drawing
- Bresenham’s Line Generation Algorithm
- Mid-Point Line Generation Algorithm
- Anti-aliased Line | Xiaolin Wu's algorithm
- Comparions between DDA and Bresenham Line Drawing algorithm
- DDA Line generation Algorithm in Computer Graphics
- Klee's Algorithm (Length Of Union Of Segments of a line)
- Slope of the line parallel to the line with the given slope
- Reflection of a point about a line in C++
- Draw a line in C++ graphics
- Find the other end point of a line with given one end and mid
- Slope of perpendicular to line
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.