Class: VIBes::Figure

Inherits:
Object
  • Object
show all
Defined in:
lib/vibes-rb.rb

Overview

Figure is the class used to handle a single figure with the viewer.

Instance Attribute Summary collapse

Figure management collapse

Axis management collapse

Groups collapse

Drawing functions collapse

Instance Method Summary collapse

Constructor Details

#initialize(figure_name = 'default') ⇒ Figure

Create a new figure name figname.

Examples:

f = VIBes::Figure.new 'Name of the figure'

Parameters:

  • figure_name (String) (defaults to: 'default')

    the name of the figure



127
128
129
130
131
# File 'lib/vibes-rb.rb', line 127

def initialize(figure_name = 'default')
	figure_name = 'default' if not(figure_name.is_a?(String)) or figure_name.empty?
	@name = figure_name
	VIBes.msg "{\"action\":\"new\",\"figure\":\"#{@name}\"}\n\n"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ self

Set a property using the form self.property=value. If the method is not known to the figure and is in the form 'figure.method=val', call the #set_properties method whith the method name as key val as value.

Examples:

f = VIBes::Figure.new
f.width = 500
f.height = 250
# width= and height= are not instance methods of Figure,
# so the last two method calls are translated into
# f.set_properties(width: 500)
# f.set_properties(height: 250)

See Also:



197
198
199
200
201
202
203
204
205
206
# File 'lib/vibes-rb.rb', line 197

def method_missing(m, *args, &block)
	if m.to_s =~ /=$/ then
		raise 'not expecting a block' if block
		raise 'expecting 1 and only 1 argument' if args.length != 1
		self[m.to_s.sub(/=$/, '')] = args.first
	else
		super
	end
	self
end

Instance Attribute Details

#nameString (readonly)

Name of the figure that will appear on the viewer. If two instances of VIBes::Figure have the same name, they will treat the same figure on the viewer.

Returns:

  • (String)

    the figure name



116
117
118
# File 'lib/vibes-rb.rb', line 116

def name
  @name
end

Instance Method Details

#[]=(key, value) ⇒ self

Set a property using the form [property]=value

Examples:

# Set the new x position
fig[:x] = 100

See Also:



179
180
181
# File 'lib/vibes-rb.rb', line 179

def []=(key, value)
	set_properties(**{key.to_sym => value})
end

#axis_autoself

Set axes limits to the bounding box of the drawing.

See Also:



230
231
232
# File 'lib/vibes-rb.rb', line 230

def axis_auto
	set_properties(viewbox: :auto)
end

#axis_equalself

Same as #axis_auto but with the same ratio on the two axis.

See Also:



237
238
239
# File 'lib/vibes-rb.rb', line 237

def axis_equal
	set_properties(viewbox: :equal)
end

#axis_labels(x_label, y_label) ⇒ self

Set axis labels

Parameters:

  • (String)


293
294
295
# File 'lib/vibes-rb.rb', line 293

def axis_labels(*labels)
	set_properties(axislabels: labels.collect!{|o| o.to_s})
end

#axis_limits(x_min, x_max, y_min, y_max) ⇒ self #axis_limits(x_range, y_range) ⇒ self #axis_limits(x_interval, y_interval) ⇒ self #axis_limits(xy_box) ⇒ self

Set the rectangle to be displayed.

Overloads:

  • #axis_limits(x_min, x_max, y_min, y_max) ⇒ self

    Parameters:

    • (Float, Integer)
  • #axis_limits(x_range, y_range) ⇒ self

    Parameters:

    • (Range<Float, Integer>)
  • #axis_limits(x_interval, y_interval) ⇒ self

    Parameters:

    • an (Interval)

      object that respond to lower_bound and upper_bound

  • #axis_limits(xy_box) ⇒ self

    Parameters:

    • an (IntervalVector)

      interval vector of 2 intervals



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/vibes-rb.rb', line 251

def axis_limits(*v)
	a = []
	v.each do |e|
		case e
		when Numeric
			a << e.to_f
		when Range
			l = e.begin.to_f
			u = e.end.to_f
			l, u = [u, l] if l > u
			a << l
			a << u
		when Array
			raise "array must have 1 or 2 elements" if e.length < 1 or e.length > 2
			l = e.first.to_f
			u = e.last.to_f
			l, u = [u, l] if l > u
			a << l
			a << u
		else
			if VIBes::object_is_an_interval?(e)
				a << e.lower_bound
				a << e.upper_bound
			elsif VIBes::object_is_a_box?(e)
				raise "the box is expected to have 2 dimensions, not #{e.length}" if e.length != 2
				a << e[0].lower_bound
				a << e[0].upper_bound
				a << e[1].lower_bound
				a << e[1].upper_bound
			else
				raise "expecting floats, ranges, 2 element arrays, intervals or box, not #{e.class}"
			end
		end
	end
	raise "Should have 4 coordinates, not #{a.length}" if a.length != 4
	set_properties(viewbox: a)
end

#clearself

Clear the content of the figure



142
143
144
145
# File 'lib/vibes-rb.rb', line 142

def clear
	VIBes.msg "{\"action\":\"clear\",\"figure\":\"#{@name}\"}\n\n"
	self
end

#clear_group(group_name, **kwargs) ⇒ self

Clear the content of the group group_name from the figure.

Parameters:

  • group_name (String)

    name of the group to remove

  • kwargs

    optional keyword arguments



322
323
324
325
326
327
328
# File 'lib/vibes-rb.rb', line 322

def clear_group(group_name, **kwargs)
   		kwargs[:action] = :clear
   		kwargs[:figure] = @name
	kwargs[:group] = group_name.to_s
	VIBes.msg "#{kwargs.to_json}\n\n"
	self
end

#closeself

Close the figure



135
136
137
138
# File 'lib/vibes-rb.rb', line 135

def close
	VIBes.msg "{\"action\":\"close\",\"figure\":\"#{@name}\"}\n\n"
	self
end

#draw_arrow(ps = nil, pe = nil, tl = nil, p1: [0, 0], p2: [1, 1], tip_length: nil, color: nil, **kwargs) ⇒ self

Draw an arrow

Examples:

# Draw an array from (0,0) to (1,1), with a tip length of 0.2
f.draw_arrow([0, 0], [1, 1], 0.2, color: 'r[r]')
f.draw_arrow(p1: [0, 0], p2: [1, 1], tip_length: 0.2, color: 'k[k]')


505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/vibes-rb.rb', line 505

def draw_arrow(ps=nil, pe=nil, tl=nil, p1: [0, 0], p2: [1, 1], tip_length: nil, color: nil, **kwargs)
	kwargs[:type] = :arrow
	p1 = ps if ps.is_a?(Array)
	p2 = pe if pe.is_a?(Array)
	raise 'The start and end points must have the same number of coordinates' if p1.length != p2.length
	kwargs[:points] = [p1.collect(&:to_f), p2.collect(&:to_f)]
	kwargs[:format] = color if color.is_a?(String)
	if tl != nil then
		kwargs[:tip_length] = tl.to_f 
	elsif tip_length.kind_of?(Numeric) then
		kwargs[:tip_length] = tip_length.to_f 
	elsif p1.length == 2 and p2.length == 2 then
		kwargs[:tip_length] = Math.sqrt((p2.first - p1.first)**2 + (p2.last - p1.last)**2)/10.0
	else
		kwargs[:tip_length] = 0.1
	end
	draw(kwargs)
end

#draw_AUV(cent = nil, rt = nil, len = nil, center: [0, 0], heading: 0.0, length: 1.0, color: nil, **kwargs) ⇒ self

Draw an AUV (yellow submarine) centered at center, with heading heading degrees and size length.

Examples:

f.draw_AUV(center: [0, -1], heading: 45, length: 1.0, color: 'k[y]')
f.draw_AUV([0, -1], 45, 1.0, color: 'k[y]')

Parameters:

  • center (Array<Float>) (defaults to: [0, 0])

    Center of the AUV

  • heading (Float) (defaults to: 0.0)

    heading of the AUV in degree

  • length (Float) (defaults to: 1.0)

    size of the AUV



619
620
621
622
623
624
625
626
# File 'lib/vibes-rb.rb', line 619

def draw_AUV(cent=nil, rt=nil, len=nil, center: [0, 0], heading: 0.0, length: 1.0, color: nil, **kwargs)
	kwargs[:type] = :vehicle_auv
	kwargs[:center] = (cent.is_a?(Array) ? cent : center).collect(&:to_f)
	kwargs[:orientation] = (rt.nil? ? heading : rt).to_f
	kwargs[:length] = Math.sqrt((len.nil? ? length : len).to_f)
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_box(*a, color: nil, **kwargs) ⇒ self

Draw a box

Examples:

Draw a box with 𝒙 ∈ [0, 1] and 𝒚 ∈ [2, 3]

f = VIBes::Figure.new
# Using 4 numeric bounds
f.draw_box(0, 1, 2, 3, color: 'k[r]')
# Using 2 ranges
f.draw_box(0..1, 2..3, color: 'k[b]')
# Using 2 arrays
f.draw_box([0, 1], [2, 3], color: 'k[g]')
# Using 2 intervals
x = P1788::Interval[0, 1]
y = P1788::Interval[2, 3]
f.draw_box(x, y, color: 'k[y]')
# Using an interval vector
b = P1788::IntervalVector[x, y]
f.draw_box(b, color: 'k[c]')

Parameters:

  • a (Numeric, Range, Array, Interval, IntervalVector)

    bounds of the box

  • color (String) (defaults to: nil)

    color format

  • kwargs

    optional keyword arguments



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/vibes-rb.rb', line 353

def draw_box(*a, color: nil, **kwargs)
	c = []
	f = nil
	a.each do |e|
		case e
		when Numeric
			c << e.to_f
		when Range
			l = e.begin.to_f
			u = e.end.to_f
			l, u = [u, l] if l > u
			c << l
			c << u
		when Array
			raise "array must have 1 or 2 elements" if e.length < 1 or e.length > 2
			l = e.first.to_f
			u = e.last.to_f
			l, u = [u, l] if l > u
			c << l
			c << u
		when String
			f = e
		else
			if VIBes::object_is_an_interval?(e) then
				c << e.lower_bound
				c << e.upper_bound
			elsif VIBes::object_is_a_box?(e) then
				e.each do |v|
					c << v.lower_bound
					c << v.upper_bound
				end
			else
				raise "expecting floats, ranges, 2 element arrays, intervals or boxes, not #{e.class}"
			end
		end
	end
	raise "Should have a multiple of 2 coordinates, not #{c.length}" if c.length % 2 != 0
	kwargs[:format] = f if f
	kwargs[:format] = color if color.is_a?(String)
	kwargs[:type] = :box
	kwargs[:bounds] = c
	draw(kwargs)
end

#draw_boxes(*boxes, color: nil, **kwargs) ⇒ self

Draw multiple boxes at once

Parameters:

  • boxes (IntervalVector, Array<IntervalVector>)

    Boxes or an array of boxes



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/vibes-rb.rb', line 400

def draw_boxes(*boxes, color: nil, **kwargs)
	kwargs[:bounds] = []
	boxes.flatten!
	color = boxes.select{|v| v.is_a?(String)}.first if color.nil?
	boxes.reject!{|v| v.is_a?(String)}
	boxes.each do |b|
		if VIBes::object_is_a_box?(b) then
			ba = []
			b.each do |v|
				ba << v.lower_bound
				ba << v.upper_bound
			end
			kwargs[:bounds] << ba
		else
			raise "expecting interval vectors, not #{b.class}"
		end
	end
	kwargs[:type] = 'boxes'
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_boxes_union(*boxes, color: nil, **kwargs) ⇒ self

Draw the union of multiple boxes.

Parameters:

  • boxes (IntervalVector, Array<IntervalVector>)

    Boxes or an array of boxes



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/vibes-rb.rb', line 425

