Module: Tabula::CohenSutherlandClipping
- Defined in:
- lib/tabula/algorithms/cohen_sutherland_clipping.rb
Overview
Cohen-Sutherland line clipping algorithm. Clips a line segment to a rectangular region.
Constant Summary collapse
- INSIDE =
Region codes for Cohen-Sutherland algorithm
0b0000- LEFT =
0b0001- RIGHT =
0b0010- BOTTOM =
0b0100- TOP =
0b1000
Class Method Summary collapse
-
.clip(ruling, rect) ⇒ Ruling?
Clip a ruling to a rectangular region.
Class Method Details
.clip(ruling, rect) ⇒ Ruling?
Clip a ruling to a rectangular region
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tabula/algorithms/cohen_sutherland_clipping.rb', line 19 def clip(ruling, rect) x1 = ruling.x1 y1 = ruling.y1 x2 = ruling.x2 y2 = ruling.y2 min_x = rect.left max_x = rect.right min_y = rect.top max_y = rect.bottom code1 = compute_code(x1, y1, min_x, max_x, min_y, max_y) code2 = compute_code(x2, y2, min_x, max_x, min_y, max_y) loop do # Both endpoints inside - trivially accept return Ruling.new(x1, y1, x2, y2) if (code1 | code2).zero? # Both endpoints share an outside region - trivially reject return nil if (code1 & code2).nonzero? # At least one endpoint is outside, select it code_out = code1.nonzero? ? code1 : code2 # Find intersection point x, y = find_intersection(x1, y1, x2, y2, code_out, min_x, max_x, min_y, max_y) # Replace the outside point if code_out == code1 x1 = x y1 = y code1 = compute_code(x1, y1, min_x, max_x, min_y, max_y) else x2 = x y2 = y code2 = compute_code(x2, y2, min_x, max_x, min_y, max_y) end end end |