I have a road layout (derived Panel
in WinForms, the roads are stored as images and set as the BackgroundImage
of the Panel
when it is created), and I want to make cars move along the roads. To achieve this I was hoping to use a GraphicsPath
, and than move an image along that GraphicsPath
. Unfortunately, I can't get the GraphicsPath
to generate correctly. I am able to create the arc I want by taking two points and the desired angles, like so:
private void CreateArc( GraphicsPath path )
{
Point source = new Point( road.Location.X + SmallVerticalOffset, road.Location.Y );
Point destination = new Point( road.Location.X + TileLength, road.Location.Y + LargeHorizontalOffset );
Rectangle boundingBox = CreateRectangle( source, destination );
int startingAngle = -270; //Same result with 90 for both angles
int sweepingAngle = 0;
path.AddArc( boundingBox, startingAngle, sweepingAngle );
}
private Rectangle CreateRectangle( Point source, Point destination )
{
return new Rectangle( Math.Min( source.X, destination.X ),
Math.Min( source.Y, destination.Y ),
Math.Abs( source.X - destination.X ),
Math.Abs( source.Y - destination.Y ) );
}
With the straight lines added elsewhere, this is the result
Instead, I want this line to be continuous, by connecting to the top of the arc, and continuing on after the arc. That is also what I was expecting, but alas. I would like to solve this using WinForms
, without any additional libraries, if possible.
EDIT
It took me a lot longer than I would like to admit, but I finally figured it out. If there is someone else struggling with this, maybe this short explanation will help you.
The previous GraphicsPath
segment attaches to the arc at the blue arrow, pointing at the intersection of the x-axis and the circle on the right hand side. To create a continuous line, you need to pick your angles in such a way, that the starting point is moved along the circle to the point where you want your arc to start. Let's say you want the left halve of the circle, starting at the top and ending at the bottom. In that case your startingAngle
would be 270, because the angles are calculated counter-clockwise from the x-axis. The opposite goes for your sweeping angle, and instead of starting at the x-axis, we now start at the point specified by our starting angle. Because we want the left halve of our circle, our sweepingAngle
will be -180, because we are going backwards from the point we calculated with our startingAngle
. To get the left halve of this circle, we would thus write path.AddArc( boundingbox, 270, -180 )