Visual C# (Windows) Guide
SFML Graphics: Drawing Lines - RectangleShape
If you want more control over the appearance of the line, you can use a rotated RectangleShape as your line.
Adding this method to your canvas makes it easier to define and add lines.
public void AddRLine(Vector2f Start, Vector2f End,
float Thickness, SFML.Graphics.Color LineColour)
{
Vector2f diff = End - Start;
float length = (float)Math.Sqrt(diff.X * diff.X + diff.Y * diff.Y);
RectangleShape line = new RectangleShape(new Vector2f(length, Thickness));
line.FillColor = LineColour;
line.Origin = new Vector2f(0, Thickness / 2);
line.Position = Start;
// Calculate angle in degrees
float angle = (float)(Math.Atan2(diff.Y, diff.X) * 180.0 / Math.PI);
line.Rotation = angle;
this.AddShape(line);
}
The following code for your main form can generate some lines for you with random colours and thicknesses,
public partial class Form1 : Form
{
private SFML.Graphics.Color[] colours =
{
SFML.Graphics.Color.Blue,
SFML.Graphics.Color.Green,
SFML.Graphics.Color.Red,
};
private Random rng = new Random();
public Form1()
{
InitializeComponent();
// Create an instance of the canvas and add to the form
SFMLCanvas Canvas = new SFMLCanvas(400, 400, SFML.Graphics.Color.White);
Canvas.Location = new Point(20, 20);
this.Controls.Add(Canvas);
Vector2f a, b;
float thickness;
for (int i = 0; i < 10; i++)
{
a = new SFML.System.Vector2f((float)rng.NextDouble() * Canvas.Width,
(float)rng.NextDouble() * Canvas.Height);
b = new SFML.System.Vector2f((float)rng.NextDouble() * Canvas.Width,
(float)rng.NextDouble() * Canvas.Height);
thickness = (float)rng.NextDouble() * 5 + 1;
Canvas.AddRLine(a, b, thickness, colours[rng.Next(colours.Length)]);
}
}
}
You may wish to use a separate data structure for lines if the interactions with the lines are different from the other shapes in your program.

