我是一名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);
}
我有这个结果:
为了解决这个问题,我试图使用贝塞尔曲线,但我无法实现这个解决方案。
拜托,能帮帮我吗。
等待您的宝贵意见和建议。