Module: Line
- Included in:
- Curses::Geometry
- Defined in:
- lib/curses/geometry/line.rb
Constant Summary collapse
- BOTTOM_LEFT_TO_TOP_RIGHT_GLYPH =
CONSTANTS :
'/'- TOP_LEFT_TO_BOTTOM_RIGHT_GLYPH =
'\\'- HORIZONTAL_GLYPH =
'-'- VERTICAL_GLYPH =
'|'
Instance Method Summary collapse
- #draw_antialiased_line(x1, y1, x2, y2) ⇒ Object
-
#draw_horizontal_line(x1, x2, y) ⇒ Object
METHODS :.
- #draw_line(x1, y1, x2, y2) ⇒ Object
- #draw_vertical_line(x, y1, y2) ⇒ Object
Instance Method Details
#draw_antialiased_line(x1, y1, x2, y2) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/curses/geometry/line.rb', line 100 def draw_antialiased_line(x1, y1, x2, y2) # Set-up of the bresenham algorythm : x, y = x1, y1 dx, dy = x2 - x1, y2 - y1 x_increment = dx > 0 ? 1 : -1 y_increment = dy > 0 ? 1 : -1 # Edge cases : if dy == 0 then # horizontal line previous_stroke_glyph = get_stroke_glyph set_stroke_glyph HORIZONTAL_GLYPH draw_horizontal_line(x1, x2, y1) set_stroke_glyph previous_stroke_glyph return end if dx == 0 then # vertical line previous_stroke_glyph = get_stroke_glyph set_stroke_glyph VERTICAL_GLYPH draw_vertical_line(x1, y1, y2) set_stroke_glyph previous_stroke_glyph return end # Chosing the "stepping" glyph : if dx > 0 then step_glyph = dy > 0 ? TOP_LEFT_TO_BOTTOM_RIGHT_GLYPH : BOTTOM_LEFT_TO_TOP_RIGHT_GLYPH else step_glyph = dy > 0 ? BOTTOM_LEFT_TO_TOP_RIGHT_GLYPH : TOP_LEFT_TO_BOTTOM_RIGHT_GLYPH end # Scanning the line : dx, dy = dx.abs, dy.abs # First Point : tan = (dy.to_f / dx).abs if ( ( tan > 0.08 ) && ( tan < 5.67 ) ) then glyph = step_glyph else glyph = dx > dy ? HORIZONTAL_GLYPH : VERTICAL_GLYPH end setpos(y1, x1) addch(glyph) # Rest of the Line : if dx > dy then d = dx >> 1 dx.times do |i| x += x_increment d += dy if d >= dx then d -= dx; y += y_increment; setpos(y, x) addch(step_glyph) else ny = y_increment < 0 ? y : y + 1 setpos ny, x addch(HORIZONTAL_GLYPH) end end else d = dy >> 1 (dy - 1).times do |i| y += y_increment d += dx if d >= dy then d -= dy x += x_increment setpos(y, x) addch(glyph) else setpos(y, x) addch(VERTICAL_GLYPH) end end end end |
#draw_horizontal_line(x1, x2, y) ⇒ Object
METHODS :
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/curses/geometry/line.rb', line 11 def draw_horizontal_line(x1, x2, y) if x1 < x2 then min_x, max_x = x1, x2 else min_x, max_x = x2, x1 end glyph = get_graphic_context[:stroke_glyph] setpos(y, min_x) min_x.upto(max_x) do |x| addch(glyph) end end |
#draw_line(x1, y1, x2, y2) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/curses/geometry/line.rb', line 35 def draw_line(x1, y1, x2, y2) # Set-up of the bresenham algorythm : x, y = x1, y1 dx, dy = x2 - x1, y2 - y1 x_increment = dx > 0 ? 1 : -1 y_increment = dy > 0 ? 1 : -1 # Edge cases : if dy == 0 then # horizontal line draw_horizontal_line(x1, x2, y1) return end if dx == 0 then # vertical line draw_vertical_line(x1, y1, y2) return end # Scanning the line : dx, dy = dx.abs, dy.abs glyph = get_graphic_context[:stroke_glyph] # First Point : setpos(y1, x1) addch(glyph) # Rest of the Line : if dx > dy then d = dx >> 1 dx.times do |i| x += x_increment d += dy if d >= dx then d -= dx; y += y_increment; setpos(y, x) else ny = y_increment < 0 ? y : y + 1 setpos ny, x end addch(glyph) end else d = dy >> 1 dy.times do |i| y += y_increment d += dx if d >= dy then d -= dy x += x_increment end setpos(y, x) addch(glyph) end end end |
#draw_vertical_line(x, y1, y2) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/curses/geometry/line.rb', line 23 def draw_vertical_line (x, y1, y2) if y1 < y2 then min_y, max_y = y1, y2 else min_y, max_y = y2, y1 end glyph = get_graphic_context[:stroke_glyph] min_y.upto(max_y) do |y| setpos(y,x) addch(glyph) end end |