How to find out if two PolyLines lines intersect

I have a problem detecting the intersection of two PolyLines.

Well, the main goal is to compare the last X and Y with another PolyLine and find out if she is facing her intersecting with her.

There are data gaps associated with moving X and Y, so most of the time I cannot find X and Y in another PolyLine.

I think I should compare visualtree or something, not the data itself, but I donโ€™t know how to do it.

<Canvas x:Name="LayoutRoot" Background="Black" Margin="2"> <Polyline x:Name="player3line" Stroke="GreenYellow" StrokeThickness="4" Points="146,106 141,106 136,105 131,105 126,105 121,106 116,108 112,110 108,113 104,115 100,118 96,120 92,123 88,126 84,129 80,132 77,136 74,140 72,144 69,148 67,152 64,156 " /> <Polyline x:Name="player4line" Stroke="Cyan" StrokeThickness="4" Points="85,113 89,116 93,119 97,121 102,123 107,124" /> </Canvas> 

There should be an easy way to check if these two intersect?

+4
source share
2 answers

I decided that I should look for all the coordinates around my point, since the stroke thickness is 4.

So, I decided that I need to check from X-2 to X + 2 and from Y-2 to Y + 2.

So, I did it and it is amazing that it works now, I admit that it is not perfect, but it is simple and at the moment I do not see processor spikes using this method:

  private bool CheckCollision(Point centerPoint) { bool functionReturnValue = false; //wall collision if (centerPoint.X - 1 < 0 || centerPoint.X + 1 > (int)LayoutRoot.ActualWidth || centerPoint.Y - 1 < 0 || centerPoint.Y + 1 > (int)LayoutRoot.ActualHeight) { functionReturnValue = true; } //player collision if (!functionReturnValue) { foreach (var player in playerList) //all players are in this list { for (int i = Convert.ToInt32(centerPoint.X - 2); i < centerPoint.X + 2; i++) { for (int j = Convert.ToInt32(centerPoint.Y - 2); j < centerPoint.Y + 2; j++) { var point = new Point() { X = i, Y = j }; if (player.CoordinatePoints.Contains(point)) { functionReturnValue = true; goto END; } } } } } goto END; END: return functionReturnValue; } 
+1
source

Any collision testing should be done on the data, as there is no magic testing of hardware and software in Silverlight, which I know about.

In the case of 2 polylines, you need to check each line segment on each segment of another line (or simplified versions of one or both for starters).

You can first check if there is a conflict between the borders of the rectangle (the minimum and maximum values โ€‹โ€‹of x, y of each polygon form a bounding box), and if they overlap at all, you need to check each individual segment of the line for a collision.

I donโ€™t know what I came across for. Just a few tricks to speed up the test.

This link shows a high-level example, but there are other game-oriented solutions.

+2
source

All Articles