鼠标移动轨迹绘制直线

3
基本上,我想要在鼠标移动的地方画一条线,就像在画图中一样,但是,每一个时刻我在mousePos上画一个点,这种情况就会发生:

This happens

现在,我需要一些帮助把它变成一条没有间隙或奇怪东西的线。

感谢您的帮助。

用于生成线条的代码(这不是图片中的代码!)

        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);
    }
}

你是指曲线还是直线? - Servy
可能,如果您展示实际的代码会有所帮助。 - Steve
@Servy 嗯,我基本上只想在鼠标经过的地方画一条线,它可以是曲线,也可以是直线,这取决于你将鼠标移动到哪里。 - Stan
1
你需要将之前的坐标保存为线段的起点,并使用当前的坐标作为线段的终点。然后在绘制完线段后更新旧的坐标。 - JoshVarty
1
@Stan - 我的意思是路径,而不是线。一条线有两个点,起点/终点。一条路径有一个起点,然后沿着长度有多个点,最后是一个终点。我很久以前(一年多前)用XAML/C#做过类似的事情,效果非常好。 - Nathan Wheeler
显示剩余3条评论
3个回答

4
您需要做的是像这样(在pathfinder666的答案上进行扩展): ```html

您要做的是像这样(在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来使其更加平滑。这取决于你最终想要实现什么。


2

过去当我需要根据鼠标移动绘制线条/路径时,我使用的是Path对象而不是直线。

这里有一个XAML/C#的例子。只需在C#中启动一个新的XAML项目,并将其粘贴到默认的“MainWindow”中:

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private PathFigure _pathFigure = new PathFigure();
    PathFigureCollection _pathCollection = new PathFigureCollection();
    PathSegmentCollection _segments = new PathSegmentCollection();
    private PathGeometry _pathGeometry = new PathGeometry();

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        _pathFigure.Segments = _segments;
        _pathCollection.Add(_pathFigure);
        _pathGeometry.Figures = _pathCollection;
        myPath.Data = _pathGeometry;
    }

    private void Window_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            LineSegment segment = new LineSegment();
            segment.Point = e.GetPosition(this);
            _pathFigure.Segments.Add(segment);

        }
    }

    private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _pathFigure.StartPoint = e.GetPosition(this);
    }
}

MainWindow.xaml:

<Window x:Class="TestPaths.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" MouseMove="Window_MouseMove" MouseLeftButtonDown="Window_MouseLeftButtonDown">
    <Grid>
        <Path Stroke="Black" StrokeThickness="1" Name="myPath" />
    </Grid>
</Window>

输出

PathGeometry示例中的MainWindow

这个示例不会创建任何新的路径,因此当您再次按下左鼠标按钮时(到当前位置),线条会跳跃。非常简单地添加功能以根据需要创建新路径。

此示例消除了保持新点滚动轨迹的需要,因为您的最后一个点始终可在现有对象中使用。有关路径的更多信息,包括如何设置曲线等,请参见MSDN上的PathGeometry文档


1
你可以将最后一个点记在内存中,并从该最后一个点绘制到当前点的线条。你可能需要平滑曲线以使其更加平滑和视觉上吸引人。

使用路径时,您只需将新的线条/曲线添加到几何图形的末尾,从而消除了始终在内存中保留端点的需要。 - Nathan Wheeler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接