def draw_boxes_union(*boxes, color: nil, **kwargs)
	kwargs[:bounds] = []
	boxes.flatten!
	color = boxes.select{|v| v.is_a?(String)}.first if color.nil?
	boxes.reject!{|v| v.is_a?(String)}
	boxes.each do |b|
		if VIBes::object_is_a_box?(b) then
			ba = []
			b.each do |v|
				ba << v.lower_bound
				ba << v.upper_bound
			end
			kwargs[:bounds] << ba
		else
			raise "expecting interval vectors, not #{b.class}"
		end
	end
	kwargs[:type] = 'boxes union'
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_circle(c, r, color: nil, **kwargs) ⇒ self

Draw a circle.

Examples:

# Draw a circle of center (-1,0) and radius 0.5
f.draw_circle([-1, 0], 0.5)


529
530
531
# File 'lib/vibes-rb.rb', line 529

def draw_circle(c, r, color: nil, **kwargs)
	draw_ellipse(c, [r, r], color: color, **kwargs)
end

#draw_confidence_ellipse(cent = nil, cov = nil, sig = nil, center: nil, cx: 0.0, cy: 0.0, covariance: nil, sxx: 1.0, sxy: 1.0, syy: 1.0, sigma: 1.0, color: nil, **kwargs) ⇒ self



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# File 'lib/vibes-rb.rb', line 677

def draw_confidence_ellipse(cent=nil, cov=nil, sig=nil, center: nil, cx: 0.0, cy: 0.0, covariance: nil, sxx: 1.0, sxy: 1.0, syy: 1.0, sigma: 1.0, color: nil, **kwargs)
	kwargs = {} if kwargs.nil?
	kwargs[:type] = :ellipse
	if cent.is_a?(Array) then
		kwargs[:center] = cent.collect(&:to_f)
	else
		kwargs[:center] = center.is_a?(Array) ? center.collect(&:to_f) : [cx.to_f, cy.to_f]
	end
	if cov.is_a?(Array) then
		kwargs[:covariance] = cov.collect(&:to_f)
	else
		kwargs[:covariance] = covariance.is_a?(Array) ? covariance.collect(&:to_f) : [sxx.to_f, sxy.to_f, sxy.to_f, syy.to_f]
	end
	kwargs[:sigma] = sig.nil? ? sigma.to_f : sig.to_f
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_ellipse(cent = nil, ax = nil, rt = nil, center: nil, cx: 0.0, cy: 0.0, axis: nil, a: 1.0, b: a, rot: 0.0, color: nil, **kwargs) ⇒ self

Draw an ellipse centered at (cx,cy) with semi-major and minor axes a and b, and rotated by rot degrees.



658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/vibes-rb.rb', line 658

def draw_ellipse(cent=nil, ax=nil, rt=nil, center: nil, cx: 0.0, cy: 0.0, axis: nil, a: 1.0, b: a, rot: 0.0, color: nil, **kwargs)
	kwargs = {} if kwargs.nil?
	kwargs[:type] = :ellipse
	if cent.is_a?(Array) then
		kwargs[:center] = cent.collect(&:to_f)
	else
		kwargs[:center] = center.is_a?(Array) ? center.collect(&:to_f) : [cx.to_f, cy.to_f]
	end
	if ax.is_a?(Array) then
		kwargs[:axis] = ax.collect(&:to_f)
	else
		kwargs[:axis] = axis.is_a?(Array) ? axis.collect(&:to_f) : [a.to_f, b.to_f]
	end
	kwargs[:orientation] = rt.nil? ? rot.to_f : rt.to_f
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_line(a_start, a_end, color: 'r', **kwargs) ⇒ self #draw_line(p1: a_start, p2: a_end, color: 'k', **kwargs) ⇒ self

Draw a line between two points

Examples:

draw_line([0, 0], [1, 1])
draw_line(p1: [0, 0], p2: [1, 1])

Overloads:

  • #draw_line(a_start, a_end, color: 'r', **kwargs) ⇒ self

    Parameters:

    • point (Array<Float>)

      coordinates in arrays

  • #draw_line(p1: a_start, p2: a_end, color: 'k', **kwargs) ⇒ self

    Parameters:

    • point (Array<Float>)

      coordinates in arrays



