Class: EightCorner::Base
- Inherits:
-
Object
- Object
- EightCorner::Base
- Defined in:
- lib/eight_corner/base.rb
Overview
This class is a catch-all. Will be cleaned up, you know, sometime.
Class Method Summary collapse
Instance Method Summary collapse
-
#aas(angle_a, angle_b, side_A) ⇒ Object
angle, angle, side A / sin(a) == B / sin(b) return length of side_B.
-
#angle(current, percent) ⇒ Object
pick an angle for the next point steer away from the corners by avoiding angles which tend toward the corner we are currently closest to.
- #deg2rad(degrees) ⇒ Object
-
#distance_to_boundary(point, degrees) ⇒ Object
what is the distance from point to extent, along a line of degrees angle.
-
#initialize(x_extent, y_extent, options = {}) ⇒ Base
constructor
A new instance of Base.
- #next_point(last_point, angle, distance) ⇒ Object
- #plot(str, options = {}) ⇒ Object
- #rad2deg(radians) ⇒ Object
-
#starting_point(str) ⇒ Object
return a starting point for string.
Constructor Details
#initialize(x_extent, y_extent, options = {}) ⇒ Base
Returns a new instance of Base.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/eight_corner/base.rb', line 12 def initialize(x_extent, y_extent, ={}) defaults = { logger: Logger.new('/dev/null') } self.class.(, defaults) = defaults.merge() @bounds = Bounds.new(x_extent, y_extent) @point_count = 8 @log = [:logger] # @figure_interdepencence = options[:figure_interdepencence] end |
Class Method Details
.validate_options!(options, defaults) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/eight_corner/base.rb', line 5 def self.(, defaults) = .keys - defaults.keys if .size > 0 raise ArgumentError, "Unrecognized options: #{.inspect}" end end |
Instance Method Details
#aas(angle_a, angle_b, side_A) ⇒ Object
angle, angle, side A / sin(a) == B / sin(b) return length of side_B
272 273 274 |
# File 'lib/eight_corner/base.rb', line 272 def aas(angle_a, angle_b, side_A) side_A / Math.sin(deg2rad(angle_a)) * Math.sin(deg2rad(angle_b)) end |
#angle(current, percent) ⇒ Object
pick an angle for the next point steer away from the corners by avoiding angles which tend toward the corner we are currently closest to.
current Point x & y extents percent : how far along the arc should we go?
as a float 0..1
always counter-clockwise.
return: an angle from current point.
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/eight_corner/base.rb', line 194 def angle(current, percent) range = Quadrant.angle_range_for(@bounds.quadrant(current)) interp = Interpolate::Points.new({ 0 => range.begin, 1 => range.end }) interp.at(percent).to_i % 360 end |
#deg2rad(degrees) ⇒ Object
261 262 263 |
# File 'lib/eight_corner/base.rb', line 261 def deg2rad(degrees) degrees * Math::PI / 180 end |
#distance_to_boundary(point, degrees) ⇒ Object
what is the distance from point to extent, along a line of degrees angle
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/eight_corner/base.rb', line 206 def distance_to_boundary(point, degrees) degrees %= 360 case degrees when 0 then point.x when 1..89 then to_top = aas(90-degrees, 90, point.y) to_right = aas(degrees, 90, @bounds.x - point.x) [to_top, to_right].min when 90 then @bounds.x - point.x when 91..179 then to_right = aas(180-degrees, 90, @bounds.x - point.x) to_bottom = aas(90-180-degrees, 90, @bounds.y - point.y) [to_right, to_bottom].min when 180 then @bounds.y - point.y when 181..269 then to_bottom = aas(90-degrees-180, 90, @bounds.y - point.y) to_left = aas(degrees - 180, 90, point.x) [to_bottom, to_left].min when 270 then point.x when 271..359 then to_left = aas(360-degrees, 90, point.x) to_top = aas(90-360-degrees, 90, point.y) [to_left, to_top].min end end |
#next_point(last_point, angle, distance) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/eight_corner/base.rb', line 245 def next_point(last_point, angle, distance) # geometry black magic here. still not positive exactly why this works. # unit circle begins at 90 and goes counterclockwise. # we want to start at 0 and go clockwise # orientation of 0 degrees to coordinate space probably matters also. theta = (180 - angle) % 360 point = Point.new point.x = (Math.sin(deg2rad(theta)) * distance + last_point.x).round point.y = (Math.cos(deg2rad(theta)) * distance + last_point.y).round point.distance_from_last = distance point.angle_from_last = angle point.bounds = @bounds point end |
#plot(str, options = {}) ⇒ Object
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 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 99 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 |
# File 'lib/eight_corner/base.rb', line 27 def plot(str, ={}) defaults = { group_method: :group2, angle_method: :percentize_modulus_exp, distance_method: :percentize_modulus, start_method: :starting_point, # will the initial_potential, and potentials generated from previous # points in the same figure, be used to alter the angle to the next # point? point_interdependence: true, # 0.5 is 'no change' see angle_potential_interp initial_potential: 0.5 } self.class.(, defaults) = defaults.merge() mapper = StringMapper.new(group_count: @point_count-1) # 7 2-element arrays. each value is a float 0..1. # 1st: % applied to calculate an angle # 2nd: % applied to calculate a distance potentials = mapper.potentials( mapper.groups(str, [:group_method]), [:angle_method], [:distance_method] ) # the figure we are drawing. figure = Figure.new # set starting point. figure.points << send([:start_method], str) # a potential is a value derived from the previous point in a figure # these are used to modify the angle used to locate the next point in # the figure. in this way, previous figures add influence # which wouldn't be present if the figure were drawn on its own. # - median potential (0.5) changes nothing. # - extremely low potential (0.0) moves the angle 15% counter-clockwise # - extremely high potential (1.0) moves the angle 15% clockwise angle_potential_interp = Interpolate::Points.new(0.0 => -0.15, 0.5 => 0.0, 1.0 => 0.15) # increase low distance potentials to encourage longer lines # this is added to the raw distance potential determined by the string mapper. # - a distance_pct of 0 will have 0.3 added to it. # - a distance_pct of 0.5 or greater will have nothing added to it. additional_distance_interp = Interpolate::Points.new(0.0 => 0.3, 0.5 => 0.0) previous_potential = [:initial_potential] (@point_count - 1).times do |i| current_point = figure.points[i] # TODO encourage more open angles? angle_pct = potentials[i][0] distance_pct = potentials[i][1] @log.debug(['angle_pct', angle_pct]) # if points can influence each other, apply potential from previous # point to the angle-selection process. if [:point_interdependence] angle_pct_adjustment = angle_potential_interp.at(previous_potential) @log.debug(['angle_pct_adjustment', angle_pct_adjustment]) @log.debug(['pre-ajustment', angle_pct, angle(current_point, angle_pct)]) angle_pct += angle_pct_adjustment @log.debug(['post-ajustment', angle_pct, angle(current_point, angle_pct)]) end angle_to_next = angle(current_point, angle_pct) dist_to_boundary = distance_to_boundary(current_point, angle_to_next) @log.debug(['angle_to_next', angle_to_next]) @log.debug(['distance_to_boundary', dist_to_boundary]) # if we're too close to the edge, go the opposite direction. # so we don't get trapped in a corner. if dist_to_boundary <= 1 @log.debug('dist_to_boundary is close to border. adjust angle.') angle_to_next += 180 angle_to_next %= 360 dist_to_boundary = distance_to_boundary(current_point, angle_to_next) @log.debug(['after 180: angle_to_next', angle_to_next]) @log.debug(['after 180: distance_to_boundary', dist_to_boundary]) end # how to encourage more space-filling? # track how many points are in each quadrant. # if current point is in the most-populated one, move to least-populated. # if current point and previous point are too close together... # if current point and last point are in different quadrants... distance_pct += additional_distance_interp.at(distance_pct) # longer lines fill space better distance_pct = 0.3 if distance_pct < 0.3 # keep away from bounds. distance_pct = 0.9 if distance_pct > 0.9 distance = dist_to_boundary * distance_pct next_point = next_point( current_point, angle_to_next, distance ) next_point.angle_pct = angle_pct next_point.distance_pct = distance_pct next_point.created_by_potential = previous_potential # TODO: how do we create invalid points? # some bug in distance_to_boundary, most likely. if ! next_point.valid? if next_point.x < 0 next_point.x = 0 end if next_point.y < 0 next_point.y = 0 end @log.error "point produced invalid next. '#{str}' #{i}" @log.error(['angle_to_next', angle_to_next]) @log.error(['distance_to_boundary', dist_to_boundary]) @log.error(['next_point', next_point]) end figure.points << next_point previous_potential = figure.points.last.potential end figure end |
#rad2deg(radians) ⇒ Object
265 266 267 |
# File 'lib/eight_corner/base.rb', line 265 def rad2deg(radians) radians * 180 / Math::PI end |
#starting_point(str) ⇒ Object
return a starting point for string
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/eight_corner/base.rb', line 164 def starting_point(str) mapper = StringMapper.new raw_x_pct = mapper.percentize_modulus(str) raw_y_pct = mapper.percentize_modulus_exp(str) # mapper produces raw %'s 0..1. # figures that start out very close to a border often get trapped and # look strange, so we won't allow a starting point <30% or >70%. interp = Interpolate::Points.new(0 => 0.2, 1 => 0.8) x_pct = interp.at( raw_x_pct ) y_pct = interp.at( raw_y_pct ) Point.new( (x_pct * @bounds.x).to_i, (y_pct * @bounds.y).to_i ) end |