English 中文(简体)
How can I "best fit" an arbitrary cairo (pycairo) path?
原标题:

It seems like given the information in stroke_extents() and the translate(x, y) and scale(x, y) functions, I should be able to take any arbitrary cairo (I m using pycairo) path and "best fit" it. In other words, center it and expand it to fill the available space.

Before drawing the path, I have scaled the canvas such that the origin is the lower left corner, up is y+, right is x+, and the height and width are both 1. Given these conditions, this code seems to correctly scale the path:

# cr is the canvas
extents = cr.stroke_extents()
x_size = abs(extents[0]) + abs(extents[2])
y_size = abs(extents[1]) + abs(extents[3])
cr.scale(1.0 / x_size, 1.0 / y_size)

I cannot for the life of me figure out the translating though. Is there a simpler approach? How can I "best fit" a cairo path on its canvas?

Please ask for clarification if anything is unclear in this question.

最佳回答

I have found a solution that I like (at least for my purposes). Just create a new surface and paint the old surface on to the new one.

问题回答

As for the scale only, I have done a similar thing to adjust an image inside a box with a "best-fit" approach. As about scale, here is the code:

available_width = 800
available_height = 600
path_width = 500    
figure_height = 700     

# formulas
width_ratio = float(available_width)/path_width
height_ratio = float(available_height)/figure_height
scale = min(height_ratio, width_ratio)

# result
new_path_width = path_width*scale
new_figure_height = figure_height*scale

print new_path_width, new_figure_height

The image gets drawn aligned to the origin (top left in my case), so perhaps a similar thing should be done to translate the path.

Also, this best fit is intended to preserve aspect ratio. If you want to stretch the figure, use each of the ratios instead of the scale variable.

Hope I have helped





相关问题
OpenGL 3D Selection

I am trying to create a 3D robot that should perform certain actions when certain body parts are clicked. I have successfully (sort of) implemented picking in that if you click on any x-plane part, it ...

Why and when should you use hoops 3d graphics? [closed]

My company needs 3D visualization for our commercial applications (CAD, mesh manipulation, computational geometry). We are tired of true vision 3D (tv3d), which we ve been using for years (poor ...

Prerequisite for learning directx

I am from .net C# background and I want to learn DirectX. I have knowledge of C++ but I am fairly new to graphic world. I am little confused about how to start learning directx, should I start ...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Determine VRAM size on windows

I need to determine roughly how much VRAM a system s graphics card has. I know all the reasons why I shouldn t but I do. It doesn t need to be perfect (some cards lie etc.) but I need a ballpark. On ...

Mask white color out of PNG image on iPhone

I know that there are already 2 relevant posts on that, but it s not clear... So the case is this...: I have a UIImage which consists of a png file requested from a url on the net. Is it possible to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签