如何减少和增加曲线值

时间:2020-08-04 15:23:13

标签: c# unity3d math

我正在尝试为我的游戏做一个挖掘工具,我具有点A和点B的x和y坐标,我要做的是在这些点之间创建一条曲线,不需要任何图形化图形,我只需要遍历这些坐标(浮点数x,浮点数y)。

我不善于解释,所以这里是一个直观的例子;

Example

如果我仅使用for循环将y值减小到中间,然后从中间到结尾增加y,则第一张图片将发生。

//Very specific code for my example 
//I wrote it just for this example so I am not sure if it works

float y;
float x;

public void Example(float startX, float endX, float startY, float endY, float depth)
{
    y = startY;
    x = startX;
    float changeAmountOfY = depth / (endX - startX);

    for (int i = (int)startX; i < (startX + endX) / 2; i++)
    {
        x++;
        y -= changeAmountOfY; 
    }

    for (int i = (int)(startX + endX) / 2; i < endX; i++)
    {
        x++;
        y += changeAmountOfY;
    }
}

public void ChangeCoordinates()
{
    Example(100f, 200f, 100f, 100f, 50f);
}

第二张图片就是我所需要的。

我正在统一开发游戏,我使用Vector2作为坐标,但这并不重要。
欢迎使用纯C#甚至C ++。
如果有人可以解释我要做什么的数学方法也很好。

1 个答案:

答案 0 :(得分:1)

也许这可以帮助您

// Calculate radius
int radius = (B.X - A.X) / 2;

// Calculate middle
int middle_x = A.X + radius;
int middle_y = A.Y;
// or
int middle_y = (A.Y + B.Y) / 2;


// Coordinates for a semicircle
// 0 to 180 degree
for (int i = 0; i <= 180; i++)
{
  double x_coordinate = middle_x + radius * Math.Cos(i * Math.PI / 180);

  // Opened to bottom
  double y_coordinate = middle_y + radius * Math.Sin(i * Math.PI / 180);

  // or opened to top
  double y_coordinate = middle_y - radius * Math.Sin(i * Math.PI / 180);
}

看看unit circle