简体   繁体   English

在iOS中维持宽高比时缩放坐标

[英]Scale Coordinates while Maintaining the Aspect ratio in iOS

I have an array of 2D coordinates that I use to draw curve in a box (w,h). 我有一个2D坐标数组,用于在框中绘制曲线(w,h)。 Now I want to scale the coordinates into a box (x,y) where x or y or both are smaller than w and h. 现在我想将坐标缩放到一个方框(x,y),其中x或y或两者都小于w和h。 The Trick part is that I have to maintain the aspect ratio. 特技部分是我必须保持纵横比。 Any Help would be very much appreciated. 任何帮助将非常感谢。

I'm not sure I get the question right, but if I do, it should be pretty easy: 我不确定我的问题是否正确,但如果我这样做,那应该很容易:

1.Check which of your new sides ( x or y ) is smaller in size; 1.检查您的哪一侧( xy )尺寸较小;

2.Get the ratio of scale by dividing x/w (if x<y ) or y/h (if y<x ); 2.通过除以x/w (如果x<y )或y/h (如果y<x )得到比例;

3.Once you know the ratio, proportionally scale your path with that value. 3.一旦知道比率,就按比例缩放你的路径。

The way you do your last step, it depends on how you keep/draw your array. 你做最后一步的方式,取决于你如何保持/绘制阵列。 If it's just a simple points buffer, the you can use matrix scaling . 如果它只是一个简单的点缓冲区,您可以使用矩阵缩放 Also, although it beats the scope of the question, if you need to do it really fast (the path has lots of points) check out Apple's Accelerate framework for matrix/vector multiplying. 此外,虽然它超出了问题的范围,但如果您需要非常快速地完成(路径有很多要点),请查看Apple的矩阵/向量乘法加速框架

If you need to keep the ratio you need to aspect fit or aspect fill. 如果您需要保持纵横比或纵横填充所需的比例。 Either way you need to find one scale factor for both the x and the y transforms. 无论哪种方式,您都需要为x和y变换找到一个比例因子。

First calculate the individual sacling factors for both the X and the Y 首先计算X和Y的各个骶骨因子

(w1, h1) -> (w2, h2)  (assuming all floats)

float xScaleFactor = w2 / w1;
float yScaleFactor = h2 / h1;

Now since you are making it smaller take either the smallest scale factor for aspect fit or the biggest scale factor for aspect fill. 现在,由于您要缩小尺寸,请采用最小尺寸因子进行纵横比拟合,或采用最大尺寸因子进行纵横填充。

float scaleFactor = MIN(xScaleFactor, yScaleFactor); // Assuming aspect fit

Now simply multiply each point's x and y component by the scale factor. 现在简单地将每个点的x和y分量乘以比例因子。

Get the difference between each of the two x and y coordinates. 获取两个x和y坐标之间的差异。 Determine which is the greater percentage difference, then the multiply the coordinates used in drawing the curve by that percentage. 确定哪个百分比差异较大,然后将绘制曲线时使用的坐标乘以该百分比。 The result will be the curve in a size that will just fit into the smaller box. 结果将是适合较小框的大小的曲线。

You may have to add an offset to the coordinates of the curve to position it within the box correctly. 您可能必须在曲线的坐标上添加偏移量才能将其正确放置在框中。

Depending on your needs, it may be better to look for a drawing API though, rather than doing this manually, as you may get some free (or cheap) functionality like having the user be able to pinch scale, etc. 根据您的需要,最好是寻找绘图API,而不是手动执行,因为您可能会获得一些免费(或便宜)的功能,例如让用户能够缩放比例等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIButton背景图片水平缩放以在保持宽高比的同时进行填充 - UIButton background image horizontal scale to fill while maintaining aspect ratio 使用drawInRect调整图像大小,同时保持像Scale Aspect Fill一样的宽高比? - Resize an image with drawInRect while maintaining the aspect ratio like Scale Aspect Fill? 如何在保持纵横比的同时调整 UIImage 的大小 - How to resize an UIImage while maintaining its Aspect Ratio 如何在保持其纵横比的同时调整AsyncImageView(UIImageView的子类)的大小 - How to resize AsyncImageView (subclass of UIImageView) while maintaining its Aspect Ratio 在自定义单元格UITableViewController中四舍五入的Rect,同时保持宽高比 - Rounded Rect's in custom cell, UITableViewController while maintaining aspect ratio 调整图像保持宽高比的大小 - Resize image maintaining aspect ratio UIViewContentModeScaleAspectFill不保持图像的宽高比 - UIViewContentModeScaleAspectFill not maintaining aspect ratio of image 在启动屏幕上保持宽高比 - maintaining aspect ratio on launch screen "如何在保持纵横比的同时缩放 UIView 的内容以适应目标矩形?" - How To Scale The Contents Of A UIView To Fit A Destination Rectangle Whilst Maintaining The Aspect Ratio? 自动布局宽高比iOS - Autolayot aspect ratio iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM