0

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

enter image description here

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.

explanation

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 )

KnapSac
  • 1
  • 4
  • I would put the Graphic into a panel (located on the win form) so the origin is the upper left corner of the panel. – jdweng Jan 02 '20 at 15:49
  • I am not entirely sure what you mean. This is already on a ```Panel```, which is located on a form. – KnapSac Jan 02 '20 at 15:52
  • You need to draw the arc in different directions, depending on which direction you're travelling around it. Sweeping from `0` to `90` is different to sweeping from `90` to `0`. – canton7 Jan 02 '20 at 15:58
  • I am currently using -270 for my starting angle, and 0 for my sweeping angle. What would be the opposite direction of that? Because simply swapping those doesn't give me the same arc, and using 90 for both angles yields the same result. – KnapSac Jan 02 '20 at 16:10
  • Are you sure you're sweeping `0` degrees from `-270`? That shouldn't sweep at all. The opposite of `path.AddArc(rect, 0, 90)` should be `path.AddArc(rect, 90, -90)`. – canton7 Jan 02 '20 at 16:13
  • My apologies, my sweeping angle is 90, so that would be ```path.AddArc(rect, -270, 90)```. – KnapSac Jan 02 '20 at 16:24
  • `path.AddArc(rect, -270, 90)` starts at `-270`, and sweeps `90` degrees clockwise to end up at `0` degrees. The opposite to that would be to start at `0` and sweep `90` degrees anti-clockwise to end up at `270`. I.e. `path.AddArc(rect, 0, -90)` – canton7 Jan 02 '20 at 16:29
  • See the `RoundedCornerRectangle` method [here](https://stackoverflow.com/a/56533229/7444103) – Jimi Jan 02 '20 at 16:44

0 Answers0