489
490
491
492
493
494
495
496
497
# File 'lib/vibes-rb.rb', line 489

def draw_line(ps=nil, pe=nil, p1: [0, 0], p2: [1, 1], color: nil, **kwargs)
	kwargs[:type] = :line
	p1 = ps if ps.is_a?(Array)
	p2 = pe if pe.is_a?(Array)
	raise 'The start and end points must have the same number of coordinates' if p1.length != p2.length
	kwargs[:points] = [p1.collect(&:to_f), p2.collect(&:to_f)]
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_pie(cent = nil, rad = nil, thet = nil, center: [0, 0], radius: [1, 2], theta: [0, 45], use_radians: false, color: nil, **kwargs) ⇒ self

Draw a pie

Examples:

f.draw_pie(center: [0, 0], radius: [1, 2], theta: [0, 45], use_radians: false)
f.draw_pie([0, 0], [1, 2], [0, 45], color: '[r]')

Parameters:

  • center (Array<Float>) (defaults to: [0, 0])
  • radius (Array<Float>) (defaults to: [1, 2])

    minimal and maximal radius

  • theta (Array<Float>) (defaults to: [0, 45])

    minimal and maximal angle, in radians or degrees depending on use_radians

  • use_radians (Boolean) (defaults to: false)

    use radians or degrees



589
590
591
592
593
594
595
596
597
# File 'lib/vibes-rb.rb', line 589

def draw_pie(cent=nil, rad=nil, thet=nil, center: [0, 0], radius: [1, 2], theta: [0, 45], use_radians: false, color: nil, **kwargs)
	kwargs[:type] = :pie
	kwargs[:center] = (cent.is_a?(Array) ? cent : center).collect(&:to_f)
	kwargs[:rho] = (rad.is_a?(Array) ? rad : radius).collect(&:to_f)
	kwargs[:theta] = (thet.is_a?(Array) ? thet : theta).collect(&:to_f)
	kwargs[:theta].collect!{|v| v*180/Math::PI} if use_radians
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_point(cx, cy, radius: 2, color: 'k', **kwargs) ⇒ self

Draw a point.

Parameters:

  • cx (Float)

    point x position

  • cy (Float)

    point y position

  • radius (Float) (defaults to: 2)

    point display radius in pixel



453
454
455
456
457
458
459
# File 'lib/vibes-rb.rb', line 453

def draw_point(*a, radius: 2, color: nil, **kwargs)
	kwargs[:type] = :point
	kwargs[:point] = a.flatten.collect(&:to_f)
	kwargs[:Radius] = radius.to_f if radius
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_points(*a, radius: 2, color: nil, **kwargs) ⇒ self

Draw multiple points at once

Examples:

# Draw three points
f.draw_points([-1, -1], [0, 1], [1, -1])

See Also:



467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/vibes-rb.rb', line 467

def draw_points(*a, radius: 2, color: nil, **kwargs)
	el = nil
	a.each do |e|
		raise "expecting arrays of the same size" unless e.is_a?(Array) and (el.nil? or e.length == el)
		el = e.length
	end
	kwargs[:type] = :points
	kwargs[:centers] = a.collect{|e| e.collect(&:to_f)}
	kwargs[:Radius] = radius.to_f if radius
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_polygon(*a, color: nil, **kwargs) ⇒ self

Draw a polygon

Examples:

f.draw_polygon([-1, -1], [0, 1], [1, -1])


551
552
553
554
555
556
557
558
559
560
561
# File 'lib/vibes-rb.rb', line 551

def draw_polygon(*a, color: nil, **kwargs)
	el = nil
	a.each do |e|
		raise "expecting arrays of the same size" unless e.is_a?(Array) and (e.length == el or el.nil?)
		el = e.length
	end
	kwargs[:type] = :polygon
	kwargs[:bounds] = a.collect{|e| e.collect(&:to_f)}
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_raster(fname, po = nil, re = nil, pos: [0, 0], resolution: [1, -1], **kwargs) ⇒ self

