简体   繁体   中英

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.

在此处输入图片说明

There may be simpler ways in Unity3D - I don't have any experience there - but this sounds like a fairly simple scaling problem.

You'll need to know the following:

  • The image coordinates of the centre of the image
    This is the vector (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)

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:

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 . You could provide a scale vector if you want it to be flexible about scaling in different axes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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