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
-
.calculate_dimensions(width, height, rwidth: nil, rheight: nil) ⇒ Object
Calculates and returns the requested dimensions for the rectangular object with the given
width
andheight
based on the following: options:. -
.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.
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. In case ofwidth
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 ofheight
begin zero,width
is used for the width.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/hexapdf/utils/graphics_helpers.rb', line 55 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:) ⇒ 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)
72 73 74 75 |
# File 'lib/hexapdf/utils/graphics_helpers.rb', line 72 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 |