Draw an image.

Examples:

# Display lena.png (512x512 pixels) so tha it fills the box (0,1)x(0,1)
f.draw_raster('lena.png', pos: [0, 1], resolution: [1.0/512, -1.0/512])
f.draw_raster('lena.png', [0, 1], [1.0/512, -1.0/512])

Parameters:

  • fname (String)

    file name of the image to draw

  • pos (Array<Float>) (defaults to: [0, 0])

    (x,y) position of the upper left corner of the image on the figure

  • resolution (Array<Float>) (defaults to: [1, -1])

    horizontal and vertical resolution to display the image. If the vertical resolution is positive, the image may be displayed upside-down.



572
573
574
575
576
577
578
# File 'lib/vibes-rb.rb', line 572

def draw_raster(fname, po=nil, re=nil, pos: [0, 0], resolution: [1, -1], **kwargs)
	kwargs[:type] = :raster
	kwargs[:filename] = fname.to_s
	kwargs[:ul_corner] = (po.is_a?(Array) ? po : pos).collect(&:to_f)
	kwargs[:scale] = (re.is_a?(Array) ? re : resolution).collect(&:to_f)
	draw(kwargs)
end

#draw_ring(cent = nil, rad = nil, center: [0, 0], radius: [1, 2], color: nil, **kwargs) ⇒ self

Draw a Ring.

Examples:

# Draw a ring of center (-1,0), min radius: 0.5 and max radius: 1
f.draw_ring([-1, 0], [0.5, 1], color: 'r[g]')
f.draw_ring(center: [-1, 0], radius: [0.5, 1], color: 'r[g]')


539
540
541
542
543
544
545
# File 'lib/vibes-rb.rb', line 539

def draw_ring(cent=nil, rad=nil, center: [0, 0], radius: [1, 2], color: nil, **kwargs)
	kwargs[:type] = :ring
	kwargs[:center] = (cent.is_a?(Array) ? cent : center).collect(&:to_f)
	kwargs[:rho] = (rad.is_a?(Array) ? rad : radius).collect(&:to_f)
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_tank(cent = nil, rt = nil, len = nil, center: [0, 0], heading: 0.0, length: 1.0, color: nil, **kwargs) ⇒ self

Draw a tank centered at center, with heading heading degrees and of size length.

Examples:

f.draw_tank(center: [0, -1], heading: 45, length: 1.0, color: 'k[g]')
f.draw_tank([0, -1], 45, 1.0, color: 'k[g]')


647
648
649
650
651
652
653
654
# File 'lib/vibes-rb.rb', line 647

def draw_tank(cent=nil, rt=nil, len=nil, center: [0, 0], heading: 0.0, length: 1.0, color: nil, **kwargs)
	kwargs[:type] = :vehicle_tank
	kwargs[:center] = (cent.is_a?(Array) ? cent : center).collect(&:to_f)
	kwargs[:orientation] = (rt.nil? ? heading : rt).to_f
	kwargs[:length] = (len.nil? ? length : len).to_f
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_text(str = nil, po = nil, s = nil, text: '', pos: [0, 0], scale: 1, color: nil, **kwargs) ⇒ self

Note:

Does not seem to work on Linux

Draw text on the figure



602
603
604
605
606
607
608
609
# File 'lib/vibes-rb.rb', line 602

def draw_text(str=nil, po=nil, s=nil, text: '', pos: [0, 0], scale: 1, color: nil, **kwargs)
	kwargs[:type] = :text
	kwargs[:text] = (str.nil? ? text : str).to_s
	kwargs[:position] = (po.is_a?(Array) ? po : pos).collect(&:to_f)
	kwargs[:scale] = (s.nil? ? scale : s).to_f
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#draw_vehicle(cent = nil, rt = nil, len = nil, center: [0, 0], heading: 0.0, length: 1.0, color: nil, **kwargs) ⇒ self

Draw a vehicle centered at center, with heading heading degrees and of size length.

Examples:

