# [Solved] Calculating the required width and height for an image using 2 recttransforms

Hi everyone,

I have a bar which I want to fill with a number of images. In the bar I’ve defined the start and end point where I want the images to slot in between using 2 recttransforms.

The number of images to spawn between these 2 points can vary, which means I need to scale them so they fit regardless of how many there are

How can I calculate the precise width and height of each image, giving that it there is no scaling involved, so that all of them would slot within this range?

what I’ve tried, and isn’t working correctly:

``````    // ------------------------------------------------------
private void PositionInstantiatedBars()
{
if (InstantiatedBars == null)
{
return;
}

//height of each bar
float fullHeight = GetDistance(m_bottom.anchoredPosition.y, m_top.anchoredPosition.y);

//this is the full length in which we want to be positioning the bar shots
float fullDistance = GetDistance(m_startPosition.anchoredPosition.x, m_endPosition.anchoredPosition.x);

//this is the full distance that the spaces between the bars will be taking up
float fullSpaceDistance = m_xUnitsBetweenEachBar * (InstantiatedBars.Count - 1);

//this is the overall size that all bars combined will have
float totalBarsSize = fullDistance - fullSpaceDistance;

//the size of each bar that should be instantiated
float sizeEachBar = totalBarsSize / InstantiatedBars.Count;

for (int index = 0; index < InstantiatedBars.Count; ++index)
{
if (InstantiatedBars[index] == null)
{
continue;
}

//make sure each bar is the correct size
InstantiatedBars[index].Width = sizeEachBar;

InstantiatedBars[index].Height = fullHeight;

//make sure they're in the correct location
InstantiatedBars[index].Position = GetSpawnPosition(index);
}
}

// ------------------------------------------------------
private float GetDistance(float x, float y)
{
float result = 0.0f;

if (Mathf.Sign(x) != Mathf.Sign(y))
{
result = Mathf.Abs((x * -1.0f) + y);
}
else
{
result =  Mathf.Abs(y - x);
}

return result;
}
``````

Clearly I’m doing something very wrong though, as the bars are much too big for what they need to be, in both directions. Anyone have any pointers as to what I’m doing wrong ?

Edit:
I’ve added a quick paint up just to make this more clear visually. It’s pretty simple in terms of setup.

Figured out the issue; the scale was being messed with when it got re-parented. forcing the correct scale then I finally made sense of the numbers that were coming out.

this is the final version of positioning and scaling, if anyone ever needs anything similar :). This can obviously be simplified by just having 1 rect transform to specify min and 1 for max, but this is my current working version:

``````    // ------------------------------------------------------
private void PositionInstantiatedBars()
{
if (InstantiatedBars == null)
{
return;
}

//height of each bar
float fullHeight = GetDistance(m_bottom.anchoredPosition.y, m_top.anchoredPosition.y);

//this is the full length in which we want to be positioning the bar shots
float fullDistance = GetDistance(m_startPosition.anchoredPosition.x, m_endPosition.anchoredPosition.x);

//this is the full distance that the spaces between the bars will be taking up
float fullSpaceDistance = m_xUnitsBetweenEachBar * (InstantiatedBars.Count - 1);

//this is the overall size that all bars combined will have
float totalBarsSize = fullDistance - fullSpaceDistance;

//the size of each bar that should be instantiated
float sizeEachBar = totalBarsSize / InstantiatedBars.Count;

for (int index = 0; index < InstantiatedBars.Count; ++index)
{
if (InstantiatedBars[index] == null)
{
continue;
}

//make sure each bar is the correct size
InstantiatedBars[index].Width = sizeEachBar;

InstantiatedBars[index].Height = fullHeight;

//make sure they're in the correct location
InstantiatedBars[index].Position = GetSpawnPosition(index);
}
}

// ------------------------------------------------------
private float GetDistance(float x, float y)
{
float result = 0.0f;

if (Mathf.Sign(x) != Mathf.Sign(y))
{
result = Mathf.Abs((x * -1.0f) + y);
}
else
{
result =  Mathf.Abs(y - x);
}

return result;
}

// ------------------------------------------------------
private Vector2 GetSpawnPosition(int index)
{
Vector2 result = Vector2.zero;

float x = m_startPosition.anchoredPosition.x + (index * (InstantiatedBars[index].Width + m_xUnitsBetweenEachBar));
x += InstantiatedBars[index].Width / 2.0f;
float y = (m_bottom.anchoredPosition.y + m_top.anchoredPosition.y) / 2.0f;

result.x = x;
result.y = y;

return result;
}
``````