Class: Ballistics::Problem

Inherits:
Object
  • Object
show all
Defined in:
lib/ballistics/problem.rb

Constant Summary collapse

DEFAULTS =
{
  shooting_angle: 0, # degrees; downhill 0 to -90, uphill 0 to +90
  wind_speed:     0, # mph
  wind_angle:    90, # degrees; 0-360 clockwise; 90 right, 270 left
  interval:      50, # yards
  max_range:    500, # yards
  y_intercept:    0, # inches; -2 means POI 2 inches below POA @ zero range
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(projectile: nil, cartridge: nil, gun: nil, atmosphere: nil) ⇒ Problem

Returns a new instance of Problem.



27
28
29
30
31
32
33
34
35
# File 'lib/ballistics/problem.rb', line 27

def initialize(projectile: nil,
               cartridge: nil,
               gun: nil,
               atmosphere: nil)
  @projectile = projectile
  @cartridge = cartridge
  @gun = gun
  @atmosphere = atmosphere
end

Instance Attribute Details

#atmosphereObject

Returns the value of attribute atmosphere.



25
26
27
# File 'lib/ballistics/problem.rb', line 25

def atmosphere
  @atmosphere
end

#cartridgeObject

Returns the value of attribute cartridge.



25
26
27
# File 'lib/ballistics/problem.rb', line 25

def cartridge
  @cartridge
end

#gunObject

Returns the value of attribute gun.



25
26
27
# File 'lib/ballistics/problem.rb', line 25

def gun
  @gun
end

#projectileObject

Returns the value of attribute projectile.



25
26
27
# File 'lib/ballistics/problem.rb', line 25

def projectile
  @projectile
end

Class Method Details

.simple(gun_id:, cart_id:, gun_family: nil) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/ballistics/problem.rb', line 17

def self.simple(gun_id:, cart_id:, gun_family: nil)
  gun = Ballistics::Gun.find(file: gun_family, id: gun_id)
  cart = gun.cartridges.fetch(cart_id)
  self.new(projectile: cart.projectile,
           cartridge: cart,
           gun: gun)
end

Instance Method Details

#enrich(opts = {}) ⇒ Object

Given a hash of specified options / params Return a hash of params enriched by DEFAULTS as well as any inferred

parameters from @projectile, @cartridge, @gun, and @atmosphere


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ballistics/problem.rb', line 41

def enrich(opts = {})
  mine = {}
  mine.merge!(@projectile.params) if @projectile
  mine.merge!(@gun.params) if @gun
  mine[:velocity] = @cartridge.mv(@gun.barrel_length) if @cartridge and @gun

  # Set up the return hash
  # opts overrides mine overrides DEFAULT
  ret = DEFAULTS.merge(mine.merge(opts))

  # validate drag function and replace with the numeral
  if ret[:drag_function] and !ret[:drag_number]
    ret[:drag_number] =
      Ballistics::Projectile.drag_number(ret[:drag_function])
  end

  # apply atmospheric correction to ballistic coefficient
  if ret[:ballistic_coefficient] and @atmosphere
    ret[:ballistic_coefficient] =
      @atmosphere.translate(ret[:ballistic_coefficient])
  end

  ret
end

#reportObject

Return a multiline string showing each component of the problem



68
69
70
71
72
73
74
75
# File 'lib/ballistics/problem.rb', line 68

def report
  lines = []
  lines << @gun.multiline if @gun
  lines << @cartridge.multiline if @cartridge
  lines << @projectile.multiline if @projectile
  lines << @atmosphere.multiline if @atmosphere
  lines.join("\n\n")
end

#table(trajectory: nil, fields: nil, opts: {}) ⇒ Object

Return a multiline string based on trajectory data



93
94
95
96
97
# File 'lib/ballistics/problem.rb', line 93

def table(trajectory: nil, fields: nil, opts: {})
  Ballistics.table(trajectory: trajectory,
                   fields: fields,
                   opts: self.enrich(opts))
end

#trajectory(opts = {}) ⇒ Object

Return a data structure with trajectory data at every interval for the

specified range


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

def trajectory(opts = {})
  Ballistics.trajectory self.enrich opts
end

#zero_angle(opts = {}) ⇒ Object

Given a zero range and basic ballistics parameters Return the angle between sight axis and bore axis necessary to achieve zero



80
81
82
# File 'lib/ballistics/problem.rb', line 80

def zero_angle(opts = {})
  Ballistics.zero_angle self.enrich opts
end