Module: HexaPDF::Utils::GraphicsHelpers

Included in:
Content::Canvas
Defined in:
lib/hexapdf/utils/graphics_helpers.rb

Overview

This module provides some helper functions for graphics.

Class Method Summary collapse

Class Method Details

.calculate_dimensions(width, height, rwidth: nil, rheight: nil) ⇒ Object

Calculates and returns the requested dimensions for the rectangular object with the given width and height based on the following: options:

rwidth

The requested width. If rheight is not specified, it is chosen so that the aspect ratio is maintained

rheight

The requested height. If rwidth is not specified, it is chosen so that the aspect ratio is maintained



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hexapdf/utils/graphics_helpers.rb', line 52

def calculate_dimensions(width, height, rwidth: nil, rheight: nil)
  if rwidth && rheight
    [rwidth, rheight]
  elsif rwidth
    [rwidth, height * rwidth / width.to_f]
  elsif rheight
    [width * rheight / height.to_f, rheight]
  else
    [width, height]
  end
end

.point_on_line(x0, y0, x1, y1, distance:) ⇒ Object

Given two points p0 = (x0, y0) and p1 = (x1, y1), returns the point on the line through these points that is distance units away from p0.

v = p1 - p0
result = p0 + distance * v/norm(v)


69
70
71
72
# File 'lib/hexapdf/utils/graphics_helpers.rb', line 69

def point_on_line(x0, y0, x1, y1, distance:)
  norm = Math.sqrt((x1 - x0)**2 + (y1 - y0)**2)
  [x0 + distance / norm * (x1 - x0), y0 + distance / norm * (y1 - y0)]
end