4
$\begingroup$

How does one use the angle returned from mathutils.geometry.box_fit_2d?

enter image description here

For example, in the image above the face is selected & the following is run in the console. (There is no transform on mesh object, so vert coordinates are global coords)

>>> bm = bmesh.from_edit_mesh(C.object.data)
>>> points = [v.co.xy for v in bm.verts if v.select]
>>> points
[Vector((39.5, 125.0)), Vector((40.25, 107.0)), Vector((107.0, 163.0)), Vector((80.5, 159.5))]
>>> a = box_fit_2d(points)
>>> degrees(a)
50.00498629110601

The plane (the square) is rotated 50.005 degrees.

I've found a couple of example usages generally for UVs http://www.programcreek.com/python/example/56195/mathutils.Vector.

$\endgroup$
4
$\begingroup$

The angle returned by box_fit_3d is the angle by which the given points have to be rotated to best fit them into a rectangle aligned with the axes.

The example script linked in your question – currently there is only one script with a call to box_fit_3d – does indeed use the angle to calculate the coordinates of the rotated points:

angle = mathutils.geometry.box_fit_2d(cos_2d)

mat = mathutils.Matrix.Rotation(angle, 2)
cos_2d = [(mat * co) for co in cos_2d]
xs = [co.x for co in cos_2d]
ys = [co.y for co in cos_2d]

But if you instead want to fit the original points into a rotated rectangle then you would have to rotate the rectangle by the negative of that angle.
The image below shows the face from your question together with a plane rotated by −50.005 degrees instead.

rotated rectangle

If you're also interested in the optimal size of the rectangle I guess it's still best to calculate the rotated points and then determine the smallest and largest coordinates like it's been done in the example script.

width = max(xs) - min(xs)
height = max(ys) - min(ys)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.