根据给定尺寸和所需数量获取均匀间隔的 X、Y 线

发布于 2024-12-14 07:10:35 字数 317 浏览 0 评论 0原文

我正在使用 Actionscript 3,但这很一般。

我想创建一个可以调用的简单函数,例如 GiveCords(Width, Height, Num),它将获取宽度和高度,将其映射出来并使用 Num 变量将给定的数量均匀地分布在空间中。

假设我给它一个值 500, 500, 1。我希望它返回 250, 250 的 X, Y 位置。

但我希望它返回带有 X, Y 的给定点的数组。

所以如果我给它10分,它会找到最好的位置,让它们彼此距离相等。

我猜有一个简单的公式可以解决这个问题,但我进行了大量搜索,但一无所获。

干杯

I'm working in Actionscript 3, but this is pretty general.

I'd like to make a simple function that I can call, e.g. GiveCords(Width, Height, Num) that will take a width and height, map that out and using the Num variable place the given amount evenly across the space.

Say I give it a value of 500, 500, 1. I'd expect it to return an X, Y position of 250, 250.

But I'd like it to return an array of given points with X, Y.

So If I gave it 10 points, it would find the best position for them all to be of even distance apart from each other.

I'm guessing there is a simple formula for working this out, but I've searched a plenty and found nothing.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涫野音 2024-12-21 07:10:36

如果我理解正确的话,这应该可以完成这项工作:

var object:Object = {width: 500, height:500, num:10};
var points:Array = getCoordinates(object);

function getCoordinates(object:Object):Array {

   var array:Array = new Array();
   var widthMultiplier:Number = object.width / (object.num + 1);
   var heightMultiplier:Number = object.height / (object.num + 1);

   for (a = 1; a <= object.num; a++) {
      var coordinates:Point = new Point();
      coordinates.x = widthMultiplier * a;
      coordinates.y = heightMultipler * a;
      array.push(coordinates);
   }

return array;
}

它需要项目数和总空间,将总空间除以项目数+ 1(以考虑最后一个元素末尾的空间)每个对象递增时间。

编辑:为了回应评论,这里有一个版本,您可以在其中指定您希望对象分布的行数。如果行数不能除以对象数并返回整数,则该函数将返回 null。如果您没有给它行参数,它会假设您希望它跨一行。享受。

var object:Object = {width:500,height:500,num:10};
var points:Array = getCoordinates(object,2);

function getCoordinates(object:Object, rows:int = 1):Array
{
    if ((object.num / rows) % 1)
    {
        return null;
    }
    else
    {
        var columns:int = object.num / rows;

        var array:Array = new Array();
        var widthMultiplier:Number = object.width / (columns + 1);
        var heightMultiplier:Number = object.height / (rows + 1);

        for (var a = 1; a <= rows; a++)
        {
            for (var b = 1; b <= columns; b++)
            {
                var coordinates:Point = new Point();
                coordinates.x = widthMultiplier * b;
                coordinates.y = heightMultiplier * a;
                array.push(coordinates);
            }
        }

        return array;
    }
}

If I understood correctly this should do the job:

var object:Object = {width: 500, height:500, num:10};
var points:Array = getCoordinates(object);

function getCoordinates(object:Object):Array {

   var array:Array = new Array();
   var widthMultiplier:Number = object.width / (object.num + 1);
   var heightMultiplier:Number = object.height / (object.num + 1);

   for (a = 1; a <= object.num; a++) {
      var coordinates:Point = new Point();
      coordinates.x = widthMultiplier * a;
      coordinates.y = heightMultipler * a;
      array.push(coordinates);
   }

return array;
}

It takes the number of items and the total space, divides the total space by the number of items + 1 (to account for the space at the end of the last element) increment the objects each time.

Edit: In response to comments here is a version where you can state the number of rows you want your objects to spread across. If the number of rows does not divide the number of objects and return an integer then the function will return null. If you do not give it a rows paramater it assumes you want it across one row. Enjoy.

var object:Object = {width:500,height:500,num:10};
var points:Array = getCoordinates(object,2);

function getCoordinates(object:Object, rows:int = 1):Array
{
    if ((object.num / rows) % 1)
    {
        return null;
    }
    else
    {
        var columns:int = object.num / rows;

        var array:Array = new Array();
        var widthMultiplier:Number = object.width / (columns + 1);
        var heightMultiplier:Number = object.height / (rows + 1);

        for (var a = 1; a <= rows; a++)
        {
            for (var b = 1; b <= columns; b++)
            {
                var coordinates:Point = new Point();
                coordinates.x = widthMultiplier * b;
                coordinates.y = heightMultiplier * a;
                array.push(coordinates);
            }
        }

        return array;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文