基本上,我想绘制鼠标所在的一条线,就像在绘画中一样,但是,当我在每个刻度上在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);
}
}
答案 0 :(得分:4)
你想要做的是这样的事情(扩展到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代替将其平滑一点。这取决于你最终要完成的目标。
答案 1 :(得分: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 Docs on the MSDN。
答案 2 :(得分:1)
你如何保持记忆中的最后一点,并从最后一点到当前点画出一条线。你可能需要使曲线平滑,使其平滑且具有视觉吸引力。