Class: Ruby2D::Line

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/ruby2d/line.rb

Overview

A line between two points.

Constant Summary

Constants included from Interactive

Interactive::OBJECT_EVENTS, Interactive::OBJECT_EVENT_FILTER_PREDICATES

Instance Attribute Summary collapse

Attributes included from Renderable

#color, #padding_bottom, #padding_left, #padding_right, #padding_top, #visible, #x, #x_align, #y, #y_align, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Renderable

#_alignment_anchor_dx, #_alignment_anchor_dy, #_apply_padding, #_extract_alignment, #_point_in_polygon?, #_point_on_segment?, #_require_numeric_position, #_resolve_alignment, #_unrotate, #_validate_dimensions, #add, #colour=, flatten_color, flatten_per_vertex, flatten_points, flatten_resolved_color, #hide, #opacity, #opacity=, #padding=, #remove, resolve_color_or_default, resolve_single_color, #show

Methods included from Interactive

#_fire_event, #interactive?, #off, #on

Constructor Details

#initialize(x1: 0, y1: 0, x2: 100, y2: 100, points: nil, z: 0, stroke_width: 1, dash: 0, gap: 5, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil, add: true, visible: true) ⇒ Line

Create a line. Pass dash: (and optionally gap:) to draw a dashed line. Specify endpoints via points: or via x1:/y1:/x2:/y2:. points: takes precedence if both are given.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby2d/line.rb', line 13

def initialize(x1: 0, y1: 0, x2: 100, y2: 100,
               points: nil,
               z: 0,
               stroke_width: 1, dash: 0, gap: 5,
               rotate: 0, rx: nil, ry: nil,
               color: nil, colour: nil, opacity: nil,
               add: true, visible: true)
  if points
    Line.send(:validate_points, points)
    x1, y1 = points[0]
    x2, y2 = points[1]
  end
  @x1 = x1
  @y1 = y1
  @x2 = x2
  @y2 = y2
  @z = z
  @stroke_width = stroke_width
  @dash = dash
  @gap = gap
  @rotate = rotate
  @_user_rx = rx
  @_user_ry = ry
  self.color = color || colour || 'white'
  self.opacity = opacity unless opacity.nil?
  @visible = visible
  self.add if add
end

Instance Attribute Details

#dashObject

Returns the value of attribute dash.



8
9
10
# File 'lib/ruby2d/line.rb', line 8

def dash
  @dash
end

#gapObject

Returns the value of attribute gap.



8
9
10
# File 'lib/ruby2d/line.rb', line 8

def gap
  @gap
end

#rotateObject

Returns the value of attribute rotate.



8
9
10
# File 'lib/ruby2d/line.rb', line 8

def rotate
  @rotate
end

#stroke_widthObject

Returns the value of attribute stroke_width.



8
9
10
# File 'lib/ruby2d/line.rb', line 8

def stroke_width
  @stroke_width
end

#x1Object

Returns the value of attribute x1.



8
9
10
# File 'lib/ruby2d/line.rb', line 8

def x1
  @x1
end

#x2Object

Returns the value of attribute x2.



8
9
10
# File 'lib/ruby2d/line.rb', line 8

def x2
  @x2
end

#y1Object

Returns the value of attribute y1.



8
9
10
# File 'lib/ruby2d/line.rb', line 8

def y1
  @y1
end

#y2Object

Returns the value of attribute y2.



8
9
10
# File 'lib/ruby2d/line.rb', line 8

def y2
  @y2
end

Class Method Details

.render(x1: 0, y1: 0, x2: 100, y2: 100, points: nil, stroke_width: 1, dash: 0, gap: 5, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil) ⇒ Object

Render a line without creating an instance



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
# File 'lib/ruby2d/line.rb', line 102

def self.render(x1: 0, y1: 0, x2: 100, y2: 100,
                points: nil,
                stroke_width: 1, dash: 0, gap: 5,
                rotate: 0, rx: nil, ry: nil,
                color: nil, colour: nil, opacity: nil)
  Window.render_ready_check
  if points
    validate_points(points)
    x1, y1 = points[0]
    x2, y2 = points[1]
  end

  input = color || colour
  c = Color.for_render(input.nil? ? 'white' : input)
  if c.is_a?(Color::Set) && c.length != 2
    raise ArgumentError,
          "`#{self}` requires 2 colors (start, end) for a gradient. #{c.length} were given."
  end

  # Expand to 4 vertex colors: start -> both start corners, end -> both end corners
  c0 = c.vertex(0)
  c1 = c.vertex(1)
  c0a = opacity || c0.a
  c1a = opacity || c1.a

  if rotate != 0
    cx = rx || (x1 + x2) / 2.0
    cy = ry || (y1 + y2) / 2.0
    rad = rotate * Math::PI / 180.0
    sa = Math.sin(rad); ca = Math.cos(rad)
    dx = x1 - cx; dy = y1 - cy
    x1 = dx * ca - dy * sa + cx; y1 = dx * sa + dy * ca + cy
    dx = x2 - cx; dy = y2 - cy
    x2 = dx * ca - dy * sa + cx; y2 = dx * sa + dy * ca + cy
  end

  if dash > 0
    Ext.draw_dashed_line(
      x1, y1, x2, y2, stroke_width, dash, gap,
      c0.r, c0.g, c0.b, c0a,
      c0.r, c0.g, c0.b, c0a,
      c1.r, c1.g, c1.b, c1a,
      c1.r, c1.g, c1.b, c1a
    )
  else
    Ext.draw_line(
      x1, y1, x2, y2, stroke_width,
      c0.r, c0.g, c0.b, c0a,
      c0.r, c0.g, c0.b, c0a,
      c1.r, c1.g, c1.b, c1a,
      c1.r, c1.g, c1.b, c1a
    )
  end
end

Instance Method Details

#color=(color) ⇒ Object

Set the color of the line. Accepts a single color or a Color::Set of 2 (start, end) for a gradient along the length.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby2d/line.rb', line 44

def color=(color)
  color = Color.set(color)

  if color.is_a?(Color::Set) && color.length != 2
    raise ArgumentError,
          "`#{self.class}` requires 2 colors (start, end) for a gradient. #{color.length} were given."
  end

  @color = color
  @_cc = nil
end

#contains?(x, y) ⇒ Boolean

Check if the line contains the given point — within half the stroke width of the drawn segment and between its endpoints, so the hit region matches the rendered (butt-capped) rectangle rather than overhanging its ends. Shares the segment test with Polyline.

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/ruby2d/line.rb', line 74

def contains?(x, y)
  return false if @stroke_width.negative?
  x, y = _unrotate(x, y) if @rotate != 0
  half = @stroke_width / 2.0
  _point_on_segment?(x, y, @x1, @y1, @x2, @y2, half * half)
end

#heightObject



66
67
68
# File 'lib/ruby2d/line.rb', line 66

def height
  (@y1 - @y2).abs
end

#lengthObject

Return the length of the line



57
58
59
# File 'lib/ruby2d/line.rb', line 57

def length
  points_distance(@x1, @y1, @x2, @y2)
end

#rxObject

Get the rotation center x coordinate



82
83
84
# File 'lib/ruby2d/line.rb', line 82

def rx
  @_user_rx.nil? ? (@x1 + @x2) / 2.0 : @_user_rx
end

#rx=(val) ⇒ Object

Set the rotation center x coordinate



92
93
94
# File 'lib/ruby2d/line.rb', line 92

def rx=(val)
  @_user_rx = val
end

#ryObject

Get the rotation center y coordinate



87
88
89
# File 'lib/ruby2d/line.rb', line 87

def ry
  @_user_ry.nil? ? (@y1 + @y2) / 2.0 : @_user_ry
end

#ry=(val) ⇒ Object

Set the rotation center y coordinate



97
98
99
# File 'lib/ruby2d/line.rb', line 97

def ry=(val)
  @_user_ry = val
end

#widthObject

Bounding-box width and height (extent between the two endpoints)



62
63
64
# File 'lib/ruby2d/line.rb', line 62

def width
  (@x1 - @x2).abs
end