我是一名 MonoGame 开发人员,我想在手机屏幕上画一条曲线,但曲线不规则,而是由几条线组成。
我使用了一些这样的代码:
第一种方法将精灵绘制成事件间隔,如下所示:
// increment is how far apart each sprite is drawn
private void DrawEvenlySpacedSprites(Texture2D texture, Vector2 point1, Vector2 point2, float increment)
{
var distance = Vector2.Distance(point1, point2); // the distance between two points
var iterations = (int)(distance / increment); // how many sprites with be drawn
var normalizedIncrement = 1.0f / iterations; // the Lerp method needs values between 0.0 and 1.0
var amount = 0.0f;
if(iterations == 0)
iterations = 1;
for (int i = 0; i < iterations; i++)
{
var drawPoint = Vector2.Lerp(point1, point2, amount);
_spriteBatch.Draw(texture, drawPoint, Color.White);
amount += normalizedIncrement;
}
}
更新方法:
private Vector2? _previousPoint;
protected override void Update(GameTime gameTime)
{
TouchCollection touches = TouchPanel.GetState();
foreach (TouchLocation touch in touches)
{
if (touch.State == TouchLocationState.Moved)
{
GraphicsDevice.SetRenderTarget(renderTarget);
_spriteBatch.Begin();
if (_previousPoint.HasValue)
DrawEvenlySpacedSprites(texture, _previousPoint.Value, touch.Position, 0.5f);
_spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
}
}
if (touches.Any())
_previousPoint = touches.Last().Position;
else
_previousPoint = null;
base.Update(gameTime);
}
我有这个结果:
为了解决这个问题,我尝试使用贝塞尔曲线,但我无法实现这个解决方案。
拜托,你能帮帮我吗?
等待您的宝贵意见和建议。