简体   繁体   English

当图像尺寸改变时,如何计算以抵消实际尺寸的坐标值?

[英]When image size change, how do you calculate to counter the real-size coordinate value?

I want to know the coordinates value when the image is stretched from the center. 我想知道从中心拉伸图像时的坐标值。

I know one pixel coordinate in texture image, and I'm using ray cast by matching it to mobile screen. 我知道纹理图像中有一个像素坐标,并且正在通过将射线匹配到移动屏幕来使用射线投射。

But I don't know how the coordinate value changes when the image grows. 但是我不知道图像增长时坐标值如何变化。

在此处输入图片说明

When images grow, how do you calculate to counter the real-size coordinate value? 当图像增长时,如何计算以抵消实际尺寸的坐标值?

The image will increase to 1.33f size. 图像将增加到1.33f尺寸。

在此处输入图片说明

There may be simpler ways in Unity3D - I don't have any experience there - but this sounds like a fairly simple scaling problem. Unity3D中可能有更简单的方法-我在那里没有任何经验-但这听起来像是一个相当简单的缩放问题。

You'll need to know the following: 您需要了解以下内容:

  • The image coordinates of the centre of the image 图像中心的图像坐标
    This is the vector (width/2, height/2) 这是向量(width/2, height/2)
  • The screen coordinates of the centre of the image 图像中心的屏幕坐标
    Where the center of the image is on the screen 图像中心在屏幕上的位置
  • The scaling factor ( 1.33f in this example) 比例因子(在此示例中为1.33f

Given the above you can calculate the pixel being touched using simple math: 鉴于以上所述,您可以使用简单的数学计算要触摸的像素:

public Vector2Int ScaleTouch(Vector2Int imgCentre, Vector2Int dispCentre, float scale, Vector2Int touch)
{
    var x = imgCentre.x + (touch.x - dispCentre.x) * scale;
    var y = imgCentre.y + (touch.y - dispCentre.y) * scale;
    return Vector2Int.RoundToInt(new Vector2(x, y));
}

Or using the methods in Vector2 and Vector2Int you might be able to do this: 或者使用Vector2Vector2Int的方法,您可以执行以下操作:

public Vector2Int ScaleTouch(Vector2Int imgCentre, Vector2Int dispCentre, float scale, Vector2Int touch)
{
    var offset = Vector2.Scale(touch - dispCentre, new Vector2(scale, scale));
    return offset + imgCentre;
}

Both assume that your scale is homogeneous in x and y . 两者都假定您的比例在xy是均匀的。 You could provide a scale vector if you want it to be flexible about scaling in different axes. 如果希望它灵活地在不同轴上缩放,则可以提供一个缩放向量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM