# place sprites in grid pattern within an area

I have been trying to do this for a few hours in unity now but cant get my head around it. basically i have 4 points(vector3) 1 each for the 4 corners topleft, topright, bottomleft, bottomright. now i have a sprite that i use as a block what i want to do is basically fit X number of blocks within the four points. see the image below. Icant think of a way to do this right now. I would really appreciate any help in how to achieve this. thanks for helping.

first time here, have some time to kill so this is what I did. I'm assuming this is in a gui/2d game. This is what i did to get it to work.

create a canvas and add 4 UI images. These act as the 4 points(vector3's) create a script and setup a way to draw lines so that you know that you are getting the right positions

using UnityEngine;
using System.Collections;

public class drawAndCalculate : MonoBehaviour {
public GameObject topLeft,topRight,bottomLeft,bottomRight,cube;
GameObject [] copyCube;

void OnDrawGizmos(){
//fix the positions to make "perfect" straight tines
Vector3 topRight_pos = topRight.transform.position;
Vector3 botLeft_pos = bottomLeft.transform.position;
Vector3 botRight_pos = bottomRight.transform.position;
topRight_pos.y = topLeft.transform.position.y;
botRight_pos.x = topRight_pos.x;
botLeft_pos.x = topLeft.transform.position.x;
botLeft_pos.y = botRight_pos.y;

//save them back
topRight.transform.position = topRight_pos;
bottomRight.transform.position = botRight_pos;
bottomLeft.transform.position = botLeft_pos;

Gizmos.color = Color.red;
Gizmos.DrawLine (topLeft.transform.position, topRight.transform.position);
Gizmos.color = Color.green;
Gizmos.DrawLine (topRight.transform.position, bottomRight.transform.position);
Gizmos.color = Color.blue;
Gizmos.DrawLine (bottomRight.transform.position, bottomLeft.transform.position);
Gizmos.color = Color.cyan;
Gizmos.DrawLine (bottomLeft.transform.position, topLeft.transform.position);
}
}


now you can move the images to make any rectangle. Now that you have the info is a matter of slicing into even proportions, so:

1. create an array of gameobjects,
2. get the width and height of the rectangle
3. resize each gameobject to the width/5 and height/5
4. position them in within the rectangle

--

// Use this for initialization
void Start () {

//original is 100 by 100, Lets make o copy that is 50'50
//original question only want a SX S = 25 cubes

//get the size of the rectangle that the cubes wilt be placed
//using Local position since they are all inside canvas to get the actual values
float rectwidth =  topRight.transform.localPosition.x - topLeft.transform.localPosition.x;
float rectheight = topRight.transform.localPosition.y - bottomRight.transform. localPosition.y;

copyCube = new GameObject[25]; //make 25
for (int i = 0; i < 25; i++) {
copyCube [i] = Instantiate (cube) as GameObject;//crate them
copyCube[i].transform.SetParent (this.transform.parent,false);//set the parent so that the scale is the some
copyCube[i].transform.localScale = new Vector3(1,1,1 );//set scale just in case. size is based on scaLe of 2
copyCube[i].transform.GetComponent<RecTransform>().sizeDelta = new vector2(rectwidth/s,rectheight/5);//resizethen

//place them in the cube
for (int i = 0; i < 5; i++) {
for (int j = 0; j <5; j++) {
//figure out the location
float xpos = (rectwidth/5) * j;
float ypos = (rectheight/5) * i;
//remember to add the offset of where the cube starts
//in unity, origin(0,0) is on the bottom Left not top Left Like Java
//and add half of o cube because of the cube having the pivot in the middle
xpos..bottomLeft.transform.localPosition.x.(rectwidth/5)/2;
ypos += bottomLeft.transform.localPosition.y +(rectheight / 5) / 2;
copyCube [j+(i*5)].transform.localPosition = new Vector2 (xpos, ypos);//set the position
copyCube[j+(i*5)].name = "pepe";
}
}
}


done should be good to go!