module HexaPDF::Utils::GraphicsHelpers

This module provides some helper functions for graphics.

Public Instance Methods

calculate_dimensions(width, height, rwidth: nil, rheight: nil) click to toggle source

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. In case of width begin zero, height is used for the height.

rheight

The requested height. If rwidth is not specified, it is chosen so that the aspect ratio is maintained. In case of height begin zero, width is used for the width.

# File lib/hexapdf/utils/graphics_helpers.rb, line 54
def calculate_dimensions(width, height, rwidth: nil, rheight: nil)
  if rwidth && rheight
    [rwidth, rheight]
  elsif rwidth
    [rwidth, width == 0 ? height : height * rwidth / width.to_f]
  elsif rheight
    [height == 0 ? width : width * rheight / height.to_f, rheight]
  else
    [width, height]
  end
end
point_on_line(x0, y0, x1, y1, distance:) click to toggle source

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)
# File lib/hexapdf/utils/graphics_helpers.rb, line 71
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