gpt4 book ai didi

c# - 如何减少和增加曲线的值

转载 作者:行者123 更新时间:2023-12-04 03:59:40 27 4
gpt4 key购买 nike

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

我不擅长解释,所以这里是一个可视化的例子;

Example

第一个图像是如果我只使用 for 循环将 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);
}

第二张图片是我需要的。

I am developing the game on unity and I am using Vector2 for the coordinates but it is not important.
Pure C# or even C++ is welcome.
It is also fine if someone can just explain the math behind what I am trying to do.

最佳答案

也许这可以帮助:

// 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 .

关于c# - 如何减少和增加曲线的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63250079/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com