簡體   English   中英

當圖像尺寸改變時,如何計算以抵消實際尺寸的坐標值?

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

我想知道從中心拉伸圖像時的坐標值。

我知道紋理圖像中有一個像素坐標,並且正在通過將射線匹配到移動屏幕來使用射線投射。

但是我不知道圖像增長時坐標值如何變化。

在此處輸入圖片說明

當圖像增長時,如何計算以抵消實際尺寸的坐標值?

圖像將增加到1.33f尺寸。

在此處輸入圖片說明

Unity3D中可能有更簡單的方法-我在那里沒有任何經驗-但這聽起來像是一個相當簡單的縮放問題。

您需要了解以下內容:

  • 圖像中心的圖像坐標
    這是向量(width/2, height/2)
  • 圖像中心的屏幕坐標
    圖像中心在屏幕上的位置
  • 比例因子(在此示例中為1.33f

鑒於以上所述,您可以使用簡單的數學計算要觸摸的像素:

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));
}

或者使用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;
}

兩者都假定您的比例在xy是均勻的。 如果希望它靈活地在不同軸上縮放,則可以提供一個縮放向量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM