ruby Ruby:获取旋转矩形的边界框的尺寸
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Ruby:获取旋转矩形的边界框的尺寸相关的知识,希望对你有一定的参考价值。
<img src="https://docs.google.com/drawings/d/1lV7Ektr4BnzyXdbiwo8MOAjQ339NpvDnjP1Qp5bQq1g/pub?w=488&h=381">
require 'bigdecimal'
def bounding_box_dimensions(inner_rectangle_width, inner_rectangle_height, rotate_in_degrees)
width = BigDecimal.new inner_rectangle_width
height = BigDecimal.new inner_rectangle_height
angle = BigDecimal.new((rotate_in_degrees * Math::PI) / 180, 5) # convert to radians
# rectangle centre coords
centre_x = width/2
centre_y = height/2
corners = [[0, 0], [0, height], [width, height], [width, 0]]
corners.map! do |points|
# translate rectangle centre to origin
temp_x = points[0] - centre_x
temp_y = points[1] - centre_y
# do rotation
rotated_x = (temp_x * Math::cos(angle)) - (temp_y * Math::sin(angle))
rotated_y = (temp_x * Math::sin(angle)) + (temp_y * Math::cos(angle))
# translate rectangle centre back to original place
points[0] = rotated_x + centre_x
points[1] = rotated_y + centre_y
points
end
min_x, max_x = corners.minmax_by { |p| p[0] }.map { |p| p[0] }
width = max_x - min_x # distance in single dimension
min_y, max_y = corners.minmax_by { |p| p[1] }.map { |p| p[1] }
height = max_y - min_y # distance in single dimension
[
BigDecimal.new(width).round(3, :default).to_s('F'),
BigDecimal.new(height).round(3, :default).to_s('F')
]
end
以上是关于ruby Ruby:获取旋转矩形的边界框的尺寸的主要内容,如果未能解决你的问题,请参考以下文章
从旋转的矩形计算边界框坐标
查找覆盖原始矩形的旋转矩形的大小
如何计算围绕其角旋转的矩形的边界框?
如何计算围绕其中心旋转的矩形的边界框?
Fabric.js - 带有免费矩形边界框的文本,如 Google 幻灯片
ruby Google Compute Engine快照在Ruby中创建和旋转