f.draw_vehicle(center: [0, -1], heading: 45, length: 1.0, color: 'k[y]')
f.draw_vehicle([0, -1], 45, 1.0, color: 'k[y]')


633
634
635
636
637
638
639
640
# File 'lib/vibes-rb.rb', line 633

def draw_vehicle(cent=nil, rt=nil, len=nil, center: [0, 0], heading: 0.0, length: 1.0, color: nil, **kwargs)
	kwargs[:type] = :vehicle
	kwargs[:center] = (cent.is_a?(Array) ? cent : center).collect(&:to_f)
	kwargs[:orientation] = (rt.nil? ? heading : rt).to_f
	kwargs[:length] = Math.sqrt((len.nil? ? length : len).to_f)
	kwargs[:format] = color if color.is_a?(String)
	draw(kwargs)
end

#new_group(group_name, **kwargs) ⇒ self

Create a new group with the specified group name and parameters.

Groups can be created to gather objects which share common properties (color, ...). Then objects can be added to groups by using the group: keyword.

Examples:

f = VIBes::Figure.new 'test groups'
f.new_group('my group', format: 'r[darkBlue]')
f.draw_circle([0, 0], 3, group: 'my group')
f.draw_box(3..6, 0..3, group: 'my group')

Parameters:

  • group_name (String)

    name of the group

  • kwargs

    optional keyword arguments



312
313
314
315
316
# File 'lib/vibes-rb.rb', line 312

def new_group(group_name, **kwargs)
	kwargs = {type: :group, name: group_name.to_s}
	kwargs.each{|k, v| kwargs[k.to_sym] = v}
	draw kwargs
end

#remove_object(object_name, **kwargs) ⇒ self



699
700
701
702
703
704
705
# File 'lib/vibes-rb.rb', line 699

def remove_object(object_name, **kwargs)
   		kwargs[:action] = :delete
   		kwargs[:figure] = @name
		kwargs[:object] = object_name.to_s
	VIBes.msg "#{kwargs.to_json}\n\n"
	self
end

#save_image(file_name = nil, **kwargs) ⇒ self

Save the figure on disk. Available formats are: png, jpeg, bmp and svg.

Parameters:

  • file_name (String) (defaults to: nil)

    path of the file to save the figure into.



151
152
153
154
155
156
157
158
# File 'lib/vibes-rb.rb', line 151

def save_image(file_name = nil, **kwargs)
	file_name = '' unless file_name.is_a?(String) and file_name.length > 0
	kwargs[:action] = :export
	kwargs[:figure] = @name
	kwargs[:file] = File.expand_path(file_name)
	VIBes.msg "#{kwargs.to_json}\n\n"
	self
end

#set_object_properties(object_name, **kwargs) ⇒ self



708
709
710
711
712
# File 'lib/vibes-rb.rb', line 708

def set_object_properties(object_name, **kwargs)
	h = {action: :set, figure: @name, object: object_name.to_s, properties: kwargs.to_json}
	VIBes.msg "#{h.to_json}\n\n"
	self
end

#set_pos(x, y) ⇒ self

Set the position of the figure window on the screen

Parameters:

  • x (Float)

    horizontal axis position

  • y (Float)

    vertical axis position



220
221
222
# File 'lib/vibes-rb.rb', line 220

def set_pos(x, y)
	set_properties(x: x.to_i, y: y.to_i)
end

#set_properties(**kwargs) ⇒ self

Set figure properties using keyword arguments.

Examples:

# Set a new position and viewport
fig.set_properties(x: 100, y: 50, width: 500, height: 250)

See Also:



167
168
169
170
# File 'lib/vibes-rb.rb', line 167

def set_properties(**kwargs)
	VIBes.msg "{\"action\":\"set\",\"figure\":\"#{@name}\",\"properties\":#{kwargs.to_json}}\n\n"
	self
end

#set_size(width, height) ⇒ self

Set the size of the figure

Parameters:

  • width (Integer)
  • height (Integer)


212
213
214
# File 'lib/vibes-rb.rb', line 212

def set_size(width, height)
	set_properties(width: width.to_i, height: height.to_i)
end