基本上,我想在鼠标移动的地方画一条线,就像在绘画中一样,但是,当我每次在 mousePos 上画一个点时,会发生这种情况:
现在,我需要一些帮助将它变成一条线,它没有间隙或任何奇怪的东西。 感谢您的帮助。
我用来生成线条的代码(这不是图片中的代码!)
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
Line newLine = new Line(pixel, point1, point2, 2, Color.White);
allLines.Add(newLine);
foreach (Line lines in allLines)
{
lines.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
和线对象:
public class Line
{
Texture2D texture;
Vector2 point1, point2;
float width;
Color color;
float angle, length;
public Line(Texture2D texture, Vector2 point1, Vector2 point2, float width, Color color)
{
this.texture = texture;
this.point1 = point1;
this.point2 = point2;
this.width = width;
this.color = color;
angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
length = Vector2.Distance(point1, point2);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, point1, null, color, angle, Vector2.Zero, new Vector2(length, width), SpriteEffects.None, 0);
}
}
最佳答案
你想要做的是这样的(扩展 pathfinder666 的回答):
private Point _lastPoint;
protected void MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Graphics g = CreateGraphics();
g.LineTo(_lastPoint.X, _lastPoint.Y, e.X, e.Y);
_lastPoint = new Point(e.X, e.Y);
}
请记住,这可能看起来有点锯齿状。您可能想使用 DrawArc而是将其平滑一点。这取决于您最终要实现的目标。
关于c# - 在鼠标经过的地方画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13017448/