Hello, I’m hoping someone can solve a problem I am working through. Suppose I create a series of randomized point with the following function:
private static IPoint CreatePoint()
{
// Create randomized point around a circle w/ a bias towards the center
double r = rdm.NextDouble();
double theta = rdm.NextDouble() * Math.PI;
double phi = rdm.NextDouble() * 2.0 * Math.PI;
double cosTheta = Math.Cos(theta);
double sinPhi = Math.Sin(phi);
double cosPhi = Math.Cos(phi);
double x = r * cosPhi;
double y = r * sinPhi * cosTheta;
return new Point(x, y);
}
The function itself is run through a loop n-times and after refusing any overlapping points, each point is stored in an array. Later, in a controller (but really view??) class, I run this, which creates n-amount of circle images at the points previously chosen (multiplied by a scale size):
void Start()
{
// Create the Star System and the Jump Gate connection icons for the galaxy map
for (int i=0; i<galaxy.StarSystems.Length; i++)
{
StarSystem star = galaxy.StarSystems[i];
Vector3 systemPos = new Vector3((float)star.Location.X * scale, (float)star.Location.Y * scale, 0);
CreateCircleSprite(systemPos, i, star.StarType);
// jumpgate stuff, whatever...
}
FitParentContainer(mapContainer);
}
The CreateCircleSprite and FitParentContainer are as below:
private GameObject CreateCircleSprite(Vector3 anchoredPos, int index, StarType type)
{
GameObject circleGameObj = new GameObject("system", typeof(Image));
circleGameObj.transform.SetParent(mapContainer);
// Texture added in shader to create glow effect. See Materials folder
Image circleImage = circleGameObj.GetComponent<Image>();
circleImage.material = circleSpriteMaterials[(int)type];
RectTransform rectTransform = circleGameObj.GetComponent<RectTransform>();
rectTransform.anchoredPosition3D = anchoredPos;
rectTransform.sizeDelta = new Vector3(starSystemRadius, starSystemRadius, 0);
rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
rectTransform.localScale = new Vector3(1, 1, 1);
MapSystemIcon systemIcon = circleGameObj.AddComponent<MapSystemIcon>();
systemIcon.ID = index;
systemIcon.OnClick += SystemIcon_OnClick;
return circleGameObj;
}
public void FitParentContainer(RectTransform container)
{
RectTransform children = container.transform.GetComponentInChildren<RectTransform>();
float minX, maxX, minY, maxY;
minX = maxX = container.localPosition.x;
minY = maxY = container.localPosition.y;
foreach(RectTransform child in children)
{
if (child.name == "system")
{
float tempMinX = child.localPosition.x ;
float tempMaxX = child.localPosition.x;
float tempMinY = child.localPosition.y;
float tempMaxY = child.localPosition.y;
if (tempMinX < minX) { minX = tempMinX; }
if (tempMaxX > maxX) { maxX = tempMaxX; }
if (tempMinY < minY) { minY = tempMinY; }
if (tempMaxY > maxY) { maxY = tempMaxY; }
}
}
container.sizeDelta = new Vector2(maxX - minX, maxY - minY);
}
What I have noticed is that sometimes I have circle images that fall outside the bounds of the parent container, which is very annoying because the parent container is in a masked scroll view which effectively cuts the view of the circles that fall outside the parent container.
To me, this shouldn’t happen because the FitParentContainer calculates the width and height of the parent by getting the subtracting the rightmost position from the leftmost, and similarly with the topmost and bottom-most positions.
If it helps, the parent container min/max anchors are 0.5, and same with the pivot.
Does anyone know why there are some circles that fall outside the bounds of the parent container and know how to keep all child circles within the parent?
Many thanks