Home c# Determine whether 3 points lie on one straight line on C #

Determine whether 3 points lie on one straight line on C #




It is necessary to determine whether any 3 points are from the set per 1 straight. The problem is that the coordinates of the points are introduced graphically, clicking the mouse, and ideally on one direct, they turn out to be very rare. In the future, the triangles need to build at these points, and it turns out that the triangle merges into one line, since the differences in several pixels on PictureBox ‘E is not visible. For computation, I used the formula obtained from the direct equation and equated to zero. Sense little. I understand that it is necessary to introduce an error, but I do not understand how to calculate it and how to write it in the code. \
Now I have this: `

for (int i = 0; i & lt; x.count; i ++)
         for (int j = i + 1; j & lt; x.count; j ++)
             for (int k = j + 1; k & lt; x.count; k ++)
          if ((((x [k] - x [i]) * (y [j] - y [i])) - ((x [j] - x [i]) * (y [k] - y [i ])) == 0)

Coordinates are written in two list.

Answer 1, Authority 100%

inside the IF operator is calculated area (double) triangle, built along three points.

But instead of comparison with zero, the absolute value of this area is compared with some small size. Or not the area, but the amount, in order close to the deviation of the points from the straight (instead of 1.0, select a reasonable value):

Area = abs ((x [k] - x [i]) * (y [j] - y [ i])) - ((x [j] - x [i]) * (y [k] - y [i]));
roughsize = max (x [k], x [i], x [j]) - min (x [k], x [i], x [j]) +
      Max (y [k], y [i], y [j]) - min (y [k], y [i], y [j])
If (Area / Roughsize & LT; 1.0) ...

Answer 2, Authority 100%

I see two ways to solve your problem.

  1. Reduce the error of graphic input by bringing the coordinates of clicking to the coordinates of a larger grid. The overall idea is as follows: we fix the grid, in the nodes of which the points of triangles can be located with a step, let’s say in 5 pixels (selected by experimentally to a comfortable value.). Visualize the grid is not necessary. Next, having received the coordinates of clicking, we calculate the local coordinates of the nearest mesh node by dividing the coordinates to the grid step and rounding to the nearest whole. So we make forced customization adjustment. Next, use the existing ways to check the existence of a nondegenerate triangle using the obtained local coordinates of the points on the grid. When subsequent drawing, do not forget to multiply the values ​​of the local coordinates of the grid on the grid step, to obtain coordinates in pixels.

  2. A more difficult option – evaluate the error of the entered data and conclude on the basis of this assessment. There are options:

    • We define the two most distant points A and B and get the equation direct AB. We find a d point d lying at the intersection of the perpendicular passing through the third point C and direct AB. Calculate the ratio of CD / MIN lengths (AD, DB) is and will be our error. Compare error with a small value – a given permissible error. If the error is less permissible – points on one straight line, if more – we have a nondegenerate triangle. But here you should not forget to process boundary cases, for example, when two triangle points are located as close to each other, which can be considered coinciding.
    • Use the circumference of some minimal radius, which will play the role of error, with centers in the audited points and check the intersection of the direct passing through the most distant points from each other with a third-point circle. It also requires checking points for a coincidence, for example, by comparing the distance between them with the radius used by the circle.
    • determine the lengths of all sides, from the length of the greatest side to subtract, the sum of the two remaining and compared with some low value. The method is rather rough, but easy to implement.

Probably mathematicians will be able to offer some more options for estimating the error, I remembered only these on the Vskidka. From a practical point of view, I personally would prefer the first option for its simplicity, but here it all depends on your ultimate goal.

Answer 3

Take 3 points, count the distance between them, take the biggest – it will be the base of the triangle, then the deviation of the average point from the straight will be equal to the height of this triangle spent from it (because it is perpendicular to our direct) Formulas Specify in your program the maximum allowable deviation in pixels and compare it with the resulting height value. If less, then points on one straight line, if more, then on different

Corners necessary for formulas as I remember can be obtained from class vector (a, b)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions