Java - Make Plot Points, Near Linear - Exactly Lin

2019-09-19 05:41发布

How would I go about changing points that are 'near' linear (within a threshold), actually linear?

I have some code that checks if 3 points are linear to one another (give or take), and I want to replace those coordinates with new ones that are 100% inline.

        double distance = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);

        double threshold = 4;

        if (Math.abs(distance) <= threshold) {
            // is Near line
            return true;
        }
        else
            return false;

This is an EXTENSION of another post of mine... This is NOT a repost, simply a related topic:

Java - Average Linear Graph Plots

1条回答
祖国的老花朵
2楼-- · 2019-09-19 05:46

The technical term for snapping a point to a line is projecting a point to a line(-segment)

The only question that remains: Should the points projected to the line or to the line segment? (A line segment is only between two points, the line has infinity length and goes through both points)

The code below solves both: To allow also projecting points to th epart of the line that is outside of points A->B, the code would be much simpler, but this is covered in the link below, too.

See http://forums.codeguru.com/showthread.php?194400-Distance-between-point-and-line-segment

The projected point is in the variables (xx,yy): (xx,yy) is the point on the lineSegment closest to (cx,cy)

查看更多
登录 后发表回答